Example #1
0
        public UserMenuResponse SaveRoleMenuRight(string roleId, string menuIds, AspNetRole role)
        {
            List<AspNetRole> Roles = menuRepository.Roles().OrderBy(dbRole => dbRole.Name).ToList();
            List<Menu> menues = menuRepository.GetAll().ToList();
            IList<string> postedMenuIdstrings = menuIds.Split(new[] { ',' });
            IList<int> postedMenuIds = new List<int>();
            if (postedMenuIdstrings.Count > 0 && !string.IsNullOrEmpty(postedMenuIdstrings[0]))
                postedMenuIds = postedMenuIdstrings.Select(int.Parse).ToList();
            List<MenuRight> userMenuRights = menuRightRepository.GetMenuByRole(roleId).ToList();

            foreach (int menuItem in postedMenuIds)
            {
                if (userMenuRights.All(right => right.Menu.MenuId != menuItem))
                {
                    MenuRight toBeAddedMenu = new MenuRight { Menu = menues.FirstOrDefault(dbMenu => dbMenu.MenuId == menuItem), AspNetRole = Roles.FirstOrDefault(dbRole => dbRole.Id == roleId) };
                    menuRightRepository.Add(toBeAddedMenu);
                }
            }

            IEnumerable<MenuRight> deleted = userMenuRights.Where(menu => !postedMenuIds.Contains(menu.Menu.MenuId));
            deleted.ToList().ForEach(menu => menuRightRepository.Delete(menu));
            menuRightRepository.SaveChanges();
            return new UserMenuResponse
            {
                MenuRights = FindMenuItemsByRoleId(roleId).ToList(),
                Menus = menuRepository.GetAll().ToList(),
                Roles = Roles
            };
        }
 public async Task<ActionResult> Create(RoleViewModel roleViewModel)
 {
     if (ModelState.IsValid)
     {
         var role = new AspNetRole();
         role.Name = roleViewModel.Name;
         int rolesCount = RoleManager.Roles.Count() + 1;
         role.Id = rolesCount.ToString();
         if (!RoleManager.RoleExists(role.Name))
         {
             var roleresult = await RoleManager.CreateAsync(role);
             if (!roleresult.Succeeded)
             {
                 TempData["message"] = new MessageViewModel
                 {
                     Message = "Error in creating role",
                     IsError = true
                 };
                 return View();
             }
             TempData["message"] = new MessageViewModel
             {
                 Message = "Role has been created successfully",
                 IsSaved = true
             };
         }
         return RedirectToAction("Index");
     }
     return View();
 }