public async Task <IHttpActionResult> DeleteUser(string id) { _logger.Debug(string.Format("Begin. Id: [{0}]", id)); var ndUser = await NdUserManager.FindByIdAsync(id); if (ndUser != null) { IdentityResult result = await NdUserManager.DeleteAsync(ndUser); if (!result.Succeeded) { _logger.Error(string.Format( "Delete user failed. Id: [{0}], Reason: [{1}]", id, string.Join(Environment.NewLine, result.Errors))); return(GetErrorResult(result)); } _logger.Debug(string.Format("User deleted successfully. Id: [{0}]", id)); return(Ok()); } _logger.Debug(string.Format("User was not found [id: {0}]", id)); return(NotFound()); }
public async Task <IHttpActionResult> ManageUsersInRole(UsersInRoleDto usersInRoleDto) { var role = await NdRoleManager.FindByIdAsync(usersInRoleDto.Id); if (role == null) { ModelState.AddModelError("", "Role does not exist"); return(BadRequest(ModelState)); } foreach (string user in usersInRoleDto.EnrolledUsers) { var ndUser = await NdUserManager.FindByIdAsync(user); if (ndUser == null) { ModelState.AddModelError("", string.Format("User: {0} does not exists", user)); continue; } if (!NdUserManager.IsInRole(user, role.Name)) { IdentityResult result = await NdUserManager.AddToRoleAsync(user, role.Name); if (!result.Succeeded) { ModelState.AddModelError("", string.Format("User: {0} could not be added to role", user)); } } } foreach (string user in usersInRoleDto.RemovedUsers) { var ndUser = await NdUserManager.FindByIdAsync(user); if (ndUser == null) { ModelState.AddModelError("", string.Format("User: {0} does not exists", user)); continue; } IdentityResult result = await NdUserManager.RemoveFromRoleAsync(user, role.Name); if (!result.Succeeded) { ModelState.AddModelError("", string.Format("User: {0} could not be removed from role", user)); } } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } return(Ok()); }
public async Task <IHttpActionResult> GetUser(string id) { _logger.Debug(string.Format("Begin. Id: [{0}]", id)); var user = await NdUserManager.FindByIdAsync(id); if (user != null) { _logger.Debug(string.Format("User found. Id: [{0}]", id)); return(Ok(_factory.Create(user))); } _logger.Debug(string.Format("User was not found. Id: [{0}]", id)); return(NotFound()); }
public async Task <IHttpActionResult> AssignRolesToUser([FromUri] string id, [FromBody] string[] rolesToAssign) { _logger.Debug(string.Format("Begin. Id: [{0}], Roles: [{1}]", id, string.Join(", ", rolesToAssign))); var ndUser = await NdUserManager.FindByIdAsync(id); if (ndUser == null) { _logger.Debug(string.Format("User was not found. Id: [{0}]", id)); return(NotFound()); } var currentRoles = await NdUserManager.GetRolesAsync(ndUser.Id); var rolesNotExists = rolesToAssign.Except(NdRoleManager.Roles.Select(x => x.Name)).ToArray(); if (rolesNotExists.Count() > 0) { ModelState.AddModelError("", string.Format("Roles '{0}' does not exixts in the system", string.Join(",", rolesNotExists))); _logger.Error(string.Format( "Model state is not valid. ModelState: [{0}]", string.Join(Environment.NewLine, ModelState.Select(x => string.Format("{0}: {1}", x.Key, x.Value))))); return(BadRequest(ModelState)); } IdentityResult removeResult = await NdUserManager.RemoveFromRolesAsync(ndUser.Id, currentRoles.ToArray()); if (!removeResult.Succeeded) { ModelState.AddModelError("", "Failed to remove user roles"); _logger.Error(string.Format( "Model state is not valid. ModelState: [{0}]", string.Join(Environment.NewLine, ModelState.Select(x => string.Format("{0}: {1}", x.Key, x.Value))))); return(BadRequest(ModelState)); } IdentityResult addResult = await NdUserManager.AddToRolesAsync(ndUser.Id, rolesToAssign); if (!addResult.Succeeded) { ModelState.AddModelError("", "Failed to add user roles"); _logger.Error(string.Format( "Model state is not valid [ModelState: {0}]", string.Join(Environment.NewLine, ModelState.Select(x => string.Format("{0}: {1}", x.Key, x.Value))))); return(BadRequest(ModelState)); } _logger.Debug(string.Format("User assigned to roles successfully. Id: [{0}], Roles: [{1}]", id, string.Join(", ", rolesToAssign))); return(Ok()); }
public async Task <IHttpActionResult> GetTherapistInfo(string id) { _logger.Debug(string.Format("Begin. Id: [{0}]", id)); var user = await NdUserManager.FindByIdAsync(id); var therapistRole = await NdRoleManager.FindByNameAsync(Role.Therapist.ToString()); if (user != null && user.Roles.Any(x => x.RoleId == therapistRole.Id)) { _logger.Debug(string.Format("User found. Id: [{0}]", id)); return(Ok(_factory.CreateUserInfo(user))); } _logger.Debug(string.Format("Therapist was not found [id: {0}]", id)); return(NotFound()); }
public async Task <IHttpActionResult> UpdateMyInfo(UserInfoDto myInfoDto) { _logger.Debug(string.Format("Begin. Clinic: [{0}], Email: [{1}], FirstName: [{2}], Gender: [{3}], LastName: [{4}], PhoneNumber: [{5}], Title: [{6}]", myInfoDto.Clinic, myInfoDto.Email, myInfoDto.FirstName, myInfoDto.Gender.HasValue ? myInfoDto.Gender.Value.ToString() : string.Empty, myInfoDto.LastName, myInfoDto.PhoneNumber, myInfoDto.Title.HasValue ? myInfoDto.Title.Value.ToString() : string.Empty)); var id = User.Identity.GetUserId(); var user = await NdUserManager.FindByIdAsync(id); var emailChanged = false; try { if (!string.IsNullOrWhiteSpace(myInfoDto.Clinic) && user.Clinic != myInfoDto.Clinic) { user.Clinic = myInfoDto.Clinic; } if (!string.IsNullOrWhiteSpace(myInfoDto.Email) && user.Email != myInfoDto.Email) { emailChanged = true; user.EmailConfirmed = false; user.Email = myInfoDto.Email; } if (!string.IsNullOrWhiteSpace(myInfoDto.FirstName) && user.FirstName != myInfoDto.FirstName) { user.FirstName = myInfoDto.FirstName; } if (myInfoDto.Gender.HasValue && user.Gender != myInfoDto.Gender.Value) { user.Gender = myInfoDto.Gender.Value; } if (!string.IsNullOrWhiteSpace(myInfoDto.LastName) && user.LastName != myInfoDto.LastName) { user.LastName = myInfoDto.LastName; } if (!string.IsNullOrWhiteSpace(myInfoDto.PhoneNumber) && user.PhoneNumber != myInfoDto.PhoneNumber) { user.PhoneNumber = myInfoDto.PhoneNumber; } if (myInfoDto.Title.HasValue && user.Title != myInfoDto.Title.Value) { user.Title = myInfoDto.Title.Value; } } catch (Exception ex) { _logger.Error(string.Format("Update my info failed. Id: [{0}]", id), ex); return(InternalServerError(ex)); } var result = await NdUserManager.UpdateAsync(user); if (!result.Succeeded) { _logger.Error(string.Format( "Update my info failed. Email: [{0}], Reason: [{1}]", user.Email, string.Join(Environment.NewLine, result.Errors))); return(GetErrorResult(result)); } await NdDbContext.SaveChangesAsync(); try { await NdUserManager.SendEmailAsync(id, "Account Information Chaged", NdEmailService.CreateAccountInformationChangedBody(_factory.CreateUserInfo(user))); } catch (Exception ex) { _logger.Error(string.Format("Error sending AccountInformationChanged email. Email: [{0}]", user.Email), ex); return(InternalServerError(ex)); } if (emailChanged) { try { string code = await NdUserManager.GenerateEmailConfirmationTokenAsync(id); var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = id, code = code })); await NdUserManager.SendEmailAsync(id, "Confirm your account", NdEmailService.CreateConfirmEmailBody(callbackUrl.ToString())); } catch (Exception ex) { _logger.Error(string.Format("Error sending ConfirmEmail email. Email: {0}]", user.Email), ex); return(InternalServerError(ex)); } } _logger.Debug(string.Format("My info updated. Id: [{0}]", id)); return(Ok()); }