示例#1
0
        /// <summary>
        /// Save Menu Rights
        /// </summary>
        public UserMenuResponse SaveRoleMenuRight(string roleId, string menuIds, UserRole role)
        {
            List<UserRole> 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
            };
        }
示例#2
0
 public async Task<ActionResult> Create(RoleViewModel roleViewModel)
 {
     if (ModelState.IsValid)
     {
         var role = new UserRole { Name = roleViewModel.Name };
         var roleresult = await RoleManager.CreateAsync(role);
         if (!roleresult.Succeeded)
         {
             ModelState.AddModelError("", roleresult.Errors.First());
             return View();
         }
         return RedirectToAction("Index");
     }
     return View();
 }