Merge branch '87-inviting-user' into 88-change-password

This commit is contained in:
Artem.Arefev 2019-05-15 22:35:30 +04:00
commit ca3159a66b
12 changed files with 287 additions and 44 deletions

View File

@ -5,7 +5,10 @@ public class Constants {
public static final String MAIL_ACTIVATE = "Account activation"; public static final String MAIL_ACTIVATE = "Account activation";
public static final String MAIL_RESET = "Password reset"; 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 MIN_PASSWORD_LENGTH = 6;
public static final int MAX_PASSWORD_LENGTH = 32;
public static final String LOGIN_REGEX = "^[_'.@A-Za-z0-9-]*$"; public static final String LOGIN_REGEX = "^[_'.@A-Za-z0-9-]*$";

View File

@ -141,13 +141,6 @@ public class UserController extends OdinController<UserListDto, UserDto> {
return new Response<>(userService.activateUser(activationKey)); return new Response<>(userService.activateUser(activationKey));
} }
// TODO: add page for user edit (user-profile)
@PostMapping("/change-information")
public Response<UserDto> 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) // TODO: add page for user password change (user-profile)
@PostMapping("/change-password") @PostMapping("/change-password")
public Response<UserDto> changePassword(@Valid @RequestBody UserDto userDto) { public Response<UserDto> changePassword(@Valid @RequestBody UserDto userDto) {

View File

@ -0,0 +1,59 @@
package ru.ulstu.user.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.configuration.Constants;
import ru.ulstu.odin.controller.OdinController;
import ru.ulstu.user.error.UserSendingMailException;
import ru.ulstu.user.model.UserDto;
import ru.ulstu.user.model.User;
import ru.ulstu.user.model.UserListDto;
import ru.ulstu.user.service.UserService;
import ru.ulstu.user.service.UserSessionService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping(value = "/users")
public class UserControllerV2 extends OdinController<UserListDto, UserDto> {
private final Logger log = LoggerFactory.getLogger(UserControllerV2.class);
private final UserService userService;
private final UserSessionService userSessionService;
public UserControllerV2(UserService userService,
UserSessionService userSessionService) {
super(UserListDto.class, UserDto.class);
this.userService = userService;
this.userSessionService = userSessionService;
}
@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.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.getUserBySessionId(sessionId);
modelMap.addAttribute("userDto", userService.updateUserInformation(user, userDto));
}
@PostMapping("/invite")
public String inviteUser(@RequestParam(value = "email") String email) throws UserSendingMailException {
userService.inviteUser(email);
return "redirect:/";
}
}

View File

@ -0,0 +1,7 @@
package ru.ulstu.user.error;
public class UserSendingMailException extends RuntimeException {
public UserSendingMailException(String message) {
super(message);
}
}

View File

@ -3,6 +3,7 @@ package ru.ulstu.user.service;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.mail.MailProperties; import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
@ -13,6 +14,7 @@ import ru.ulstu.configuration.ApplicationProperties;
import ru.ulstu.configuration.Constants; import ru.ulstu.configuration.Constants;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
@ -38,10 +40,9 @@ public class MailService {
} }
@Async @Async
public void sendEmail(String to, String subject, String content) { public void sendEmail(String to, String subject, String content) throws MessagingException {
log.debug("Send email to '{}' with subject '{}'", to, subject); log.debug("Send email to '{}' with subject '{}'", to, subject);
MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.name()); MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.name());
message.setTo(to); message.setTo(to);
message.setFrom(mailProperties.getUsername()); message.setFrom(mailProperties.getUsername());
@ -49,13 +50,6 @@ public class MailService {
message.setText(content, true); message.setText(content, true);
javaMailSender.send(mimeMessage); javaMailSender.send(mimeMessage);
log.debug("Sent email to User '{}'", to); log.debug("Sent email to User '{}'", to);
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.warn("Email could not be sent to user '{}'", to, e);
} else {
log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
}
}
} }
@Async @Async
@ -64,7 +58,17 @@ public class MailService {
context.setVariable(USER, user); context.setVariable(USER, user);
context.setVariable(BASE_URL, applicationProperties.getBaseUrl()); context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
String content = templateEngine.process(templateName, context); String content = templateEngine.process(templateName, context);
try {
sendEmail(user.getEmail(), subject, content); sendEmail(user.getEmail(), subject, content);
} catch (
Exception e) {
if (log.isDebugEnabled()) {
log.warn("Email could not be sent to user '{}'", user.getEmail(), e);
} else {
log.warn("Email could not be sent to user '{}': {}", user.getEmail(), e.getMessage());
}
}
} }
//Todo: выделить сервис нотификаций //Todo: выделить сервис нотификаций
@ -75,7 +79,26 @@ public class MailService {
context.setVariable(USER, user); context.setVariable(USER, user);
context.setVariable(BASE_URL, applicationProperties.getBaseUrl()); context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
String content = templateEngine.process(templateName, context); String content = templateEngine.process(templateName, context);
try {
sendEmail(user.getEmail(), subject, content); sendEmail(user.getEmail(), subject, content);
} catch (
Exception e) {
if (log.isDebugEnabled()) {
log.warn("Email could not be sent to user '{}'", user.getEmail(), e);
} else {
log.warn("Email could not be sent to user '{}': {}", user.getEmail(), e.getMessage());
}
}
}
@Async
public void sendEmailFromTemplate(Map<String, Object> variables, String templateName, String subject, String email)
throws MessagingException {
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 @Async
@ -87,4 +110,8 @@ public class MailService {
public void sendPasswordResetMail(User user) { public void sendPasswordResetMail(User user) {
sendEmailFromTemplate(user, "passwordResetEmail", Constants.MAIL_RESET); sendEmailFromTemplate(user, "passwordResetEmail", Constants.MAIL_RESET);
} }
public void sendInviteMail(Map<String, Object> variables, String email) throws MessagingException {
sendEmailFromTemplate(variables, "userInviteEmail", Constants.MAIL_INVITE, email);
}
} }

