private ApplicationRole CreateOrUpdateRole(string name, string description, IEnumerable <ApplicationPermission> perms, Repository repo) { if (_applicationRoleDictionary == null) { _applicationRoleDictionary = new ConcurrentDictionary <string, ApplicationRole>(); var applicationRoles = repo.Get <ApplicationRole>(a => a.Name != null); foreach (ApplicationRole item in applicationRoles) { if (!_applicationRoleDictionary.ContainsKey(item.Name)) { _applicationRoleDictionary.TryAdd(item.Name, item); } } } ApplicationRole role = null; bool update = false; if (_applicationRoleDictionary.ContainsKey(name)) { role = _applicationRoleDictionary[name]; if (role.Description != description) { role.Description = description; update = true; } if (role.IsCustom != false) { role.IsCustom = false; update = true; } List <ApplicationPermission> applicationPermissions = role.Permissions.Where(p => !p.IsCustom).ToList(); HandleApplicationPermissions <ApplicationRole>(role, applicationPermissions, perms, ref update); } else { update = true; role = new ApplicationRole(); role.Name = name; role.Description = description; role.IsCustom = false; if (perms != null) { foreach (var p in perms) { role.AddPermissions(p); } } } if (update) { repo.Save(role); } return(role); }
public ActionResult <ApplicationRoleDTO> PostRole(ApplicationRoleDTO applicationRoleDTO) { var manager = ServiceLocator.Current.GetInstance <IMiniSessionService>(); var repo = new Repository(manager); var applicationRole = new ApplicationRole { Description = applicationRoleDTO.Description, Name = applicationRoleDTO.Name, IsCustom = applicationRoleDTO.IsCustom }; foreach (var permissionDTO in applicationRoleDTO.Permissions) { var applicationPermission = repo.GetById <ApplicationPermission>(permissionDTO.Id); applicationRole.AddPermissions(applicationPermission); } repo.Save <ApplicationRole>(applicationRole); manager.Session.Flush(); return(CreatedAtAction("PostRole", new { id = applicationRole.Id }, applicationRole)); }