public IHttpActionResult Update(int id, [FromBody] AdministratorInfoCreateUpdate adminInfo) { try { var oRequest = new ORequest(AccessTokenManager.ScopeAdministrators); if (!oRequest.IsApiAuthorized) { return(Unauthorized()); } if (adminInfo == null) { return(BadRequest("Could not read administrator from body")); } if (!DataProvider.AdministratorDao.ApiIsExists(id)) { return(NotFound()); } string errorMessage; var retval = DataProvider.AdministratorDao.ApiUpdate(id, adminInfo, out errorMessage); if (retval == null) { return(BadRequest(errorMessage)); } return(Ok(new OResponse(retval))); } catch (Exception ex) { LogUtils.AddErrorLog(ex); return(InternalServerError(ex)); } }
public IHttpActionResult Create([FromBody] AdministratorInfoCreateUpdate adminInfo) { try { var request = new AuthenticatedRequest(); var isApiAuthorized = request.IsApiAuthenticated && AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeAdministrators); if (!isApiAuthorized) { return(Unauthorized()); } var retval = DataProvider.AdministratorDao.ApiInsert(adminInfo, out var errorMessage); if (retval == null) { return(BadRequest(errorMessage)); } return(Ok(new { Value = retval })); } catch (Exception ex) { LogUtils.AddErrorLog(ex); return(InternalServerError(ex)); } }
private bool UpdateValidate(AdministratorInfoCreateUpdate adminInfoToUpdate, string userName, string email, string mobile, out string errorMessage) { errorMessage = string.Empty; if (adminInfoToUpdate.UserName != null && adminInfoToUpdate.UserName != userName) { if (string.IsNullOrEmpty(adminInfoToUpdate.UserName)) { errorMessage = "用户名不能为空"; return(false); } if (adminInfoToUpdate.UserName.Length < ConfigManager.SystemConfigInfo.AdminUserNameMinLength) { errorMessage = $"用户名长度必须大于等于{ConfigManager.SystemConfigInfo.AdminUserNameMinLength}"; return(false); } if (IsUserNameExists(adminInfoToUpdate.UserName)) { errorMessage = "用户名已存在,请更换用户名"; return(false); } } if (adminInfoToUpdate.Email != null && adminInfoToUpdate.Email != email) { if (!string.IsNullOrEmpty(adminInfoToUpdate.Email) && IsEmailExists(adminInfoToUpdate.Email)) { errorMessage = "电子邮件地址已被注册,请更换邮箱"; return(false); } } if (adminInfoToUpdate.Mobile != null && adminInfoToUpdate.Mobile != mobile) { if (!string.IsNullOrEmpty(adminInfoToUpdate.Mobile) && IsMobileExists(adminInfoToUpdate.Mobile)) { errorMessage = "手机号码已被注册,请更换手机号码"; return(false); } } return(true); }
public AdministratorInfo ApiInsert(AdministratorInfoCreateUpdate adminInfoToInsert, out string errorMessage) { errorMessage = string.Empty; try { var dbAdminInfo = new AdministratorInfoDatabase(); adminInfoToInsert.Load(dbAdminInfo); if (!InsertValidate(dbAdminInfo.UserName, dbAdminInfo.Password, dbAdminInfo.Email, dbAdminInfo.Mobile, out errorMessage)) { return(null); } dbAdminInfo.Password = EncodePassword(dbAdminInfo.Password, EPasswordFormatUtils.GetEnumType(dbAdminInfo.PasswordFormat), out var passwordSalt); dbAdminInfo.PasswordSalt = passwordSalt; dbAdminInfo.CreationDate = DateTime.Now; dbAdminInfo.LastActivityDate = DateTime.Now; using (var connection = GetConnection()) { var identity = connection.Insert(dbAdminInfo); if (identity > 0) { dbAdminInfo.Id = Convert.ToInt32(identity); } } return(dbAdminInfo.ToAdministratorInfo()); } catch (Exception ex) { errorMessage = ex.Message; return(null); } }
public AdministratorInfo ApiUpdate(int id, AdministratorInfoCreateUpdate adminInfoToUpdate, out string errorMessage) { var adminInfo = ApiGetAdministrator(id); if (!UpdateValidate(adminInfoToUpdate, adminInfo.UserName, adminInfo.Email, adminInfo.Mobile, out errorMessage)) { return(null); } var dbUserInfo = new AdministratorInfoDatabase(adminInfo); adminInfoToUpdate.Load(dbUserInfo); dbUserInfo.Password = adminInfo.Password; dbUserInfo.PasswordFormat = adminInfo.PasswordFormat; dbUserInfo.PasswordSalt = adminInfo.PasswordSalt; using (var connection = GetConnection()) { connection.Update(dbUserInfo); } return(dbUserInfo.ToAdministratorInfo()); }