Beispiel #1
0
 public ActionResult Create(Role role, string[] permissions)
 {
     var Plist = PermissionRepository.FindAll().ToList();
     if (ModelState.IsValid)
     {
         if (RoleRepository.Find(Specification<Role>.Eval(e => e.RoleName == role.RoleName)) == null)
         {
             RoleRepository.Add(role);
             RoleRepository.Context.Commit();
             if (permissions != null)
             {
                 foreach (var permissionId in permissions)
                 {
                     PermissionsInRoles pir = new PermissionsInRoles();
                     pir.RoleId = role.RoleId;
                     pir.PermissionId = int.Parse(permissionId);
                     PermissionsInRolesRepository.Add(pir);
                     PermissionsInRolesRepository.Context.Commit();
                 }
             }
             return RedirectToAction("Index");
         }
         else
         {
             ViewData["PermissionSelectList"] = Plist;
             TempData["ErrorMessage"] = "角色名称已存在";
         }
     }
     ViewData["PermissionSelectList"] = Plist;
     return View(role);
 }
Beispiel #2
0
 public ActionResult Edit(Role role, string[] permissions)
 {
     if (ModelState.IsValid)
     {
         RoleRepository.Update(role);
         RoleRepository.Context.Commit();
         if (permissions != null)
         {
             var Sqlpir = PermissionsInRolesRepository.FindAll(Specification<PermissionsInRoles>.Eval(e => e.RoleId == role.RoleId));
             //数据库PermissionsInRoles中原有权限ID
             List<int> idlist = new List<int>();
             foreach (var item in Sqlpir)
             {
                 idlist.Add(item.PermissionId);
             }
             //数据库原有权限和将要提交的权限比较 若是数据库不包含将要提交的权限,那么就将此权限添加
             foreach (var permissionId in permissions)
             {
                 if (!idlist.Contains(int.Parse(permissionId)))
                 {
                     PermissionsInRoles pir = new PermissionsInRoles();
                     pir.RoleId = role.RoleId;
                     pir.PermissionId = int.Parse(permissionId);
                     PermissionsInRolesRepository.Add(pir);
                     PermissionsInRolesRepository.Context.Commit();
                 }
             }
             //
             foreach (var item in idlist)
             {
                 if (!permissions.ToList().Contains(item.ToString()))
                 {
                     PermissionsInRolesRepository.Remove(PermissionsInRolesRepository.Find(Specification<PermissionsInRoles>.Eval(e => e.PermissionId == item && e.RoleId == role.RoleId)));
                     PermissionsInRolesRepository.Context.Commit();
                 }
             }
         }
         return RedirectToAction("Index");
     }
     var Plist = PermissionRepository.FindAll().ToList();
     ViewData["PermissionSelectList"] = Plist;
     return View(role);
 }