Exemplo n.º 1
0
        public async Task <IdentityResult> DeleteGroupAsync(string groupId)
        {
            var group = await this.FindByIdAsync(groupId);

            if (group == null)
            {
                throw new ArgumentNullException("User");
            }

            var currentGroupMembers = (await this.GetGroupUsersAsync(groupId)).ToList();

            // remove the roles from the group:
            group.ApplicationRoles.Clear();

            // Remove all the users:
            group.ApplicationUsers.Clear();

            // Remove the group itself:
            _db.ApplicationGroups.Remove(group);

            await _db.SaveChangesAsync();

            // Reset all the user roles:
            foreach (var user in currentGroupMembers)
            {
                await this.RefreshUserGroupRolesAsync(user.Id);
            }
            return(IdentityResult.Success);
        }
        public async Task <IdentityResult> SetPageParentsChildernAsync(
            long pageId,
            long[] childern)
        {
            var thisPage = await this.GetPageWithRelationsAsync(pageId);

            thisPage.ChildernPages.Clear();
            await _db.SaveChangesAsync();

            if (childern != null && childern.Any())
            {
                thisPage.ChildernPages = await _db.ApplicationPages.Where(p => childern.Contains(p.ApplicationPageId)).ToListAsync();
            }

            await _db.SaveChangesAsync();

            return(IdentityResult.Success);
        }
        /// <summary>
        /// @TODo make sure that menu items or pages not repeated also make sure that if user changed the name of exisiting page (NamesUpdated = true) to exlude from insertion
        /// </summary>
        /// <param name="pages"></param>
        /// <returns></returns>
        public async Task AddMenus(List <ApplicationPage> pages)
        {
            try
            {
                using (var context = new LaborServicesDbContext())
                {
                    var allPages = context.ApplicationPages.ToList();


                    var pageList = (from p in allPages
                                    select new ApplicationPage()
                    {
                        ApplicationPageId = p.ApplicationPageId,
                        NameAr = p.NameAr,
                        NameEn = p.NameEn,
                        Controller = p.Controller,
                        Action = p.Action,
                        Area = p.Area,
                        IsBaseParent = p.IsBaseParent,
                        NamesUpdated = p.NamesUpdated
                    }).ToList();

                    var deletedIds = pageList.Where(o => string.IsNullOrEmpty(o.Controller) == false && !pages.Any(n => n.Controller == o.Controller && n.Action == o.Action && n.Area == o.Area)).Select(x => x.ApplicationPageId).ToList();
                    var newlyAdded = pages.Where(n => !pageList.Any(o => n.Controller == o.Controller && n.Action == o.Action && n.Area == o.Area)).ToList();

                    var newItems = pageList.Any() == false
                        ? pages
                        : newlyAdded;



                    if (deletedIds.Any())
                    {
                        await PageManager.DeletePagesAsync(deletedIds);
                    }

                    if (newItems.Any() == false)
                    {
                        return;
                    }

                    foreach (var page in newItems)
                    {
                        context.ApplicationPages.Add(page);
                    }


                    var pagesAdded = await context.SaveChangesAsync();

                    if (pagesAdded <= 0)
                    {
                        return;
                    }
                    var defaultRole = await context.Roles
                                      .FirstOrDefaultAsync(r => r.Name == AppConstants.AdminRoleName);

                    await PageManager.SetRolePagesAsync(defaultRole.Id, newItems.Select(x => x.ApplicationPageId).ToArray());
                }
            }
            catch (Exception ex)
            {
            }
        }