88 inviting user base
This commit is contained in:
parent
3d02f551cc
commit
11ef644e5e
@ -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-]*$";
|
||||
|
||||
|
@ -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<UserListDto, UserDto> {
|
||||
|
||||
private final UserService userService;
|
||||
private final UserSessionService userSessionService;
|
||||
private final OdinService<UserRoleDto, UserRoleDto> odinRolesService;
|
||||
private final OdinService<UserSessionListDto, OdinVoid> odinSessionsService;
|
||||
|
||||
public UserControllerV2(UserService userService,
|
||||
UserSessionService userSessionService,
|
||||
OdinService<UserRoleDto, UserRoleDto> odinRolesService,
|
||||
OdinService<UserSessionListDto, OdinVoid> 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<PageableItems<UserRoleDto>> getUserRoles() {
|
||||
log.debug("REST: UserController.getUserRoles()");
|
||||
return new Response<>(userService.getUserRoles());
|
||||
}
|
||||
|
||||
@GetMapping(ROLES_META_URL)
|
||||
@Secured(UserRoleConstants.ADMIN)
|
||||
public Response<OdinMetadata> getUserRolesMetaData() {
|
||||
log.debug("REST: UserController.getUserRolesMetaData()");
|
||||
return new Response<>(odinRolesService.getListModel(UserRoleDto.class));
|
||||
}
|
||||
|
||||
@GetMapping(SESSIONS_URL)
|
||||
@Secured(UserRoleConstants.ADMIN)
|
||||
public Response<PageableItems<UserSessionListDto>> 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<OdinMetadata> getUserSessionsMetaData() {
|
||||
log.debug("REST: UserController.getUserSessionsMetaData()");
|
||||
return new Response<>(odinSessionsService.getListModel(UserSessionListDto.class));
|
||||
}
|
||||
|
||||
@GetMapping("")
|
||||
@Secured(UserRoleConstants.ADMIN)
|
||||
public Response<PageableItems<UserListDto>> 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<UserDto> getUser(@PathVariable Integer userId) {
|
||||
log.debug("REST: UserController.getUser( {} )", userId);
|
||||
return new Response<>(userService.getUserWithRolesById(userId));
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("")
|
||||
@Secured(UserRoleConstants.ADMIN)
|
||||
public Response<UserDto> createUser(@Valid @RequestBody UserDto userDto) {
|
||||
log.debug("REST: UserController.createUser( {} )", userDto.getLogin());
|
||||
return new Response<>(userService.createUser(userDto));
|
||||
}
|
||||
|
||||
@PutMapping("")
|
||||
@Secured(UserRoleConstants.ADMIN)
|
||||
public Response<UserDto> 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<UserDto> deleteUser(@PathVariable Integer userId) {
|
||||
log.debug("REST: UserController.deleteUser( {} )", userId);
|
||||
return new Response<>(userService.deleteUser(userId));
|
||||
}
|
||||
|
||||
@PostMapping(REGISTER_URL)
|
||||
public Response<UserDto> registerUser(@Valid @RequestBody UserDto userDto) {
|
||||
log.debug("REST: UserController.registerUser( {} )", userDto.getLogin());
|
||||
return new Response<>(userService.createUser(userDto));
|
||||
}
|
||||
|
||||
@PostMapping(ACTIVATE_URL)
|
||||
public Response<UserDto> 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<UserDto> 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<Boolean> requestPasswordReset(@RequestParam("email") String email) {
|
||||
log.debug("REST: UserController.requestPasswordReset( {} )", email);
|
||||
return new Response<>(userService.requestUserPasswordReset(email));
|
||||
}
|
||||
|
||||
@PostMapping(PASSWORD_RESET_URL)
|
||||
public Response<Boolean> 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:/";
|
||||
}
|
||||
}
|
||||
|
@ -78,6 +78,15 @@ public class MailService {
|
||||
sendEmail(user.getEmail(), subject, content);
|
||||
}
|
||||
|
||||
@Async
|
||||
public void sendEmailFromTemplate(Map<String, Object> 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<String, Object> variables, String email) {
|
||||
sendEmailFromTemplate(variables, "userInviteEmail", Constants.MAIL_INVITE, email);
|
||||
}
|
||||
}
|
||||
|
@ -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<User> 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<String, Object> variables = ImmutableMap.of("password", password, "email", email);
|
||||
mailService.sendInviteMail(variables, email);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
21
src/main/resources/mail_templates/userInviteEmail.html
Normal file
21
src/main/resources/mail_templates/userInviteEmail.html
Normal 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>
|
@ -62,18 +62,21 @@
|
||||
<a class="nav-link js-scroll-trigger" target="_blank" href="https://kias.rfbr.ru/">КИАС РФФИ</a>
|
||||
</li>
|
||||
<li class="nav-item dropdown">
|
||||
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
||||
<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>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div th:replace="users/inviteModal"/>
|
||||
<div class="container-fluid">
|
||||
<div class="container">
|
||||
<ul id="messages" class="feedback-panel">
|
||||
@ -114,6 +117,7 @@
|
||||
/*]]>*/
|
||||
|
||||
|
||||
|
||||
</script>
|
||||
<th:block layout:fragment="scripts">
|
||||
</th:block>
|
||||
@ -148,6 +152,7 @@
|
||||
f();
|
||||
}
|
||||
})(document, window, "yandex_metrika_callbacks2");
|
||||
|
||||
</script>
|
||||
<noscript>
|
||||
<div><img src="https://mc.yandex.ru/watch/49387279" style="position:absolute; left:-9999px;" alt=""/></div>
|
||||
|
32
src/main/resources/templates/users/inviteModal.html
Normal file
32
src/main/resources/templates/users/inviteModal.html
Normal 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">×</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>
|
@ -49,15 +49,6 @@
|
||||
class="alert alert-danger">Incorrect email</p>
|
||||
<p class="help-block text-danger"></p>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="login">login:</label>
|
||||
<input class="form-control" id="login" type="text"
|
||||
placeholder="login"
|
||||
th:field="*{login}"/>
|
||||
<p th:if="${#fields.hasErrors('login')}" th:errors="*{login}"
|
||||
class="alert alert-danger">Incorrect login</p>
|
||||
<p class="help-block text-danger"></p>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button id="sendMessageButton" name="save"
|
||||
class="btn btn-success text-uppercase"
|
||||
|
Loading…
Reference in New Issue
Block a user