public async Task <ActionResult <UULResponse> > ChangePassword(UserUpdatePasswordDTO userPwdsDTO) { if (!userPwdsDTO.isValid(out var msg)) { return(Error.ProfileValidationFailed.CreateErrorResponse(_logger, "ChangePassword", new Exception(msg))); } UULResponse response; try { var userInfoDTO = await AuthenticateUserOrThrow(userPwdsDTO.toLoginInfoDTO()); var user = await UserDao.GetUserByDetailsOrThrow(_context, userInfoDTO.Login, userInfoDTO.ApartmentCode); var salt = SecHelper.CreateSalt(); user.Salt = salt; user.Hash = SecHelper.SaltAndHashPwd(userPwdsDTO.NewPwd, salt); _context.Users.Update(user); await _context.SaveChangesAsync(); var tokenString = SecHelper.GenerateJSONWebToken(userInfoDTO.Login, userInfoDTO.ApartmentCode, _config); var habitants = await _context.Habitants.Where(h => h.User.ID == user.ID).Select(h => new HabitantDTO(h)).ToListAsync(); response = new UULResponse() { Success = true, Message = tokenString, Data = new UserInfoDTO(user, habitants) }; } catch (UserProfileNotFoundException e) { response = Error.ProfileNotFound.CreateErrorResponse(_logger, "ChangePassword", e); } catch (AuthException e) { response = Error.AuthFailed.CreateErrorResponse(_logger, "ChangePassword", e); } catch (Exception e) { response = Error.ProfileChangePwdFailed.CreateErrorResponse(_logger, "ChangePassword", e); } return(response); }
public async Task <ActionResult <UULResponse> > NewUser(NewUserDTO newUser) { UULResponse response; try { if (!newUser.isValid(out var msg)) { return(Error.ProfileValidationFailed.CreateErrorResponse(_logger, "NewProfile", new Exception(msg))); } var exist = await _context.Users.AnyAsync(u => u.Login.Equals(newUser.Login) && u.ApartmentCode == newUser.ApartmentCode); if (exist) { return(Error.ProfileAlreadyExists.CreateErrorResponse(_logger, "NewProfile")); } var user = UserDao.AddFromDto(_context, newUser); await _context.SaveChangesAsync(); var userInfo = new UserInfoDTO(user); var tokenString = SecHelper.GenerateJSONWebToken(userInfo.Login, userInfo.ApartmentCode, _config); response = new UULResponse() { Success = true, Message = tokenString, Data = userInfo }; } catch (Exception e) { response = Error.ProfileCreationFailed.CreateErrorResponse(_logger, "NewProfile", e); } return(response); }
public async Task <ActionResult <UULResponse> > LoginUser(UserLoginInfoDTO loginInfoDTO) { UULResponse response; try { var userInfoDTO = await AuthenticateUserOrThrow(loginInfoDTO); var tokenString = SecHelper.GenerateJSONWebToken(userInfoDTO.Login, userInfoDTO.ApartmentCode, _config); response = new UULResponse() { Success = true, Message = "Login success", Data = tokenString }; } catch (AuthException e) { response = Error.AuthFailed.CreateErrorResponse(_logger, "Login", e); } catch (Exception e) { response = Error.ProfileLoginFailed.CreateErrorResponse(_logger, "Login", e); } return(response); }