2 * In Samsung Ukraine R&D Center (SRK under a contract between)
3 * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6 package com.samsung.samserver.web.rest.service;
8 import com.samsung.samserver.domain.User;
9 import com.samsung.samserver.repository.UserRepository;
10 import com.samsung.samserver.security.SecurityUtils;
11 import com.samsung.samserver.service.impl.MailService;
12 import com.samsung.samserver.service.impl.UserService;
13 import com.samsung.samserver.service.dto.UserDTO;
15 import com.samsung.samserver.web.rest.errors.InternalServerErrorException;
16 import com.samsung.samserver.web.rest.errors.UserServiceError;
17 import org.apache.commons.lang3.StringUtils;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.springframework.stereotype.Service;
21 import org.springframework.web.bind.annotation.*;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.validation.Valid;
25 import javax.validation.constraints.Size;
29 * REST service for managing the current user's account.
32 public class AccountService {
34 private final Logger log = LoggerFactory.getLogger(AccountService.class);
36 private final UserRepository userRepository;
38 private final UserService userService;
40 private final MailService mailService;
42 public AccountService(UserRepository userRepository, UserService userService, MailService mailService) {
44 this.userRepository = userRepository;
45 this.userService = userService;
46 this.mailService = mailService;
50 * POST /register : register the user.
52 * @param managedUserVM the managed user View Model
53 * @throws UserServiceError.InvalidPasswordException 400 (Bad Request) if the password is incorrect
54 * @throws UserServiceError.EmailAlreadyUsedException 400 (Bad Request) if the email is already used
55 * @throws UserServiceError.LoginAlreadyUsedException 400 (Bad Request) if the login is already used
57 public void registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) {
58 if (!checkPasswordLength(managedUserVM.getPassword())) {
59 throw new UserServiceError.InvalidPasswordException();
61 userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()).ifPresent(u -> {throw new UserServiceError.LoginAlreadyUsedException();});
62 userRepository.findOneByEmailIgnoreCase(managedUserVM.getEmail()).ifPresent(u -> {throw new UserServiceError.EmailAlreadyUsedException();});
63 User user = userService.registerUser(managedUserVM, managedUserVM.getPassword());
64 mailService.sendActivationEmail(user);
68 * GET /activate : activate the registered user.
70 * @param key the activation key
71 * @throws RuntimeException 500 (Internal Server Error) if the user couldn't be activated
73 public void activateAccount(@RequestParam(value = "key") String key) {
74 Optional<User> user = userService.activateRegistration(key);
75 if (!user.isPresent()) {
76 throw new InternalServerErrorException("No user was found for this reset key");
81 * GET /authenticate : check if the user is authenticated, and return its login.
83 * @param request the HTTP request
84 * @return the login if the user is authenticated
86 public String isAuthenticated(HttpServletRequest request) {
87 log.debug("REST request to check if the current user is authenticated");
88 return request.getRemoteUser();
92 * GET /account : get the current user.
94 * @return the current user
95 * @throws RuntimeException 500 (Internal Server Error) if the user couldn't be returned
97 public UserDTO getAccount() {
98 return userService.getUserWithAuthorities()
100 .orElseThrow(() -> new InternalServerErrorException("User could not be found"));
104 * POST /account : update the current user information.
106 * @param userDTO the current user information
107 * @throws UserServiceError.EmailAlreadyUsedException 400 (Bad Request) if the email is already used
108 * @throws RuntimeException 500 (Internal Server Error) if the user login wasn't found
110 public void saveAccount(@Valid @RequestBody UserDTO userDTO) {
111 final String userLogin = SecurityUtils.getCurrentUserLogin().orElseThrow(() -> new InternalServerErrorException("Current user login not found"));
112 Optional<User> existingUser = userRepository.findOneByEmailIgnoreCase(userDTO.getEmail());
113 if (existingUser.isPresent() && (!existingUser.get().getLogin().equalsIgnoreCase(userLogin))) {
114 throw new UserServiceError.EmailAlreadyUsedException();
116 Optional<User> user = userRepository.findOneByLogin(userLogin);
117 if (!user.isPresent()) {
118 throw new InternalServerErrorException("User could not be found");
120 userService.updateUser(userDTO.getFirstName(), userDTO.getLastName(), userDTO.getEmail(),
121 userDTO.getLangKey(), userDTO.getImageUrl());
125 * POST /account/change-password : changes the current user's password
127 * @param password the new password
128 * @throws UserServiceError.InvalidPasswordException 400 (Bad Request) if the new password is incorrect
130 public void changePassword(@RequestBody String password) {
131 if (!checkPasswordLength(password)) {
132 throw new UserServiceError.InvalidPasswordException();
134 userService.changePassword(password);
138 * POST /account/reset-password/init : Send an email to reset the password of the user
140 * @param mail the mail of the user
141 * @throws UserServiceError.EmailNotFoundException 400 (Bad Request) if the email address is not registered
143 public void requestPasswordReset(@RequestBody String mail) {
144 mailService.sendPasswordResetMail(
145 userService.requestPasswordReset(mail)
146 .orElseThrow(UserServiceError.EmailNotFoundException::new)
151 * POST /account/reset-password/finish : Finish to reset the password of the user
153 * @param keyAndPassword the generated key and the new password
154 * @throws UserServiceError.InvalidPasswordException 400 (Bad Request) if the password is incorrect
155 * @throws RuntimeException 500 (Internal Server Error) if the password could not be reset
157 public void finishPasswordReset(@RequestBody KeyAndPasswordVM keyAndPassword) {
158 if (!checkPasswordLength(keyAndPassword.getNewPassword())) {
159 throw new UserServiceError.InvalidPasswordException();
161 Optional<User> user =
162 userService.completePasswordReset(keyAndPassword.getNewPassword(), keyAndPassword.getKey());
164 if (!user.isPresent()) {
165 throw new InternalServerErrorException("No user was found for this reset key");
169 private static boolean checkPasswordLength(String password) {
170 return !StringUtils.isEmpty(password) &&
171 password.length() >= ManagedUserVM.PASSWORD_MIN_LENGTH &&
172 password.length() <= ManagedUserVM.PASSWORD_MAX_LENGTH;
177 * View Model object for storing the user's key and password.
179 public static class KeyAndPasswordVM {
183 private String newPassword;
185 public String getKey() {
189 public void setKey(String key) {
193 public String getNewPassword() {
197 public void setNewPassword(String newPassword) {
198 this.newPassword = newPassword;
204 * View Model extending the UserDTO, which is meant to be used in the user management UI.
206 public static class ManagedUserVM extends UserDTO {
208 public static final int PASSWORD_MIN_LENGTH = 4;
210 public static final int PASSWORD_MAX_LENGTH = 100;
212 @Size(min = PASSWORD_MIN_LENGTH, max = PASSWORD_MAX_LENGTH)
213 private String password;
215 public ManagedUserVM() {
216 // Empty constructor needed for Jackson.
219 public String getPassword() {
223 public void setPassword(String password) {
224 this.password = password;
228 public String toString() {
229 return "ManagedUserVM{" +
230 "} " + super.toString();