Esempio n. 1
0
        public async Task <string> Save(JObject data)
        {
            dynamic groupDto     = data;
            int     groupId      = groupDto.GroupId;
            int     entityTypeId = groupDto.EntityTypeId;
            string  groupName    = groupDto.Name;

            if (!AuthorizeManager.AuthorizeActionOnEntityId(groupId, (int)EntityIdentity.Group,
                                                            (int)ActionKey.EditGroup))
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.InvalidAccessToEditGroup, groupName));
            }

            JArray sremovedListArray = groupDto.RemovedList;
            var    removedList       = sremovedListArray.ToObject <List <int> >();
            JArray addedListArray    = groupDto.AddedList;
            var    addedList         = addedListArray.ToObject <List <int> >();

            foreach (var item in addedList)
            {
                if (entityTypeId == 101)
                {
                    var group = new EntityGroup()
                    {
                        GroupId      = groupId,
                        EntityTypeId = entityTypeId,
                        LinkId       = item
                    };
                    _contentManagementContext.EntityGroups.Add(group);
                }
                else
                {
                    var group = new EntityGroup()
                    {
                        GroupId              = groupId,
                        EntityTypeId         = entityTypeId,
                        MasterDataKeyValueId = item
                    };
                    _contentManagementContext.EntityGroups.Add(group);
                }
            }

            if (removedList.Count > 0)
            {
                if (entityTypeId == 101)
                {
                    _contentManagementContext.EntityGroups.Where(eg => removedList.Contains(eg.LinkId ?? 0) && eg.GroupId == groupId).Delete();
                }
                else
                {
                    _contentManagementContext.EntityGroups.Where(eg => removedList.Contains(eg.MasterDataKeyValueId ?? 0) && eg.GroupId == groupId).Delete();
                }
            }

            await _contentManagementContext.SaveChangesAsync();

            return(entityTypeId == 101 ? "link":"masterData");
        }
Esempio n. 2
0
        public async Task <bool> DeleteUser(JObject user)
        {
            dynamic userDto = user;
            int     id;

            try
            {
                id = userDto.Id;
            }
            catch (Exception)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.FieldMustBeNumeric, "User Id"));
            }
            var appUser = await _userManager.FindByIdAsync(id);

            if (appUser == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.UserNotFound));
            }

            AuthorizeManager.SetAndCheckModifyAndAccessRole(appUser, null, false);

            var userProfileId = appUser.Id;

            var result = await _userManager.DeleteAsync(appUser);

            if (result.Succeeded)
            {
                var userProfile = await _contentManagementContext.Users.FirstOrDefaultAsync(up => up.Id == userProfileId);

                if (userProfile != null)
                {
                    _contentManagementContext.Users.Remove(userProfile);

                    await _contentManagementContext.SaveChangesAsync();
                }

                return(true);
            }

            throw new KhodkarInvalidException(result.Errors.First());
        }
