public static async Task <IStatusGeneric <RoleToPermissions> > CreateRoleWithPermissionsAsync( string roleName, string description, IEnumerable <Permission> permissionInRole, IAuthorizationRepository repository) { if (roleName == null) { throw new ArgumentNullException(nameof(roleName)); } if (repository == null) { throw new ArgumentNullException(nameof(repository)); } var status = new StatusGenericHandler <RoleToPermissions>(); RoleToPermissions roleToPermissions = await repository.GetRoleToPermissionAsync(roleName); if (roleToPermissions != null) { status.AddError("That role already exists"); return(status); } return(status.SetResult(new RoleToPermissions(roleName, description, permissionInRole))); }
public async Task <IStatusGeneric> DeleteRoleAsync( string roleName, bool isRemoveFromUsers, IAuthorizationRepository repository) { if (repository == null) { throw new ArgumentNullException(nameof(repository)); } var status = new StatusGenericHandler { Message = "Deleted role successfully." }; RoleToPermissions roleToUpdate = await repository.GetRoleToPermissionAsync(roleName); if (roleToUpdate == null) { return(status.AddError("That role doesn't exists")); } ICollection <UserToRole> usersWithRoles = await repository.GetUsersToRoleByNameAsync(roleName); if (usersWithRoles.Any()) { if (!isRemoveFromUsers) { return(status.AddError($"That role is used by {usersWithRoles.Count} and you didn't ask for them to be updated.")); } await repository.DeleteAsync(usersWithRoles); status.Message = $"Removed role from {usersWithRoles.Count} user and then deleted role successfully."; } await repository.DeleteAsync(roleToUpdate); return(status); }
public static async Task <IStatusGeneric <UserToRole> > AddRoleToUserAsync( string userId, string roleName, IAuthorizationRepository repository) { if (userId == null) { throw new ArgumentNullException(nameof(userId)); } if (roleName == null) { throw new ArgumentNullException(nameof(roleName)); } if (repository == null) { throw new ArgumentNullException(nameof(repository)); } var status = new StatusGenericHandler <UserToRole>(); UserToRole userToRole = await repository.GetUserToRoleAsync(userId, roleName); if (userToRole != null) { status.AddError($"The user already has the Role '{roleName}'."); return(status); } RoleToPermissions roleToAdd = await repository.GetRoleToPermissionAsync(roleName); if (roleToAdd == null) { status.AddError($"Could not find the Role '{roleName}'."); return(status); } return(status.SetResult(new UserToRole(userId, roleToAdd))); }
} //needed by EF Core public UserToRole(string userId, RoleToPermissions role) { UserId = userId; Role = role; }