public RoleViewModel RoleGet(long? id) { if (id == null) id = 0; try { RoleViewModel model = new RoleViewModel(); if (id > 0) { model = SecurityProvider.GetRoles().Where(a => a.Id == id).Select(a => new RoleViewModel() { Id = a.Id, RoleName = a.RoleName, Description = a.Description, StatusType = a.Status, }).Single(); } model.Permissions = new List<PermissionViewModel>(); foreach (PrivilegeType privType in Enum.GetValues(typeof(PrivilegeType))) { model.Permissions.Add(new PermissionViewModel() { Privilege = privType, Selected = false }); } if (id > 0) { var privs = SecurityProvider.GetRoles().Where(a => a.Id == id).Single().Privileges; //Gets all the privlege assigned to the role found using the ID foreach (var p in privs) { var priv = model.Permissions.Where(a => a.Privilege == p.Security).SingleOrDefault(); //Gets the single privlege assigned to the role if (priv != null) { priv.Selected = true; } else { model.Permissions.Add(new PermissionViewModel() { Privilege = p.Security, Selected = true }); } } } return model; } catch (SecurityException e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message)); } }
public RoleViewModel SaveRole(RoleViewModel model) { try { if (!ModelState.IsValid) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error details")); } var role1 = SecurityProvider.SaveRole(model.Id, model.RoleName, model.Description, model.StatusType, model.Permissions.Select(a => a.Privilege).ToList()); var role = new RoleViewModel() { Id = role1.Id, RoleName = role1.RoleName, Description = role1.Description, Permissions = role1.Privileges.Select(a => new PermissionViewModel() { Privilege = a.Security }).ToList() }; return role; } catch (SecurityException e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message)); } }