Esempio n. 3
0
        public async Task <MasterDataKeyValue> Save(JObject data, bool isAuthroize = false)
        {
            dynamic masterDataDto = data;
            int?    masterDataId  = masterDataDto.Id;
            var     masterData    = new MasterDataKeyValue
            {
                Id = masterDataId ?? 0
            };
            bool isNew = masterDataDto.IsNew;

            //bool isPath;

            //try
            //{
            //    isPath = masterDataDto.IsPath;
            //}
            //catch (Exception)
            //{

            //    isPath = false;
            //}

            //bool isPathSecond;

            //try
            //{
            //    isPathSecond = masterDataDto.IsPathSecond;
            //}
            //catch (Exception)
            //{

            //    isPathSecond = false;
            //}

            int pathOrUrlProtocolId, secondPathOrUrlProtocolId;

            try
            {
                pathOrUrlProtocolId = masterDataDto.PathOrUrlProtocolId;
            }
            catch (Exception)
            {
                pathOrUrlProtocolId = (int)Protocol.LocalUrlPorotocol;
            }

            try
            {
                secondPathOrUrlProtocolId = masterDataDto.SecondPathOrUrlProtocolId;
            }
            catch (Exception)
            {
                secondPathOrUrlProtocolId = (int)Protocol.LocalUrlPorotocol;
            }

            var currentMasterData = await ContentManagementContext.MasterDataKeyValues.AsNoTracking().SingleOrDefaultAsync(md => md.Id == masterData.Id);

            if (!isNew)
            {
                if (currentMasterData == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.MasterDataKeyValuesNotFound));
                }

                if (currentMasterData.EditMode)
                {
                    SourceControl.CheckCodeCheckOute(masterData);
                }

                masterData            = currentMasterData;
                masterData.RowVersion = masterDataDto.RowVersion;

                ContentManagementContext.MasterDataKeyValues.Attach(masterData);
            }
            else
            {
                ContentManagementContext.MasterDataKeyValues.Add(masterData);
            }
            masterData.TypeId = masterDataDto.TypeId;

            //if (!isPath)
            //{
            //    if (masterDataUrl.IndexOf(Helper.RootUrl, StringComparison.Ordinal) != 0)
            //    {
            //        masterDataUrl = Helper.RootUrl + masterDataUrl;
            //    }

            //}
            //else
            //{

            //    if ( masterDataUrl.IndexOf("~", StringComparison.Ordinal) != 0)
            //    {
            //        if (masterDataUrl.IndexOf(Helper.RootUrl, StringComparison.Ordinal) != 0)
            //            masterDataUrl = "~" + Helper.RootUrl + masterDataUrl;
            //        else
            //            masterDataUrl = "~" + masterDataUrl;
            //    }
            //}
            string masterDataUrlOrPath = ProperPathOrUrl(Convert.ToString(masterDataDto.PathOrUrl), pathOrUrlProtocolId);

            if (masterDataUrlOrPath != Helper.RootUrl && masterData.TypeId != (int)EntityIdentity.BundleSource)
            {
                var repeatedMasterData =
                    await
                    ContentManagementContext.MasterDataKeyValues.Where(
                        md => md.PathOrUrl == masterDataUrlOrPath && md.TypeId == masterData.TypeId).CountAsync()
                ;

                if ((repeatedMasterData > 0 && isNew) || (repeatedMasterData > 1 && !isNew))
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedValue, masterDataUrlOrPath));
                }
            }
            masterData.PathOrUrl = masterDataUrlOrPath;
            try
            {
                int parentId = masterDataDto.ParentId;
                if (currentMasterData?.ParentId != parentId || isNew)
                {
                    var parentCode = await ContentManagementContext.MasterDataKeyValues.SingleOrDefaultAsync(md => md.Id == parentId);

                    if (parentCode == null)
                    {
                        throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.ParentRecordNotFound));
                    }
                    AuthorizeManager.CheckParentNodeModifyAccessForAddingChildNode(parentCode, parentCode.Id);
                }
                masterData.ParentId = parentId;
            }
            catch (KhodkarInvalidException)
            {
                throw;
            }
            catch (Exception)
            {
                masterData.ParentId = null;
            }

            try
            {
                masterData.Key = masterDataDto.Key;
            }
            catch (Exception)
            {
                masterData.Key = null;
            }

            try
            {
                masterData.Value = masterDataDto.Value;
            }
            catch (Exception)
            {
                masterData.Value = null;
            }

            try
            {
                masterData.ForeignKey1 = masterDataDto.ForeignKey1;
            }
            catch (Exception)
            {
                masterData.ForeignKey1 = null;
            }

            try
            {
                masterData.ForeignKey2 = masterDataDto.ForeignKey2;
            }
            catch (Exception)
            {
                masterData.ForeignKey2 = null;
            }

            try
            {
                masterData.ForeignKey3 = masterDataDto.ForeignKey3;
            }
            catch (Exception)
            {
                masterData.ForeignKey3 = null;
            }

            masterData.SecondCode = masterDataDto.SecondCode;


            string secondMasterDataPathOrUrl = masterDataDto.SecondPathOrUrl;

            masterData.SecondPathOrUrl = ProperPathOrUrl(secondMasterDataPathOrUrl, secondPathOrUrlProtocolId);

            masterData.Name = masterDataDto.Name;
            masterData.Code = masterDataDto.Code;
            if (masterData.Code != null)
            {
                var repeatedMasterData =
                    await
                    ContentManagementContext.MasterDataKeyValues.Where(
                        md => md.Code == masterData.Code && md.TypeId == masterData.TypeId).CountAsync();

                if ((repeatedMasterData > 0 && isNew) || (repeatedMasterData > 1 && !isNew))
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedValue, masterData.Code));
                }
            }



            masterData.Guid        = masterDataDto.Guid;
            masterData.Description = masterDataDto.Description;
            masterData.Data        = masterDataDto.Data;
            masterData.Version     = (currentMasterData?.Version ?? 0) + 1;

            try
            {
                masterData.Order = masterDataDto.Order;
            }
            catch (Exception)
            {
                masterData.Order = 1;
            }
            masterData.IsLeaf = masterDataDto.IsLeaf;
            masterData.IsType = masterDataDto.IsType;

            if (masterData.IsType && masterData.IsLeaf)
            {
                throw new ConstraintException(LanguageManager.ToAsErrorMessage(ExceptionKey.MasterDataTypeIsLeaf));
            }

            try
            {
                masterData.TypeId = masterDataDto.TypeId;
            }
            catch (Exception)
            {
                throw new ValidationException(LanguageManager.ToAsErrorMessage(ExceptionKey.FieldMustBeNumeric, "TypeId"));
            }

            try
            {
                masterData.ParentTypeId = masterDataDto.ParentTypeId;
            }
            catch (Exception)
            {
                if (masterData.IsType)
                {
                    throw new ValidationException(LanguageManager.ToAsErrorMessage(ExceptionKey.FieldMustBeNumeric, "ParentTypeId"));
                }

                masterData.ParentTypeId = null;
            }

            masterData.Language = Config.DefaultsLanguage;
            //if(masterData.IsLeaf)
            if (masterData.TypeId == (int)EntityIdentity.Permission && !isAuthroize)
            {
                throw new UnauthorizedAccessException(LanguageManager.ToAsErrorMessage(ExceptionKey.InvalidGrant));
            }

            if (currentMasterData != null)
            {
                masterData.ViewRoleId   = currentMasterData.ViewRoleId;
                masterData.ModifyRoleId = currentMasterData.ModifyRoleId;
                masterData.AccessRoleId = currentMasterData.AccessRoleId;
            }


            AuthorizeManager.SetAndCheckModifyAndAccessRole(masterData, masterDataDto);


            masterData.Status      = masterDataDto.Status;
            masterData.EditMode    = masterDataDto.EditMode;
            masterData.EnableCache = masterDataDto.EnableCache;
            try
            {
                masterData.SlidingExpirationTimeInMinutes = masterDataDto.SlidingExpirationTimeInMinutes;
            }
            catch (Exception)
            {
                masterData.SlidingExpirationTimeInMinutes = 0;
            }
            await ContentManagementContext.SaveChangesAsync();

            if (masterData.TypeId == (int)EntityIdentity.SqlServerConnections)
            {
                WebConfigManager.AddOrUpdateConnection(masterData.Code, masterData.SecondCode,
                                                       ConnectionProvider.SqlServer);
            }
            else
            {
                UpdateMasterDatakeyVlaueSettingOfWebConfig(masterData);
            }
            return(masterData);
        }
        public async Task <LanguageAndCulture> Save(JObject data)
        {
            dynamic languageAndCultureDto = data;
            int?    languageAndCultureId  = languageAndCultureDto.Id;
            var     languageAndCulture    = new LanguageAndCulture()
            {
                Id = languageAndCultureId ?? 0
            };
            bool isNew   = languageAndCultureDto.isNew;
            bool publish = languageAndCultureDto.publish;
            var  currentLanguageAndCulture = await _contentManagementContext.LanguageAndCultures.AsNoTracking().SingleOrDefaultAsync(ln => ln.Id == languageAndCulture.Id);

            if (!isNew)
            {
                if (currentLanguageAndCulture == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.LanguageAndCultureNotFound));
                }


                languageAndCulture            = currentLanguageAndCulture;
                languageAndCulture.RowVersion = languageAndCultureDto.RowVersion;

                _contentManagementContext.LanguageAndCultures.Attach(languageAndCulture);
            }
            else
            {
                _contentManagementContext.LanguageAndCultures.Add(languageAndCulture);
            }

            languageAndCulture.Country       = languageAndCultureDto.Country;
            languageAndCulture.Culture       = languageAndCultureDto.Culture;
            languageAndCulture.Language      = languageAndCultureDto.Language;
            languageAndCulture.FlagId        = languageAndCultureDto.FlagId;
            languageAndCulture.IsDefaults    = languageAndCultureDto.IsDefaults;
            languageAndCulture.IsRightToLeft = languageAndCultureDto.IsRightToLeft;

            //for force update and change version for force client to get new versipn of language.js
            languageAndCulture.Version = (currentLanguageAndCulture?.Version ?? 0) + 1;

            languageAndCulture.Status = languageAndCultureDto.Status;

            if (currentLanguageAndCulture != null)
            {
                languageAndCulture.ViewRoleId   = currentLanguageAndCulture.ViewRoleId;
                languageAndCulture.ModifyRoleId = currentLanguageAndCulture.ModifyRoleId;
                languageAndCulture.AccessRoleId = currentLanguageAndCulture.AccessRoleId;
            }

            AuthorizeManager.SetAndCheckModifyAndAccessRole(languageAndCulture, languageAndCultureDto);

            await _contentManagementContext.SaveChangesAsync();

            await _contentManagementContext.LanguageAndCultures.Where(lc => lc.Language == languageAndCulture.Language && lc.Id != languageAndCulture.Id)
            .UpdateAsync(t => new LanguageAndCulture()
            {
                Version = languageAndCulture.Version
            });

            string jsCode = languageAndCultureDto.JsCode;

            if (!string.IsNullOrEmpty(jsCode))
            {
                await WriteFileAsync(Config.ResourcesSourceCodePath, languageAndCulture.Language, ".js", jsCode);

                if (!publish)
                {
                    return(languageAndCulture);
                }

                await WriteFileAsync(Config.ResourcesDistPath, languageAndCulture.Language, ".js", _compressManager.CompressJavaScript(jsCode, languageAndCulture.Country));
            }
            UpdateWebConfigSetting(languageAndCulture, ActionKey.Add);



            CacheManager.Remove(CacheManager.GetBrowsersCodeInfoKey(CacheKey.BrowsersCodeInfo.ToString(),
                                                                    "~/" + languageAndCulture.Language));


            //var bundleInfo =
            //    SourceControl.BrowsersCodeInfos.FirstOrDefault(bc => bc.BundleUrl == languageAndCulture.Language);

            //if(bundleInfo != null)
            //bundleInfo.Version
            //    = Helper.UrlEncode(System.Text.Encoding.UTF8.GetString(languageAndCulture.RowVersion));

            //SourceControl.BrowsersCodeInfos.Remove(
            //    SourceControl.BrowsersCodeInfos.Find(bc => bc.BundleUrl == languageAndCulture.Language));

            return(languageAndCulture);
        }
