From 95fc9da47decf785efc8aed710c02936b0994a38 Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Wed, 15 May 2019 00:49:54 +0400 Subject: [PATCH 01/14] lk base, created from dev --- .../ulstu/user/controller/UserController.java | 7 - .../user/controller/UserControllerV2.java | 178 ++++++++++++++++++ .../ru/ulstu/user/service/UserService.java | 21 +-- .../user/service/UserSessionService.java | 4 + src/main/resources/templates/default.html | 10 +- .../resources/templates/users/profile.html | 75 ++++++++ 6 files changed, 269 insertions(+), 26 deletions(-) create mode 100644 src/main/java/ru/ulstu/user/controller/UserControllerV2.java create mode 100644 src/main/resources/templates/users/profile.html diff --git a/src/main/java/ru/ulstu/user/controller/UserController.java b/src/main/java/ru/ulstu/user/controller/UserController.java index d7db909..6806d19 100644 --- a/src/main/java/ru/ulstu/user/controller/UserController.java +++ b/src/main/java/ru/ulstu/user/controller/UserController.java @@ -141,13 +141,6 @@ public class UserController extends OdinController { return new Response<>(userService.activateUser(activationKey)); } - // TODO: add page for user edit (user-profile) - @PostMapping("/change-information") - public Response changeInformation(@Valid @RequestBody UserDto userDto) { - log.debug("REST: UserController.changeInformation( {} )", userDto.getLogin()); - return new Response<>(userService.updateUserInformation(userDto)); - } - // TODO: add page for user password change (user-profile) @PostMapping("/change-password") public Response changePassword(@Valid @RequestBody UserDto userDto) { diff --git a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java new file mode 100644 index 0000000..f15872e --- /dev/null +++ b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java @@ -0,0 +1,178 @@ +package ru.ulstu.user.controller; + +import com.sun.org.apache.xpath.internal.operations.Mod; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.security.access.annotation.Secured; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.configuration.Constants; +import ru.ulstu.core.model.response.PageableItems; +import ru.ulstu.core.model.response.Response; +import ru.ulstu.odin.controller.OdinController; +import ru.ulstu.odin.model.OdinMetadata; +import ru.ulstu.odin.model.OdinVoid; +import ru.ulstu.odin.service.OdinService; +import ru.ulstu.user.model.*; +import ru.ulstu.user.service.UserService; +import ru.ulstu.user.service.UserSessionService; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import javax.validation.Valid; + +import static ru.ulstu.user.controller.UserController.URL; + +@Controller +@RequestMapping(value = "/users") +public class UserControllerV2 extends OdinController { + public static final String URL = Constants.API_1_0 + "users"; + public static final String ROLES_URL = "/roles"; + public static final String ROLES_META_URL = ROLES_URL + OdinController.META_LIST_URL; + public static final String SESSIONS_URL = "/sessions"; + public static final String SESSIONS_META_URL = SESSIONS_URL + OdinController.META_LIST_URL; + public static final String REGISTER_URL = "/register"; + public static final String ACTIVATE_URL = "/activate"; + public static final String PASSWORD_RESET_REQUEST_URL = "/password-reset-request"; + public static final String PASSWORD_RESET_URL = "/password-reset"; + + private final Logger log = LoggerFactory.getLogger(UserController.class); + + private final UserService userService; + private final UserSessionService userSessionService; + private final OdinService odinRolesService; + private final OdinService odinSessionsService; + + public UserControllerV2(UserService userService, + UserSessionService userSessionService, + OdinService odinRolesService, + OdinService odinSessionsService) { + super(UserListDto.class, UserDto.class); + this.userService = userService; + this.userSessionService = userSessionService; + this.odinRolesService = odinRolesService; + this.odinSessionsService = odinSessionsService; + } + + @GetMapping("/profile") + public void getUserProfile(ModelMap modelMap, HttpServletRequest request) { + HttpSession session = request.getSession(false); + final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); + modelMap.addAttribute("userDto", new UserDto(userSessionService.getUserDtoBySessionId(sessionId))); + } + + @PostMapping("/profile") + public void updateUserProfile(ModelMap modelMap, HttpServletRequest request, UserDto userDto) { + HttpSession session = request.getSession(false); + final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); + User user = userSessionService.getUserDtoBySessionId(sessionId); + modelMap.addAttribute("userDto", userService.updateUserInformation(user, userDto)); + } + + @GetMapping(ROLES_URL) + @Secured(UserRoleConstants.ADMIN) + public Response> getUserRoles() { + log.debug("REST: UserController.getUserRoles()"); + return new Response<>(userService.getUserRoles()); + } + + @GetMapping(ROLES_META_URL) + @Secured(UserRoleConstants.ADMIN) + public Response getUserRolesMetaData() { + log.debug("REST: UserController.getUserRolesMetaData()"); + return new Response<>(odinRolesService.getListModel(UserRoleDto.class)); + } + + @GetMapping(SESSIONS_URL) + @Secured(UserRoleConstants.ADMIN) + public Response> getUserSessions(@RequestParam(value = "offset", defaultValue = "0") int offset, + @RequestParam(value = "count", defaultValue = "10") int count) { + log.debug("REST: UserController.getUserSessions()"); + return new Response<>(userSessionService.getSessions(offset, count)); + } + + @GetMapping(SESSIONS_META_URL) + @Secured(UserRoleConstants.ADMIN) + public Response getUserSessionsMetaData() { + log.debug("REST: UserController.getUserSessionsMetaData()"); + return new Response<>(odinSessionsService.getListModel(UserSessionListDto.class)); + } + + @GetMapping("") + @Secured(UserRoleConstants.ADMIN) + public Response> getAllUsers(@RequestParam(value = "offset", defaultValue = "0") int offset, + @RequestParam(value = "count", defaultValue = "10") int count) { + log.debug("REST: UserController.getAllUsers( {}, {} )", offset, count); + return new Response<>(userService.getAllUsers(offset, count)); + } + + @GetMapping("/{userId}") + @Secured(UserRoleConstants.ADMIN) + public Response getUser(@PathVariable Integer userId) { + log.debug("REST: UserController.getUser( {} )", userId); + return new Response<>(userService.getUserWithRolesById(userId)); + } + + + @PostMapping("") + @Secured(UserRoleConstants.ADMIN) + public Response createUser(@Valid @RequestBody UserDto userDto) { + log.debug("REST: UserController.createUser( {} )", userDto.getLogin()); + return new Response<>(userService.createUser(userDto)); + } + + @PutMapping("") + @Secured(UserRoleConstants.ADMIN) + public Response updateUser(@Valid @RequestBody UserDto userDto) { + log.debug("REST: UserController.updateUser( {} )", userDto.getLogin()); + return new Response<>(userService.updateUser(userDto)); + } + + @DeleteMapping("/{userId}") + @Secured(UserRoleConstants.ADMIN) + public Response deleteUser(@PathVariable Integer userId) { + log.debug("REST: UserController.deleteUser( {} )", userId); + return new Response<>(userService.deleteUser(userId)); + } + + @PostMapping(REGISTER_URL) + public Response registerUser(@Valid @RequestBody UserDto userDto) { + log.debug("REST: UserController.registerUser( {} )", userDto.getLogin()); + return new Response<>(userService.createUser(userDto)); + } + + @PostMapping(ACTIVATE_URL) + public Response activateUser(@RequestParam("key") String activationKey) { + log.debug("REST: UserController.activateUser( {} )", activationKey); + return new Response<>(userService.activateUser(activationKey)); + } + + // TODO: add page for user password change (user-profile) + @PostMapping("/change-password") + public Response changePassword(@Valid @RequestBody UserDto userDto) { + log.debug("REST: UserController.changePassword( {} )", userDto.getLogin()); + return new Response<>(userService.changeUserPassword(userDto)); + } + + @PostMapping(PASSWORD_RESET_REQUEST_URL) + public Response requestPasswordReset(@RequestParam("email") String email) { + log.debug("REST: UserController.requestPasswordReset( {} )", email); + return new Response<>(userService.requestUserPasswordReset(email)); + } + + @PostMapping(PASSWORD_RESET_URL) + public Response finishPasswordReset(@RequestParam("key") String key, + @RequestBody UserResetPasswordDto userResetPasswordDto) { + log.debug("REST: UserController.requestPasswordReset( {} )", key); + return new Response<>(userService.completeUserPasswordReset(key, userResetPasswordDto)); + } +} diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index c98e4b7..202cf10 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -205,23 +205,10 @@ public class UserService implements UserDetailsService { return userMapper.userEntityToUserDto(user); } - public UserDto updateUserInformation(UserDto userDto) { - if (userDto.getId() == null) { - throw new EntityIdIsNullException(); - } - if (!Objects.equals( - Optional.ofNullable(getUserByEmail(userDto.getEmail())) - .map(BaseEntity::getId).orElse(userDto.getId()), - userDto.getId())) { - throw new UserEmailExistsException(userDto.getEmail()); - } - User user = userRepository.findOne(userDto.getId()); - if (user == null) { - throw new UserNotFoundException(userDto.getId().toString()); - } - user.setFirstName(userDto.getFirstName()); - user.setLastName(userDto.getLastName()); - user.setEmail(userDto.getEmail()); + public UserDto updateUserInformation(User user, UserDto updateUser) { + user.setFirstName(updateUser.getFirstName()); + user.setLastName(updateUser.getLastName()); + user.setEmail(updateUser.getEmail()); user = userRepository.save(user); log.debug("Updated Information for User: {}", user.getLogin()); return userMapper.userEntityToUserDto(user); diff --git a/src/main/java/ru/ulstu/user/service/UserSessionService.java b/src/main/java/ru/ulstu/user/service/UserSessionService.java index 0d985cd..03415de 100644 --- a/src/main/java/ru/ulstu/user/service/UserSessionService.java +++ b/src/main/java/ru/ulstu/user/service/UserSessionService.java @@ -54,4 +54,8 @@ public class UserSessionService { userSessionRepository.save(userSession); log.debug("User session {} closed", sessionId); } + + public User getUserDtoBySessionId(String sessionId) { + return userSessionRepository.findOneBySessionId(sessionId).getUser(); + } } diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 65e0be6..4f9ec12 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -61,8 +61,14 @@ - diff --git a/src/main/resources/templates/users/profile.html b/src/main/resources/templates/users/profile.html new file mode 100644 index 0000000..5468d1b --- /dev/null +++ b/src/main/resources/templates/users/profile.html @@ -0,0 +1,75 @@ + + + + + + + + +
+
+
+
+
+

