public async Task <ReplyBase> RemoveUserFromRole(string userId, int roleId) { using (var authContext = GetContext()) { var existingRole = authContext.UserRoles.FirstOrDefault(c => c.RoleId == roleId && c.UserId == userId); if (existingRole == null) { return(ReplyBase.NotFound($"Unable to find user role for user '{userId}' with id {roleId}")); } authContext.UserRoles.Remove(existingRole); authContext.SaveChanges(); } return(ReplyBase.Success()); }
public async Task <ReplyBase> RemoveAuthorizationNamespace(int id) { using (var authContext = GetContext()) { var existingNamespace = authContext.AuthorizationNamespaces.Find(id); if (existingNamespace == null) { return(ReplyBase.NotFound($"Unable to find authorization namespace with id {id}")); } authContext.SaveChanges(); return(IdReply.Success()); } }
public async Task <ReplyBase> RemoveRole(int id) { using (var authContext = GetContext()) { var existingRole = authContext.Roles.Find(id); if (existingRole == null) { return(ReplyBase.NotFound($"Unable to find role with id {id}")); } authContext.Roles.Remove(existingRole); authContext.SaveChanges(); } return(ReplyBase.Success()); }
public async Task <ReplyBase> UpdateAuthorizationNamespace(AuthorizationNamespaceDto authorizationNamespaceDto) { using (var authContext = GetContext()) { var existingNamespace = authContext.AuthorizationNamespaces.Find(authorizationNamespaceDto.Id); if (existingNamespace == null) { return(ReplyBase.NotFound($"Unable to find authorization namespace with id {authorizationNamespaceDto.Id}")); } authContext.SaveChanges(); return(ReplyBase.Success()); } }
public async Task <ReplyBase> RemoveResourceDetail(int id) { using (var authContext = GetContext()) { var existingResourceDetail = authContext.ResourceDetails.Find(id); if (existingResourceDetail == null) { return(ReplyBase.NotFound($"Unable to find resource detail with ID {id}")); } authContext.SaveChanges(); return(ReplyBase.Success()); } }
public async Task <ReplyBase> RemoveResource(int id) { using (var authContext = GetContext()) { var existingResource = authContext.Resources.Find(id); if (existingResource == null) { return(ReplyBase.NotFound($"Resource with id {id} not found")); } authContext.Resources.Remove(existingResource); authContext.SaveChanges(); return(ReplyBase.Success()); } }
public async Task <ReplyBase> RemovePermission(int id) { using (var authContext = GetContext()) { var existingPermission = authContext.Permissions.Find(id); if (existingPermission == null) { return(ReplyBase.NotFound($"Unable to find permission with id {id}")); } authContext.SaveChanges(); return(ReplyBase.Success()); } }
public async Task <ReplyBase> UpdateRole(RoleDto roleDto) { using (var authContext = GetContext()) { var existingRole = authContext.Roles .Include(c => c.RolePermissions) .FirstOrDefault(c => c.Id == roleDto.Id); if (existingRole == null) { return(ReplyBase.NotFound($"Unable to find role with id {roleDto.Id}")); } var existingPermissions = authContext.Permissions.Where(c => c.RoleId == roleDto.Id).ToList(); var permissionsToDelete = existingPermissions .Where(c => !roleDto.RolePermissions.Select(d => d.Id).Contains(c.Id)).ToList(); authContext.Permissions.RemoveRange(permissionsToDelete); foreach (var roleDtoRolePermission in roleDto.RolePermissions) { if (roleDtoRolePermission.Id == 0) { existingRole.RolePermissions.Add(new Permission { OwnerScoped = roleDtoRolePermission.OwnerScoped, PermissionType = roleDtoRolePermission.PermissionType, ResourceDetailId = roleDtoRolePermission.ResourceDetailId, RoleId = roleDtoRolePermission.RoleId }); } else { var existingPerm = existingRole.RolePermissions.First(c => c.Id == roleDtoRolePermission.Id); existingPerm.PermissionType = roleDtoRolePermission.PermissionType; existingPerm.OwnerScoped = roleDtoRolePermission.OwnerScoped; existingPerm.ResourceDetailId = roleDtoRolePermission.ResourceDetailId; } } existingRole.Description = roleDto.Description; existingRole.NamespaceId = roleDto.NamespaceId; existingRole.ParentRoleId = roleDto.ParentRoleId; authContext.SaveChanges(); }; return(ReplyBase.Success()); }
public async Task <ReplyBase> UpdateResource(ResourceDto resourceDto) { using (var authContext = GetContext()) { var existingResource = authContext.Resources.Find(resourceDto.Id); if (existingResource == null) { return(ReplyBase.NotFound($"Resource with id {resourceDto.Id} not found")); } existingResource.Description = resourceDto.Description; existingResource.Key = resourceDto.Key; var details = authContext.ResourceDetails.Where(c => c.ResourceId == existingResource.Id).ToList(); var resourceDetailsToDelete = details.Where(d => !resourceDto.ResourceDetails.Select(c => c.Id).Contains(d.Id)).ToList(); authContext.ResourceDetails.RemoveRange(resourceDetailsToDelete); foreach (var resourceDtoResourceDetail in resourceDto.ResourceDetails) { if (resourceDtoResourceDetail.Id != 0) { var existingResourceDetail = authContext.ResourceDetails.Find(resourceDtoResourceDetail.Id); existingResourceDetail.Description = resourceDtoResourceDetail.Description; existingResourceDetail.Key = resourceDtoResourceDetail.Key; } else { resourceDtoResourceDetail.ResourceId = resourceDto.Id; authContext.ResourceDetails.Add(new ResourceDetail { Description = resourceDtoResourceDetail.Description, ResourceId = resourceDto.Id, Key = resourceDtoResourceDetail.Key }); } } authContext.SaveChanges(); return(ReplyBase.Success()); } }
public async Task <ReplyBase> UpdateResourceDetail(ResourceDetailDto resourceDetailDto) { using (var authContext = GetContext()) { var existingResourceDetail = authContext.ResourceDetails.Find(resourceDetailDto.Id); if (existingResourceDetail == null) { return(ReplyBase.NotFound($"Unable to find resource with ID {resourceDetailDto.Id}")); } existingResourceDetail.Description = resourceDetailDto.Description; existingResourceDetail.Key = resourceDetailDto.Key; existingResourceDetail.ResourceId = resourceDetailDto.ResourceId; authContext.SaveChanges(); return(ReplyBase.Success()); } }
public async Task <ReplyBase> UpdatePermission(PermissionDto permissionDto) { using (var authContext = GetContext()) { var existingPermission = authContext.Permissions.Find(permissionDto.Id); if (existingPermission == null) { return(ReplyBase.NotFound($"Unable to find permission with id {permissionDto.Id}")); } existingPermission.OwnerScoped = permissionDto.OwnerScoped; existingPermission.PermissionType = permissionDto.PermissionType; existingPermission.RoleId = permissionDto.RoleId; existingPermission.ResourceDetailId = permissionDto.ResourceDetailId; authContext.SaveChanges(); return(ReplyBase.Success()); } }