Esempio n. 5
0
        public async Task <File> Save(JObject data)
        {
            dynamic fileDto = data;

            string oldUrl = "";
            int?   fileId = fileDto.Id;
            var    file   = new File()
            {
                Id = fileId ?? 0
            };
            var currentFile = await _contentManagementContext.Files.AsNoTracking()
                              .SingleOrDefaultAsync(fl => fl.Id == file.Id);

            try
            {
                if (currentFile == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.FileNotFound));
                }

                oldUrl          = currentFile.Url;
                file            = currentFile;
                file.RowVersion = fileDto.RowVersion;
                _contentManagementContext.Files.Attach(file);
            }
            catch (Exception)
            {
                _contentManagementContext.Files.Add(file);
            }

            try
            {
                file.Guid = fileDto.Guid;
            }
            catch (Exception)
            {
                file.Guid = SecureGuid.NewGuid();
            }

            string filePathUrl = fileDto.Url;

            if (filePathUrl.IndexOf(Helper.RootUrl, StringComparison.Ordinal) != 0)
            {
                filePathUrl = Helper.RootUrl + filePathUrl;
            }
            if (filePathUrl.LastIndexOf(Helper.RootUrl, StringComparison.Ordinal) == filePathUrl.Length - 1)
            {
                filePathUrl = filePathUrl.Substring(0, filePathUrl.Length - 1);
            }

            file.Name        = fileDto.Name;
            file.Description = fileDto.Description;
            file.TypeCode    = fileDto.TypeCode;


            var repeatedLink = await _contentManagementContext.FilePaths.Where(fp => fp.Url == filePathUrl).CountAsync();

            if ((repeatedLink > 0 && oldUrl == "") || (repeatedLink > 1 && oldUrl == ""))
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedValue, filePathUrl));
            }

            file.ContentType = _fileSystemManager.GetExtension(filePathUrl);
            file.Url         = filePathUrl;
            file.Size        = _fileSystemManager.GetFileSize(filePathUrl);
            file.Language    = Config.DefaultsLanguage;
            file.Content     = _fileSystemManager.FileToByte(filePathUrl);

            //if(service.IsLeaf)
            if (currentFile != null)
            {
                file.ViewRoleId   = currentFile.ViewRoleId;
                file.ModifyRoleId = currentFile.ModifyRoleId;
                file.AccessRoleId = currentFile.AccessRoleId;
            }
            AuthorizeManager.SetAndCheckModifyAndAccessRole(file, fileDto);

            file.Status = fileDto.Status;


            await _contentManagementContext.SaveChangesAsync();

            return(file);
        }