Личный кабинет

+
+
+
+
+
+
+ +
+ + +

Incorrect firstName

+

+
+
+ + +

Incorrect lastName

+

+
+
+ + +

Incorrect email

+

+
+
+ + +

Incorrect login

+

+
+
+ +
+
+
+
+
+
+
+ + From 11ef644e5e8e5c2b157ee937341f4a4aa8c4ef04 Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Wed, 15 May 2019 02:48:44 +0400 Subject: [PATCH 02/14] 88 inviting user base --- .../ru/ulstu/configuration/Constants.java | 3 + .../user/controller/UserControllerV2.java | 127 +----------------- .../ru/ulstu/user/service/MailService.java | 14 ++ .../ru/ulstu/user/service/UserService.java | 32 ++++- .../user/service/UserSessionService.java | 2 +- .../java/ru/ulstu/user/util/UserUtils.java | 5 + .../mail_templates/userInviteEmail.html | 21 +++ src/main/resources/templates/default.html | 7 +- .../templates/users/inviteModal.html | 32 +++++ .../resources/templates/users/profile.html | 9 -- 10 files changed, 114 insertions(+), 138 deletions(-) create mode 100644 src/main/resources/mail_templates/userInviteEmail.html create mode 100644 src/main/resources/templates/users/inviteModal.html diff --git a/src/main/java/ru/ulstu/configuration/Constants.java b/src/main/java/ru/ulstu/configuration/Constants.java index 0a2268a..51806b6 100644 --- a/src/main/java/ru/ulstu/configuration/Constants.java +++ b/src/main/java/ru/ulstu/configuration/Constants.java @@ -5,7 +5,10 @@ public class Constants { public static final String MAIL_ACTIVATE = "Account activation"; public static final String MAIL_RESET = "Password reset"; + public static final String MAIL_INVITE = "Account registration"; + public static final int MIN_PASSWORD_LENGTH = 6; + public static final int MAX_PASSWORD_LENGTH = 32; public static final String LOGIN_REGEX = "^[_'.@A-Za-z0-9-]*$"; diff --git a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java index f15872e..3447a49 100644 --- a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java +++ b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java @@ -1,36 +1,21 @@ package ru.ulstu.user.controller; -import com.sun.org.apache.xpath.internal.operations.Mod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.security.access.annotation.Secured; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; -import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; import ru.ulstu.configuration.Constants; -import ru.ulstu.core.model.response.PageableItems; -import ru.ulstu.core.model.response.Response; import ru.ulstu.odin.controller.OdinController; -import ru.ulstu.odin.model.OdinMetadata; -import ru.ulstu.odin.model.OdinVoid; -import ru.ulstu.odin.service.OdinService; import ru.ulstu.user.model.*; import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserSessionService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; -import javax.validation.Valid; - -import static ru.ulstu.user.controller.UserController.URL; @Controller @RequestMapping(value = "/users") @@ -49,130 +34,32 @@ public class UserControllerV2 extends OdinController { private final UserService userService; private final UserSessionService userSessionService; - private final OdinService odinRolesService; - private final OdinService odinSessionsService; public UserControllerV2(UserService userService, - UserSessionService userSessionService, - OdinService odinRolesService, - OdinService odinSessionsService) { + UserSessionService userSessionService) { super(UserListDto.class, UserDto.class); this.userService = userService; this.userSessionService = userSessionService; - this.odinRolesService = odinRolesService; - this.odinSessionsService = odinSessionsService; } @GetMapping("/profile") public void getUserProfile(ModelMap modelMap, HttpServletRequest request) { HttpSession session = request.getSession(false); final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); - modelMap.addAttribute("userDto", new UserDto(userSessionService.getUserDtoBySessionId(sessionId))); + modelMap.addAttribute("userDto", new UserDto(userSessionService.getUserBySessionId(sessionId))); } @PostMapping("/profile") public void updateUserProfile(ModelMap modelMap, HttpServletRequest request, UserDto userDto) { HttpSession session = request.getSession(false); final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); - User user = userSessionService.getUserDtoBySessionId(sessionId); + User user = userSessionService.getUserBySessionId(sessionId); modelMap.addAttribute("userDto", userService.updateUserInformation(user, userDto)); } - @GetMapping(ROLES_URL) - @Secured(UserRoleConstants.ADMIN) - public Response> getUserRoles() { - log.debug("REST: UserController.getUserRoles()"); - return new Response<>(userService.getUserRoles()); - } - - @GetMapping(ROLES_META_URL) - @Secured(UserRoleConstants.ADMIN) - public Response getUserRolesMetaData() { - log.debug("REST: UserController.getUserRolesMetaData()"); - return new Response<>(odinRolesService.getListModel(UserRoleDto.class)); - } - - @GetMapping(SESSIONS_URL) - @Secured(UserRoleConstants.ADMIN) - public Response> getUserSessions(@RequestParam(value = "offset", defaultValue = "0") int offset, - @RequestParam(value = "count", defaultValue = "10") int count) { - log.debug("REST: UserController.getUserSessions()"); - return new Response<>(userSessionService.getSessions(offset, count)); - } - - @GetMapping(SESSIONS_META_URL) - @Secured(UserRoleConstants.ADMIN) - public Response getUserSessionsMetaData() { - log.debug("REST: UserController.getUserSessionsMetaData()"); - return new Response<>(odinSessionsService.getListModel(UserSessionListDto.class)); - } - - @GetMapping("") - @Secured(UserRoleConstants.ADMIN) - public Response> getAllUsers(@RequestParam(value = "offset", defaultValue = "0") int offset, - @RequestParam(value = "count", defaultValue = "10") int count) { - log.debug("REST: UserController.getAllUsers( {}, {} )", offset, count); - return new Response<>(userService.getAllUsers(offset, count)); - } - - @GetMapping("/{userId}") - @Secured(UserRoleConstants.ADMIN) - public Response getUser(@PathVariable Integer userId) { - log.debug("REST: UserController.getUser( {} )", userId); - return new Response<>(userService.getUserWithRolesById(userId)); - } - - - @PostMapping("") - @Secured(UserRoleConstants.ADMIN) - public Response createUser(@Valid @RequestBody UserDto userDto) { - log.debug("REST: UserController.createUser( {} )", userDto.getLogin()); - return new Response<>(userService.createUser(userDto)); - } - - @PutMapping("") - @Secured(UserRoleConstants.ADMIN) - public Response updateUser(@Valid @RequestBody UserDto userDto) { - log.debug("REST: UserController.updateUser( {} )", userDto.getLogin()); - return new Response<>(userService.updateUser(userDto)); - } - - @DeleteMapping("/{userId}") - @Secured(UserRoleConstants.ADMIN) - public Response deleteUser(@PathVariable Integer userId) { - log.debug("REST: UserController.deleteUser( {} )", userId); - return new Response<>(userService.deleteUser(userId)); - } - - @PostMapping(REGISTER_URL) - public Response registerUser(@Valid @RequestBody UserDto userDto) { - log.debug("REST: UserController.registerUser( {} )", userDto.getLogin()); - return new Response<>(userService.createUser(userDto)); - } - - @PostMapping(ACTIVATE_URL) - public Response activateUser(@RequestParam("key") String activationKey) { - log.debug("REST: UserController.activateUser( {} )", activationKey); - return new Response<>(userService.activateUser(activationKey)); - } - - // TODO: add page for user password change (user-profile) - @PostMapping("/change-password") - public Response changePassword(@Valid @RequestBody UserDto userDto) { - log.debug("REST: UserController.changePassword( {} )", userDto.getLogin()); - return new Response<>(userService.changeUserPassword(userDto)); - } - - @PostMapping(PASSWORD_RESET_REQUEST_URL) - public Response requestPasswordReset(@RequestParam("email") String email) { - log.debug("REST: UserController.requestPasswordReset( {} )", email); - return new Response<>(userService.requestUserPasswordReset(email)); - } - - @PostMapping(PASSWORD_RESET_URL) - public Response finishPasswordReset(@RequestParam("key") String key, - @RequestBody UserResetPasswordDto userResetPasswordDto) { - log.debug("REST: UserController.requestPasswordReset( {} )", key); - return new Response<>(userService.completeUserPasswordReset(key, userResetPasswordDto)); + @PostMapping("/invite" ) + public String inviteUser(@RequestParam(value = "email") String email){ + userService.inviteUser(email); + return "redirect:/"; } } diff --git a/src/main/java/ru/ulstu/user/service/MailService.java b/src/main/java/ru/ulstu/user/service/MailService.java index da1da6d..da8e098 100644 --- a/src/main/java/ru/ulstu/user/service/MailService.java +++ b/src/main/java/ru/ulstu/user/service/MailService.java @@ -78,6 +78,15 @@ public class MailService { sendEmail(user.getEmail(), subject, content); } + @Async + public void sendEmailFromTemplate(Map variables, String templateName, String subject, String email) { + Context context = new Context(); + variables.entrySet().forEach(entry -> context.setVariable(entry.getKey(), entry.getValue())); + context.setVariable(BASE_URL, applicationProperties.getBaseUrl()); + String content = templateEngine.process(templateName, context); + sendEmail(email, subject, content); + } + @Async public void sendActivationEmail(User user) { sendEmailFromTemplate(user, "activationEmail", Constants.MAIL_ACTIVATE); @@ -87,4 +96,9 @@ public class MailService { public void sendPasswordResetMail(User user) { sendEmailFromTemplate(user, "passwordResetEmail", Constants.MAIL_RESET); } + + @Async + public void sendInviteMail(Map variables, String email) { + sendEmailFromTemplate(variables, "userInviteEmail", Constants.MAIL_INVITE, email); + } } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index 202cf10..ef11796 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -1,5 +1,6 @@ package ru.ulstu.user.service; +import com.google.common.collect.ImmutableMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; @@ -12,6 +13,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import ru.ulstu.configuration.ApplicationProperties; +import ru.ulstu.configuration.Constants; import ru.ulstu.core.error.EntityIdIsNullException; import ru.ulstu.core.jpa.OffsetablePageRequest; import ru.ulstu.core.model.BaseEntity; @@ -36,13 +38,7 @@ import ru.ulstu.user.repository.UserRepository; import ru.ulstu.user.repository.UserRoleRepository; import ru.ulstu.user.util.UserUtils; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; @Service @@ -55,6 +51,7 @@ public class UserService implements UserDetailsService { private final UserMapper userMapper; private final MailService mailService; private final ApplicationProperties applicationProperties; + private final Random random; public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, @@ -68,6 +65,7 @@ public class UserService implements UserDetailsService { this.userMapper = userMapper; this.mailService = mailService; this.applicationProperties = applicationProperties; + this.random = new Random(); } private User getUserByEmail(String email) { @@ -315,4 +313,24 @@ public class UserService implements UserDetailsService { public List filterByAgeAndDegree(boolean hasDegree, boolean hasAge) { return userRepository.filterByAgeAndDegree(hasDegree, hasAge); } + + public void inviteUser(String email) { + if (userRepository.findOneByEmailIgnoreCase(email) != null) { + throw new UserEmailExistsException(email); + } + + String password = UserUtils.generatePassword(); + + User user = new User(); + user.setPassword(passwordEncoder.encode(password)); + user.setLogin(email); + user.setEmail(email); + user.setFirstName(""); + user.setLastName(""); + user.setActivated(true); + userRepository.save(user); + + Map variables = ImmutableMap.of("password", password, "email", email); + mailService.sendInviteMail(variables, email); + } } diff --git a/src/main/java/ru/ulstu/user/service/UserSessionService.java b/src/main/java/ru/ulstu/user/service/UserSessionService.java index 03415de..ae289d0 100644 --- a/src/main/java/ru/ulstu/user/service/UserSessionService.java +++ b/src/main/java/ru/ulstu/user/service/UserSessionService.java @@ -55,7 +55,7 @@ public class UserSessionService { log.debug("User session {} closed", sessionId); } - public User getUserDtoBySessionId(String sessionId) { + public User getUserBySessionId(String sessionId) { return userSessionRepository.findOneBySessionId(sessionId).getUser(); } } diff --git a/src/main/java/ru/ulstu/user/util/UserUtils.java b/src/main/java/ru/ulstu/user/util/UserUtils.java index de585a5..ec58dd9 100644 --- a/src/main/java/ru/ulstu/user/util/UserUtils.java +++ b/src/main/java/ru/ulstu/user/util/UserUtils.java @@ -5,6 +5,7 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; +import ru.ulstu.configuration.Constants; public class UserUtils { private static final int DEF_COUNT = 20; @@ -32,4 +33,8 @@ public class UserUtils { } return null; } + + public static String generatePassword() { + return RandomStringUtils.randomAscii(Constants.MIN_PASSWORD_LENGTH, Constants.MAX_PASSWORD_LENGTH); + } } diff --git a/src/main/resources/mail_templates/userInviteEmail.html b/src/main/resources/mail_templates/userInviteEmail.html new file mode 100644 index 0000000..180f0c5 --- /dev/null +++ b/src/main/resources/mail_templates/userInviteEmail.html @@ -0,0 +1,21 @@ + + + + Account activation + + + + +

+ Аккаунт в системе NG-Tracker был создан. + Данные для входа: + Логин - + Пароль - +

+

+ Regards, +
+ Balance Team. +

+ + diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 4f9ec12..0c92865 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -62,18 +62,21 @@ КИАС РФФИ +
-
- - -

Incorrect login

-

-
@@ -77,6 +78,7 @@
+
+
+ + +

Incorrect email

+

+