public async Task <bool> UserHasRole(User user, string role) { RolesForUser userRoles = null; userRoles = await GetUserRoles(user); bool userHasRole = userRoles.Roles.Any(x => x.Equals(role)); return(userHasRole); }
public async Task <RolesForUser> GetUserRoles(User user) { RolesForUser userRoles = null; userRoles = await TryCatch <ArgumentNullException, RolesForUser>(async() => { RolesForUser roles = new RolesForUser() { Roles = await _userManager.GetRolesAsync(user), }; return(roles); }); return(userRoles); }
//GET/api/auth/getuserroles public async Task <IActionResult> GetUserRoles([FromQuery] string userId) { //HERE -> CustomException skapar inte en user klass när user är null if (String.IsNullOrEmpty(userId)) { return(new JsonResult(await Errors .GetGenericErrorResponse( new GetUserRolesResponse() { Email = "no_email", Roles = new List <string>(), UserId = userId, StatusCode = 400, Error = "UserId can not be empty", Description = "UserId cannot be empty.", Code = "userId_can_not_be_empty" }))); } User user = await _accountsService.GetUser(userId); if (user == null) { return(new JsonResult(await Errors .GetGenericErrorResponse( new GetUserRolesResponse() { Email = "no_email", Roles = new List <string>(), UserId = userId, StatusCode = 404, Error = "User is not found", Description = "User can not be found.", Code = "user_is_not_found" }))); } RolesForUser userRoles = await _accountsService.GetUserRoles(user); return(new JsonResult( Wrappyfier .WrapGetUserRoles( user.Id, user.Email, userRoles.Roles.ToList(), 200))); }
//Delete /api/auth/deleteuser public async Task <IActionResult> DeleteUser([FromBody] DeleteUserViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _accountsService.GetUser(model.Id); if (user == null) { return(new JsonResult(await Errors .GetGenericErrorResponse( new DeleteUserResponse() { Id = model.Id, Email = "no_email", StatusCode = 401, Error = "User not found", Description = String.Format(Constants.APIMessages.NotFoundMessage, model.Id), Code = "user_not_found", }))); } RolesForUser rolesForUser = await _accountsService.GetUserRoles(user); if (rolesForUser.IsNull) { return(new JsonResult(await Errors .GetGenericErrorResponse( new DeleteUserResponse() { Id = "no_id", Email = "no_email", StatusCode = 422, Error = "Get roles for user error", Description = "Unable to get roles for user.", Code = "get_roles_for_user_error", }))); } if (rolesForUser.Roles.Any()) { IdentityResult removeRolesFromUserResult = await _accountsService.RemoveRolesFromUser(user, rolesForUser.Roles); if (!removeRolesFromUserResult.Succeeded) { return(new JsonResult(await Errors .GetGenericErrorResponse( new DeleteUserResponse() { Id = user.Id, Email = user.Email, StatusCode = 422, Error = "Unable to complete delete opretaion of user related roles.", Description = "Roles realted to the current user could not be removed at this time.", Code = "unable_to_complete_delete_operation_of_user_related_roles", }))); } } IdentityResult removeUserResult = await _accountsService.DeleteUser(user); if (!removeUserResult.Succeeded) { IdentityResult reAddRolesToUserResult = await _accountsService.AddRolesToUser(user, rolesForUser.Roles); if (reAddRolesToUserResult.Succeeded) { return(new JsonResult(await Errors.GetGenericErrorResponse( new DeleteUserResponse() { Id = user.Id, Email = user.Email, Code = "unable_to_complete_delete_operation", StatusCode = 422, Description = "User was not deleted. The delete task could not be completed at this time.", Error = "Unable to complete delete opretaion." }))); } return(new JsonResult(await Errors.GetGenericErrorResponse( new DeleteUserResponse() { Id = user.Id, Email = user.Email, Code = "unable_to_complete_delete_operation_USER_DOES_NOT_HAVE_ANY_ROLES", StatusCode = 422, Description = "User was not deleted. The delete task could not be completed at this time. User has no roles assign. Pleas add roles to user for access.", Error = "Unable to complete delete opretaion. User does not have any roles" }))); } await _context.SaveChangesAsync(); return(new OkObjectResult(Wrappyfier.WrapDeleteUserResponse(user.Id, user.Email))); }