Esempio n. 6
0
        public async Task <MasterDataKeyValue> Save(JObject data)
        {
            dynamic serviceDto = data;
            int?    serviceId  = serviceDto.Id;
            var     service    = new MasterDataKeyValue
            {
                Id     = serviceId ?? 0,
                TypeId = (int)EntityIdentity.Service
            };
            bool isNew = serviceDto.IsNew;


            bool   checkIn = serviceDto.CheckIn;
            string comment = serviceDto.Comment;

            var currentService = await _contentManagementContext
                                 .MasterDataKeyValues.AsNoTracking().SingleOrDefaultAsync(sv => sv.Id == service.Id && sv.TypeId == (int)EntityIdentity.Service);

            if (!isNew)
            {
                if (currentService == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.ServiceNotFound));
                }

                if (currentService.EditMode)
                {
                    _sourceControl.CheckCodeCheckOute(currentService);
                }

                service            = currentService;
                service.RowVersion = serviceDto.RowVersion;

                _contentManagementContext.MasterDataKeyValues.Attach(service);
            }
            else
            {
                _contentManagementContext.MasterDataKeyValues.Add(service);
            }



            string serviceCode = serviceDto.Code;

            if (serviceCode.IndexOf(_codeTemplate.ServicePrefix, StringComparison.Ordinal) != 0)
            {
                serviceCode = _codeTemplate.ServicePrefix + "." + serviceCode;
            }

            string serviceUrl = serviceDto.Url;

            if (serviceUrl.IndexOf(Helper.RootUrl, StringComparison.Ordinal) != 0)
            {
                serviceUrl = Helper.RootUrl + serviceUrl;
            }
            if (serviceUrl.LastIndexOf(Helper.RootUrl, StringComparison.Ordinal) == serviceUrl.Length - 1 && serviceUrl != "/")
            {
                serviceUrl = serviceUrl.Remove(serviceUrl.LastIndexOf(Helper.RootUrl, StringComparison.Ordinal));
            }
            var repeatedService = await _contentManagementContext
                                  .MasterDataKeyValues.Where(sr => sr.PathOrUrl == serviceUrl && sr.TypeId == (int)EntityIdentity.Service).CountAsync()
            ;

            if ((repeatedService > 0 && isNew) || (repeatedService > 1 && !isNew))
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedValue, serviceUrl));
            }


            int?parentId = serviceDto.ParentId;

            if (currentService?.ParentId != parentId || isNew)
            {
                var parentCode = await _contentManagementContext
                                 .MasterDataKeyValues.SingleOrDefaultAsync(sr => sr.Id == parentId && sr.TypeId == (int)EntityIdentity.Service);

                if (parentCode == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.ParentRecordNotFound));
                }
                AuthorizeManager.CheckParentNodeModifyAccessForAddingChildNode(parentCode, parentCode.Id);
            }

            service.ParentId = parentId;

            service.Name = serviceDto.Name;
            service.Code = serviceCode;

            repeatedService = await _contentManagementContext
                              .MasterDataKeyValues.Where(sr => sr.Code == service.Code && sr.TypeId == (int)EntityIdentity.Service)
                              .CountAsync();

            if ((repeatedService > 0 && isNew) || (repeatedService > 1 && !isNew))
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedValue, service.Code));
            }


            service.Guid        = serviceDto.Guid;
            service.Description = serviceDto.Description;
            service.Version     = (currentService?.Version ?? 0) + 1;
            service.PathOrUrl   = serviceUrl;
            try
            {
                service.Order = serviceDto.Order;
            }
            catch (Exception)
            {
                service.Order = 1;
            }

            //LOG SERVICE EVENT?1:TRUE,2:FALSE
            try
            {
                service.Key = serviceDto.Key;
            }
            catch (Exception)
            {
                service.Key = 0;
            }

            service.IsLeaf   = serviceDto.IsLeaf;
            service.Language = Config.DefaultsLanguage;

            //if(service.IsLeaf)
            if (currentService != null)
            {
                service.ViewRoleId   = currentService.ViewRoleId;
                service.ModifyRoleId = currentService.ModifyRoleId;
                service.AccessRoleId = currentService.AccessRoleId;
            }
            AuthorizeManager.SetAndCheckModifyAndAccessRole(service, serviceDto);

            service.Status      = serviceDto.Status;
            service.EditMode    = serviceDto.EditMode;
            service.EnableCache = serviceDto.EnableCache;
            try
            {
                service.SlidingExpirationTimeInMinutes = serviceDto.SlidingExpirationTimeInMinutes;
            }
            catch (Exception)
            {
                service.SlidingExpirationTimeInMinutes = 0;
            }
            await _contentManagementContext.SaveChangesAsync();

            string jsCode = serviceDto.JsCode;

            if (!string.IsNullOrEmpty(jsCode))
            {
                await _sourceControl.AddChange(Config.ServicesSourceCodePath, service.Guid + ".js", jsCode, service.Version,
                                               comment);

                if (checkIn)
                {
                    await WriteFileAsync(Config.ServicesSourceCodePath, service.Guid, ".js", jsCode);
                }
            }


            CacheManager.ClearAllItemContainKey(service.PathOrUrl);
            UpdateServiceSettingOfWebConfig(service);
            return(service);
        }
