public async Task <string> UpdateUserInfo([FromBody] User user) { try { if (user != null) { StringConversion stringConversion = new StringConversion(); user.Id = Guid.Parse(stringConversion.DecryptString(user.UpdatedId)); var extendUserResponse = await _bosAuthClient.ExtendUserAsync(user); if (extendUserResponse != null && extendUserResponse.IsSuccessStatusCode) { return("User's information updated successfully"); } else { return(extendUserResponse != null ? extendUserResponse.BOSErrors[0].Message : "We are unable to update this user's information at this time. Please try again."); } } else { return("User data inputted is inaccurate. Please try again"); } } catch (Exception ex) { Logger.LogException("Users", "UpdateUserInfo", ex); return(ex.Message); } }
public async Task <string> ForcePasswordChange([FromBody] JObject data) { try { if (data != null) { StringConversion stringConversion = new StringConversion(); string userId = stringConversion.DecryptString(data["userId"].ToString()); //Decrypting the userId sent from the View string password = data["password"].ToString(); var response = await _bosAuthClient.ForcePasswordChangeAsync(Guid.Parse(userId), password); //Making an call to the BOS API to ForceChange the Password. This is done because at this point there is no way of knowing the user's original password if (response != null && response.IsSuccessStatusCode) { return("Password updated successfully"); //On success, returing a message } else { Logger.LogException("Auth", "ForcePasswordChange", null); return("Something went wrong. We are not able to change the password at this moment. Please try again later."); } } else { return("Data cannot be null"); } } catch (Exception ex) { Logger.LogException("Auth", "ForcePasswordChange", ex); return("Something went wrong. We are not able to change the password at this moment. Please try again later."); } }
public async Task <string> UpdateUserRolesByAdmin([FromBody] JObject data) { try { List <Role> updatedRoles = data["UpdatedRoles"].ToObject <List <Role> >(); var updatedUserId = data["UserId"].ToString(); StringConversion stringConversion = new StringConversion(); Guid userId = Guid.Parse(stringConversion.DecryptString(updatedUserId)); if (updatedRoles.Count > 0) { var response = await _bosAuthClient.AssociateUserToMultipleRolesAsync(userId, updatedRoles); if (response != null && !response.IsSuccessStatusCode) { throw new Exception("Something went wrong while updating the roles. Please try again later"); } else { return("User's roles updates successfully"); } } else { return("Roles to associate with the user cannot be empty"); } } catch (Exception ex) { Logger.LogException("Roles", "UpdateRole", ex); return(ex.Message); } }
public async Task <string> DeleteUser([FromBody] string userId) { try { if (!string.IsNullOrEmpty(userId)) { StringConversion stringConversion = new StringConversion(); string actualUserId = stringConversion.DecryptString(userId); var response = await _bosAuthClient.DeleteUserAsync(Guid.Parse(actualUserId)); if (response != null && response.IsSuccessStatusCode) { return("User deleted successfully"); } else { return(response != null ? response.BOSErrors[0].Message : "We are unable to delete this user at this time. Please try again."); } } else { return("UserId cannot be null. Please check and try again."); } } catch (Exception ex) { Logger.LogException("Users", "DeleteUser", ex); return(ex.Message); } }
public async Task <string> DeleteUser([FromBody] string userId) { try { if (!string.IsNullOrEmpty(userId)) //Confirming a non-null, non-empty userId { StringConversion stringConversion = new StringConversion(); string actualUserId = stringConversion.DecryptString(userId); //Since the userId sent to the view is encrypted, before sending it to the BOS API, we have to decrypt it var response = await _bosAuthClient.DeleteUserAsync(Guid.Parse(actualUserId)); //Making an API call to BOS to delete the user if (response != null && response.IsSuccessStatusCode) { return("User deleted successfully"); //On success, return the message } else { return(response != null ? response.BOSErrors[0].Message : "We are unable to delete this user at this time. Please try again."); //Else, return the BOS error message //An example could be, if there is no user with the id } } else { return("UserId cannot be null. Please check and try again."); } } catch (Exception ex) { Logger.LogException("Users", "DeleteUser", ex); return(ex.Message); } }
public async Task <IActionResult> EditUser(string userId) { try { dynamic model = new ExpandoObject(); StringConversion stringConversion = new StringConversion(); string actualUserId = stringConversion.DecryptString(userId); var userInfo = await _bosAuthClient.GetUserByIdWithRolesAsync <User>(Guid.Parse(actualUserId)); if (userInfo.IsSuccessStatusCode) { userInfo.User.UpdatedId = userId; model.UserInfo = userInfo.User; } List <string> rolesList = new List <string>(); foreach (UserRole role in userInfo.User.Roles) { rolesList.Add(role.Role.Name); } model.RolesList = rolesList; var availableRoles = await _bosAuthClient.GetRolesAsync <Role>(); if (availableRoles.IsSuccessStatusCode) { model.AvailableRoles = availableRoles.Roles; } return(View("EditUser", model)); } catch (Exception ex) { throw new Exception(ex.Message); } }
public async Task <string> ForcePasswordChange([FromBody] JObject data) { try { StringConversion stringConversion = new StringConversion(); string userId = stringConversion.DecryptString(data["userId"].ToString()); string password = data["password"].ToString(); var response = await _bosAuthClient.ForcePasswordChangeAsync(Guid.Parse(userId), password); if (response != null && response.IsSuccessStatusCode) { return("Password updated successfully"); } else { Logger.LogException("Auth", "ForcePasswordChange", null); return("Something went wrong. We are not able to change the password at this moment. Please try again later."); } } catch (Exception ex) { Logger.LogException("Auth", "ForcePasswordChange", ex); return("Something went wrong. We are not able to change the password at this moment. Please try again later."); } }
public async Task <string> UpdateUserInfo([FromBody] User user) { try { if (user != null && !string.IsNullOrEmpty(user.UpdatedId)) //Checking for a non-null user object { StringConversion stringConversion = new StringConversion(); user.Id = Guid.Parse(stringConversion.DecryptString(user.UpdatedId)); //Since the userId sent to the view is encrypted, before sending it to the BOS API, we have to decrypt it var extendUserResponse = await _bosAuthClient.ExtendUserAsync(user); //Making an API call to BOS to update the user's information if (extendUserResponse != null && extendUserResponse.IsSuccessStatusCode) { return("User's information updated successfully"); //On success, returning the message } else { //Else, return the BOS error message. An example could be, if there is no user with the id return(extendUserResponse != null ? extendUserResponse.BOSErrors[0].Message : "We are unable to update this user's information at this time. Please try again."); } } else { return("User data cannot be null. Please try again"); } } catch (Exception ex) { Logger.LogException("Users", "UpdateUserInfo", ex); return(ex.Message); } }
public async Task <string> UpdateUserRolesByAdmin([FromBody] JObject data) { try { if (data != null && data["UpdatedRoles"] != null) //Checking for non-null input data and roles list { List <Role> updatedRoles = data["UpdatedRoles"].ToObject <List <Role> >(); //Converting the data to a list of roles Guid userId = Guid.Empty; if (data["UserId"] != null) { var updatedUserId = data["UserId"].ToString(); //The userId sent to the View is in an encrypted format. So, we will have to decrypt it before sending it to the BOS API StringConversion stringConversion = new StringConversion(); userId = Guid.Parse(stringConversion.DecryptString(updatedUserId)); } if (updatedRoles.Count > 0) //Confirming that there is at least one role in the list { if (userId != Guid.Empty) { var response = await _bosAuthClient.AssociateUserToMultipleRolesAsync(userId, updatedRoles); //Making an API call to BOS to associate user with the roles if (response != null && response.IsSuccessStatusCode) { return("User's roles updates successfully"); //On success, returing appropriate message } else if (response != null && response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { return("Token Expired, Please try again");//Token Expired } else { return(response.BOSErrors[0].Message); //Else, return BOS error message } } else { return("Incorrect user id"); } } else { return("Roles to associate with the user cannot be empty"); } } else { return("Roles to associate with the user cannot be empty"); } } catch (Exception ex) { Logger.LogException("Roles", "UpdateRole", ex); return(ex.Message); } }
/// <summary> /// Author: BOS Framework, Inc /// Description: Is triggered when the 'Edit' link is clicked. Returns the view with the form to edit the selected user, with the information pre-filled. /// </summary> /// <param name="userId"></param> /// <returns></returns> public async Task <IActionResult> EditUser(string userId) { try { if (!string.IsNullOrEmpty(userId)) { dynamic model = await GetPageData(); if (model == null) { model = new ExpandoObject(); } StringConversion stringConversion = new StringConversion(); string actualUserId = stringConversion.DecryptString(userId); var userInfo = await _bosAuthClient.GetUserByIdWithRolesAsync <User>(Guid.Parse(actualUserId)); if (userInfo != null && userInfo.IsSuccessStatusCode) { userInfo.User.UpdatedId = userId; model.UserInfo = userInfo.User; } List <string> rolesList = new List <string>(); foreach (UserRole role in userInfo.User.Roles) { rolesList.Add(role.Role.Name); } model.RolesList = rolesList; var availableRoles = await _bosAuthClient.GetRolesAsync <Role>(); if (availableRoles != null && availableRoles.IsSuccessStatusCode) { model.AvailableRoles = availableRoles.Roles; } return(View("EditUser", model)); } else { ModelState.AddModelError("CustomError", "The selected user has inaccurate id. Please try again."); return(View("Index", await GetPageData())); } } catch (Exception ex) { Logger.LogException("Users", "EditUser", ex); dynamic model = new ExpandoObject(); model.Message = ex.Message; model.StackTrace = ex.StackTrace; return(View("ErrorPage", model)); } }
public async Task <string> UpdateUserInfo([FromBody] User user) { StringConversion stringConversion = new StringConversion(); user.Id = Guid.Parse(stringConversion.DecryptString(user.UpdatedId)); var extendUserResponse = await _bosAuthClient.ExtendUserAsync(user); if (extendUserResponse.IsSuccessStatusCode) { return("User's information updated successfully"); } else { return(extendUserResponse.BOSErrors[0].Message); } }
public async Task <string> DeleteUser([FromBody] string userId) { StringConversion stringConversion = new StringConversion(); string actualUserId = stringConversion.DecryptString(userId); var response = await _bosAuthClient.DeleteUserAsync(Guid.Parse(actualUserId)); if (response.IsSuccessStatusCode) { return("User deleted successfully"); } else { throw new Exception(response.BOSErrors[0].Message); } }
public async Task <string> ForcePasswordChange([FromBody] JObject data) { StringConversion stringConversion = new StringConversion(); string userId = stringConversion.DecryptString(data["userId"].ToString()); string password = data["password"].ToString(); var response = await _bosAuthClient.ForcePasswordChangeAsync(Guid.Parse(userId), password); if (response.IsSuccessStatusCode) { return("Password updated successfully"); } else { throw new Exception(response.BOSErrors[0].Message); } }
public async Task <string> UpdateUserInfo([FromBody] JObject user) { try { if (user != null) //Confirm non-null input data { StringConversion stringConversion = new StringConversion(); Guid myId = Guid.Parse(stringConversion.DecryptString(Convert.ToString(user["UpdatedId"]))); bool confirmed = false; string emailConfirmed = Convert.ToString(user["EmailConfirmed"]); if (!string.IsNullOrEmpty(emailConfirmed)) { confirmed = true; } User edituser = new User { Id = myId, Active = Convert.ToBoolean(user["Active"]), Email = Convert.ToString(user["Email"]), FirstName = Convert.ToString(user["FirstName"]), LastName = Convert.ToString(user["LastName"]), Username = Convert.ToString(user["Username"]), EmailConfirmed = Convert.ToBoolean(confirmed), Deleted = false }; var extendUserResponse = await _bosAuthClient.ExtendUserAsync(edituser); if (extendUserResponse != null && extendUserResponse.IsSuccessStatusCode) { return("User's information updated successfully"); //On success, returning the message } else if (extendUserResponse != null && extendUserResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized) { return("Token Expired, Please try again");//Token Expired } else { //Else, return the BOS error message. An example could be, if there is no user with the id return(extendUserResponse != null ? extendUserResponse.BOSErrors[0].Message : "We are unable to update this user's information at this time. Please try again."); } } else { return("User data cannot be null. Please try again"); } } catch (Exception ex) { Logger.LogException("Users", "UpdateUserInfo", ex); return(ex.Message); } }
/// <summary> /// Author: BOS Framework, Inc /// Description: Is triggered when the 'Edit' link is clicked. Returns the view with the form to edit the selected user, with the information pre-filled. /// </summary> /// <param name="userId"></param> /// <returns></returns> public async Task <IActionResult> EditUser(string userId) { try { /*-------- LOGIC ---------- * Confirm non-null userId sent as the input * Decrypt the userId to get the actual userId * Get the user's information and associated roles via BOS API call * Get all the roles in the application * Prepare the model object that is required to render the page * Navigate to the "EditUsewr" view with data */ if (!string.IsNullOrEmpty(userId)) //Checking for a non-null, non-empty userId { dynamic model = await GetPageData(); //Getting the data that is required for rendering the page if (model == null) { model = new ExpandoObject(); //If the method returns null, then re-ininitate a dynamic object } StringConversion stringConversion = new StringConversion(); string actualUserId = stringConversion.DecryptString(userId); //The userID that is sent to the view is encrypted. Before sending it to the BOS API, we'll have to decrypt it var userInfo = await _bosAuthClient.GetUserByIdWithRolesAsync <User>(Guid.Parse(actualUserId)); //Making an API call to BOS to get the user's information together with the associated roles if (userInfo != null && userInfo.IsSuccessStatusCode && userInfo.User != null) { userInfo.User.UpdatedId = userId; //Setting rhe updated (encrypted) userID, so it can be used in the View model.UserInfo = userInfo.User; //User's data is assigned to the model List <string> rolesList = new List <string>(); foreach (UserRole role in userInfo.User.Roles) { rolesList.Add(role.Role.Name); } model.RolesList = rolesList; //All the roles that the user is already associated with } var availableRoles = await _bosAuthClient.GetRolesAsync <Role>(); //Making a BOS API Call to fetch all the Roles in the application if (availableRoles != null && availableRoles.IsSuccessStatusCode) { model.AvailableRoles = availableRoles.Roles; //On success, setting the complete Roles list } return(View("EditUser", model)); //Returning to the "EditUser" view, that has the form to edit user's information and roles, with the data required to render the page } else { ModelState.AddModelError("CustomError", "The selected user has inaccurate id. Please try again."); return(View("Index", await GetPageData())); } } catch (Exception ex) { Logger.LogException("Users", "EditUser", ex); dynamic model = new ExpandoObject(); model.Message = ex.Message; model.StackTrace = ex.StackTrace; return(View("ErrorPage", model)); } }
public async Task <string> ChangeUserActiveStatus([FromBody] JObject data) { try { if (data != null) { if (data["UserId"] == null) { return("UserId cannot be null"); } else if (data["Action"] == null) { return("Action cannot be null"); } StringConversion stringConversion = new StringConversion(); string actualUserId = stringConversion.DecryptString(data["UserId"]?.ToString()); //Since the userId sent to the view is encrypted, before sending it to the BOS API, we have to decrypt it var action = data["Action"]?.ToString(); //Based on the action that has been requested, we either make a call to the BOS' ActivateUser API or DeactivateUser API if (action == "activate") { var response = await _bosAuthClient.ActivateUserAsync(Guid.Parse(actualUserId)); //Making the BOS API call with the userId if (response != null && response.IsSuccessStatusCode) { return("The user has been activated successfully"); //On success, returning an appropriate message } else { return(response.BOSErrors[0].Message); //On error, returing the BOS error message } } else if (action == "deactivate") { var response = await _bosAuthClient.DeactivateUserAsync(Guid.Parse(actualUserId)); //Making the BOS API call with the userId if (response != null && response.IsSuccessStatusCode) { return("The user has been deactivated successfully"); //On success, returning an appropriate message } else { return(response.BOSErrors[0].Message); //On error, returing the BOS error message } } else { return("You are trying to perform an unrecognized operation"); } } else { return("Data cannot be null"); } } catch (Exception ex) { Logger.LogException("Users", "ChangeUserActiveStatus", ex); return(ex.Message); } }