View File

@ -1,5 +1,6 @@
package ru.ulstu.user.service; package ru.ulstu.user.service;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@ -25,6 +26,7 @@ import ru.ulstu.user.error.UserNotActivatedException;
import ru.ulstu.user.error.UserNotFoundException; import ru.ulstu.user.error.UserNotFoundException;
import ru.ulstu.user.error.UserPasswordsNotValidOrNotMatchException; import ru.ulstu.user.error.UserPasswordsNotValidOrNotMatchException;
import ru.ulstu.user.error.UserResetKeyError; import ru.ulstu.user.error.UserResetKeyError;
import ru.ulstu.user.error.UserSendingMailException;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserDto;
import ru.ulstu.user.model.UserListDto; import ru.ulstu.user.model.UserListDto;
@ -36,10 +38,12 @@ import ru.ulstu.user.repository.UserRepository;
import ru.ulstu.user.repository.UserRoleRepository; import ru.ulstu.user.repository.UserRoleRepository;
import ru.ulstu.user.util.UserUtils; import ru.ulstu.user.util.UserUtils;
import javax.mail.MessagingException;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -205,23 +209,10 @@ public class UserService implements UserDetailsService {
return userMapper.userEntityToUserDto(user); return userMapper.userEntityToUserDto(user);
} }
public UserDto updateUserInformation(UserDto userDto) { public UserDto updateUserInformation(User user, UserDto updateUser) {
if (userDto.getId() == null) { user.setFirstName(updateUser.getFirstName());
throw new EntityIdIsNullException(); user.setLastName(updateUser.getLastName());
} user.setEmail(updateUser.getEmail());
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());
user = userRepository.save(user); user = userRepository.save(user);
log.debug("Updated Information for User: {}", user.getLogin()); log.debug("Updated Information for User: {}", user.getLogin());
return userMapper.userEntityToUserDto(user); return userMapper.userEntityToUserDto(user);
@ -328,4 +319,28 @@ public class UserService implements UserDetailsService {
public List<User> filterByAgeAndDegree(boolean hasDegree, boolean hasAge) { public List<User> filterByAgeAndDegree(boolean hasDegree, boolean hasAge) {
return userRepository.filterByAgeAndDegree(hasDegree, hasAge); return userRepository.filterByAgeAndDegree(hasDegree, hasAge);
} }
public void inviteUser(String email) throws UserSendingMailException {
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<String, Object> variables = ImmutableMap.of("password", password, "email", email);
try {
mailService.sendInviteMail(variables, email);
} catch (MessagingException e) {
throw new UserSendingMailException(email);
}
}
} }

View File

@ -54,4 +54,8 @@ public class UserSessionService {
userSessionRepository.save(userSession); userSessionRepository.save(userSession);
log.debug("User session {} closed", sessionId); log.debug("User session {} closed", sessionId);
} }
public User getUserBySessionId(String sessionId) {
return userSessionRepository.findOneBySessionId(sessionId).getUser();
}
} }

View File