Esempio n. 7
0
        //public async Task<IList<LinkObjective>> GetAllLinkAsync(IEnumerable<string> accessRoles = null, IEnumerable<string> denyRoles = null)
        //{
        //    //var ts = _securityContext.ApplicationTrees
        //    // .Include(x => x.Offspring.Select(y => y.Offspring))
        //    // .SingleOrDefault(x => x.Id == 72);
        //    //ts = ts;
        //    var lang = Setting.Language;
        //    var baseTree = await _contentManagementContext.Links.Where(ln => ln.Language == lang).AsNoTracking().ToListAsync();
        //    var newTree = new List<LinkObjective>();
        //    foreach (var node in baseTree)
        //    {
        //        if (node.IsLeaf)
        //        {
        //            if (accessRoles == null && denyRoles == null)
        //                BuildTree<LinkObjective, LinkObjective>(node, newTree, baseTree);
        //            else if (accessRoles != null && denyRoles != null)
        //            {
        //                if (accessRoles.Any(r => r == node.ViewRoleId.ToString()) && !denyRoles.Any(r => r == node.ViewRoleId.ToString()))
        //                {
        //                    BuildTree<LinkObjective, LinkObjective>(node, newTree, baseTree);
        //                }
        //            }
        //            else if (denyRoles == null)
        //            {
        //                if (accessRoles.Any(r => r == node.ViewRoleId.ToString()))
        //                {
        //                    BuildTree<LinkObjective, LinkObjective>(node, newTree, baseTree);
        //                }
        //            }
        //            else if (accessRoles == null)
        //            {
        //                if (!denyRoles.Any(r => r == node.ViewRoleId.ToString()))
        //                {
        //                    BuildTree<LinkObjective, LinkObjective>(node, newTree, baseTree);
        //                }
        //            }

        //        }


        //        if (node.ParentId == null)
        //        {
        //            if (node.IsLeaf)
        //            {

        //                if (accessRoles == null && denyRoles == null)
        //                {
        //                    if (!newTree.Exists(t => t.Id == node.Id))
        //                        newTree.Add(node);
        //                }
        //                else if (accessRoles != null && denyRoles != null)
        //                {
        //                    if (accessRoles.Any(r => r == node.ViewRoleId.ToString()) && !denyRoles.Any(r => r == node.ViewRoleId.ToString()))
        //                    {
        //                        if (!newTree.Exists(t => t.Id == node.Id))
        //                            newTree.Add(node);
        //                    }
        //                }
        //                else if (denyRoles == null)
        //                {
        //                    if (accessRoles.Any(r => r == node.ViewRoleId.ToString()))
        //                    {
        //                        if (!newTree.Exists(t => t.Id == node.Id))
        //                            newTree.Add(node);
        //                    }
        //                }
        //                else if (accessRoles == null)
        //                {
        //                    if (!denyRoles.Any(r => r == node.ViewRoleId.ToString()))
        //                    {
        //                        if (!newTree.Exists(t => t.Id == node.Id))
        //                            newTree.Add(node);
        //                    }
        //                }
        //            }
        //            else
        //            {
        //                if (!newTree.Exists(t => t.Id == node.Id))
        //                    newTree.Add(node);
        //            }
        //        }
        //    }
        //    return RemoveEmptyParentNode<LinkObjective, LinkObjective>(newTree);
        //}

        #region [SaveLink..]
        public async Task <Link> Save(JObject data)
        {
            dynamic linkDto = data;

            string oldUrl = "";

            int?linkId = linkDto.Id;


            var link = new Link()
            {
                Id = linkId == null ? 0 : linkDto.Id
            };
            var currentLink = await _contentManagementContext.Links.AsNoTracking().SingleOrDefaultAsync(ln => ln.Id == link.Id);

            try
            {
                if (currentLink == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.LinkNotFound));
                }
                oldUrl          = currentLink.Url;
                link            = currentLink;
                link.RowVersion = linkDto.RowVersion;
                _contentManagementContext.Links.Attach(link);
            }
            catch (Exception)
            {
                link = new Link();
                _contentManagementContext.Links.Add(link);
            }


            string linkUrl = linkDto.Url;

            if (linkUrl.IndexOf(Helper.RootUrl, StringComparison.Ordinal) != 0)
            {
                linkUrl = Helper.RootUrl + linkUrl;
            }
            if (linkUrl.LastIndexOf(Helper.RootUrl, StringComparison.Ordinal) == linkUrl.Length - 1)
            {
                linkUrl = linkUrl.Substring(0, linkUrl.Length - 1);
            }
            try
            {
                int parentId = linkDto.ParentId;
                if (currentLink?.ParentId != parentId)
                {
                    var parentCode = await _contentManagementContext.Links.SingleOrDefaultAsync(md => md.Id == parentId);

                    if (parentCode == null)
                    {
                        throw new KhodkarInvalidException(
                                  LanguageManager.ToAsErrorMessage(ExceptionKey.ParentRecordNotFound));
                    }
                    AuthorizeManager.CheckParentNodeModifyAccessForAddingChildNode(parentCode, parentCode.Id);
                }
                link.ParentId = parentId;
            }
            catch (KhodkarInvalidException)
            {
                throw;
            }
            catch (Exception)
            {
                link.ParentId = null;
            }
            link.Text               = linkDto.Text;
            link.TransactionCode    = linkDto.TransactionCode;
            link.Html               = linkDto.Html;
            link.Action             = linkDto.Action;
            link.Url                = linkUrl;
            link.ShowToSearchEngine = linkDto.ShowToSearchEngine;

            var repeatedLink = await _contentManagementContext.Links.Where(sr => sr.Url == link.Url).CountAsync();

            if ((repeatedLink > 0 && oldUrl == "") || (repeatedLink > 1 && oldUrl == ""))
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedValue, link.Url));
            }


            try
            {
                link.Order = linkDto.Order;
            }
            catch (Exception)
            {
                link.Order = 1;
            }
            link.IsLeaf   = linkDto.IsLeaf;
            link.Language = linkDto.Language;

            //if(service.IsLeaf)

            if (currentLink != null)
            {
                link.ViewRoleId   = currentLink.ViewRoleId;
                link.ModifyRoleId = currentLink.ModifyRoleId;
                link.AccessRoleId = currentLink.AccessRoleId;
            }

            AuthorizeManager.SetAndCheckModifyAndAccessRole(link, linkDto);

            link.Status   = linkDto.Status;
            link.IsMobile = linkDto.IsMobile;
            link.TypeId   = linkDto.TypeId;

            await _contentManagementContext.SaveChangesAsync();

            if (oldUrl != "")
            {
                _contentManagementContext.WebPages.Where(wp => wp.Url == oldUrl).Update(wp => new WebPage()
                {
                    Url = linkUrl
                });
            }
            return(link);
        }