@ -5,6 +5,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import ru.ulstu.configuration.Constants;
public class UserUtils { public class UserUtils {
private static final int DEF_COUNT = 20; private static final int DEF_COUNT = 20;
@ -32,4 +33,8 @@ public class UserUtils {
} }
return null; return null;
} }
public static String generatePassword() {
return RandomStringUtils.randomAscii(Constants.MIN_PASSWORD_LENGTH, Constants.MAX_PASSWORD_LENGTH);
}
} }

View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Account activation</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" th:href="@{|${baseUrl}/favicon.ico|}"/>
</head>
<body>
<p>
Аккаунт в системе NG-Tracker был создан.
Данные для входа:
Логин - <span th:text="${email}"></span>
Пароль - <span th:text="${password}"></span>
</p>
<p>
Regards,
<br/>
<em>Balance Team.</em>
</p>
</body>
</html>

View File

@ -61,13 +61,22 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link js-scroll-trigger" target="_blank" href="https://kias.rfbr.ru/">КИАС РФФИ</a> <a class="nav-link js-scroll-trigger" target="_blank" href="https://kias.rfbr.ru/">КИАС РФФИ</a>
</li> </li>
<li class="nav-item"> <li class="nav-item dropdown">
<a class="nav-link js-scroll-trigger" href="/logout">Выход</a> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Профиль
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/users/profile">Личный кабинет</a>
<a class="dropdown-item" href="/logout">Выход</a>
<a class="dropdown-item" data-toggle="modal" href="invite.html" data-target="#inviteModal">Пригласить</a>
</div>
</li> </li>
</ul> </ul>
</div> </div>
</div> </div>
</nav> </nav>
<div th:replace="users/inviteModal"/>
<div class="container-fluid"> <div class="container-fluid">
<div class="container"> <div class="container">
<ul id="messages" class="feedback-panel"> <ul id="messages" class="feedback-panel">
@ -108,6 +117,7 @@
/*]]>*/ /*]]>*/
</script> </script>
<th:block layout:fragment="scripts"> <th:block layout:fragment="scripts">
</th:block> </th:block>
@ -142,6 +152,7 @@
f(); f();
} }
})(document, window, "yandex_metrika_callbacks2"); })(document, window, "yandex_metrika_callbacks2");
</script> </script>
<noscript> <noscript>
<div><img src="https://mc.yandex.ru/watch/49387279" style="position:absolute; left:-9999px;" alt=""/></div> <div><img src="https://mc.yandex.ru/watch/49387279" style="position:absolute; left:-9999px;" alt=""/></div>

View File

@ -0,0 +1,32 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="headerfiles">
<meta charset="UTF-8"/>
</head>
<body>
<div id="inviteModal" class="modal fade text-center">
<div class="modal-dialog">
<div class="modal-content">
<form id="invite-form" method="post" action="/users/invite">
<div class="modal-header">
<h5 class="modal-title" id="label">Пригласить пользователя</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<input class="form-control" id="email" type="text"
placeholder="email" name="email"/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Закрыть</button>
<button type="submit" class="btn btn-primary">Пригласить</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>

View File

@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/html">
<head>
<link rel="stylesheet" href="../css/grant.css"/>
</head>
<body>
<div class="container" layout:fragment="content">
<section id="ewrq">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading text-uppercase">Личный кабинет</h2>
</div>
</div>
<hr/>
<div class="row">
<div class="col-lg-12">
<form id="profile-form" method="post" th:action="@{'/users/profile'}"
th:object="${userDto}">
<input type="hidden" name="id" th:field="*{id}"/>
<div class="form-group">
<label for="firstName">Имя:</label>
<input class="form-control" id="firstName" type="text"
placeholder="Имя"
th:field="*{firstName}"/>
<p th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}"
class="alert alert-danger">Incorrect firstName</p>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<label for="lastName">Фамилия:</label>
<input class="form-control" id="lastName" type="text"
placeholder="lastName"
th:field="*{lastName}"/>
<p th:if="${#fields.hasErrors('lastName')}" th:errors="*{lastName}"
class="alert alert-danger">Incorrect lastName</p>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input class="form-control" id="email" type="text"
placeholder="Email"
th:field="*{email}"/>
<p th:if="${#fields.hasErrors('email')}" th:errors="*{email}"
class="alert alert-danger">Incorrect email</p>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<button id="sendMessageButton" name="save"
class="btn btn-success text-uppercase"
type="submit">
Сохранить
</button>
</div>
</form>
</div>
</div>
</div>
</section>
</div>
</body>
</html>