From 48d7131593ffaa064f2b05f04da4ec6a847dfb16 Mon Sep 17 00:00:00 2001 From: arefiev1997 Date: Mon, 13 May 2019 03:16:37 +0400 Subject: [PATCH] gl_88 inviting user --- build.gradle | 1 + .../ulstu/user/controller/UserController.java | 6 ++ .../ru/ulstu/user/service/MailService.java | 5 ++ .../ru/ulstu/user/service/UserService.java | 28 +++++++--- .../java/ru/ulstu/user/util/UserUtils.java | 12 ++++ src/main/resources/application.properties | 2 +- .../resources/mail_templates/inviteUser.html | 21 +++++++ src/main/resources/templates/default.html | 55 ++++++++++++++----- src/main/resources/templates/invite.html | 15 +++++ 9 files changed, 124 insertions(+), 21 deletions(-) create mode 100644 src/main/resources/mail_templates/inviteUser.html create mode 100644 src/main/resources/templates/invite.html diff --git a/build.gradle b/build.gradle index 0edeb92..15af8bd 100644 --- a/build.gradle +++ b/build.gradle @@ -125,6 +125,7 @@ dependencies { compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.6.0' compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.6.0' + compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0' testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test' testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.3.1' diff --git a/src/main/java/ru/ulstu/user/controller/UserController.java b/src/main/java/ru/ulstu/user/controller/UserController.java index f88b8fa..c327eed 100644 --- a/src/main/java/ru/ulstu/user/controller/UserController.java +++ b/src/main/java/ru/ulstu/user/controller/UserController.java @@ -158,4 +158,10 @@ public class UserController extends OdinController { log.debug("REST: UserController.requestPasswordReset( {} )", key); return new Response<>(userService.completeUserPasswordReset(key, userResetPasswordDto)); } + + @PostMapping("invite") + public Response inviteUser(@RequestParam("email") String email) { + log.debug("REST: UserController.inviteUser( {} )", email); + return new Response<>(userService.inviteUser(email)); + } } diff --git a/src/main/java/ru/ulstu/user/service/MailService.java b/src/main/java/ru/ulstu/user/service/MailService.java index 77be5e7..61ed3ca 100644 --- a/src/main/java/ru/ulstu/user/service/MailService.java +++ b/src/main/java/ru/ulstu/user/service/MailService.java @@ -85,4 +85,9 @@ public class MailService { public void sendPasswordResetMail(User user) { sendEmailFromTemplate(user, "passwordResetEmail", Constants.MAIL_RESET); } + + @Async + public void sendInviteEmail(User user, Map variables) { + sendEmailFromTemplate(variables, user, "inviteUser", Constants.MAIL_ACTIVATE); + } } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index f92d2e3..8f707ae 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; @@ -37,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 @@ -329,4 +324,23 @@ public class UserService implements UserDetailsService { public List filterByAgeAndDegree(boolean hasDegree, boolean hasAge) { return userRepository.filterByAgeAndDegree(hasDegree, hasAge); } + + public boolean inviteUser(String email) { + if (userRepository.findOneByEmailIgnoreCase(email) != null) { + throw new UserEmailExistsException(email); + } + + String password = UserUtils.generatePassword(6); + User user = new User(); + user.setLogin(email); + user.setPassword(passwordEncoder.encode(password)); + user.setActivated(true); + user.setFirstName("USER"); + user.setLastName("USER"); + user.setEmail(email); + userRepository.save(user); + Map variables = ImmutableMap.of("password", password); + mailService.sendInviteEmail(user, variables ); + return true; + } } diff --git a/src/main/java/ru/ulstu/user/util/UserUtils.java b/src/main/java/ru/ulstu/user/util/UserUtils.java index 8524c74..ef0b1fa 100644 --- a/src/main/java/ru/ulstu/user/util/UserUtils.java +++ b/src/main/java/ru/ulstu/user/util/UserUtils.java @@ -6,6 +6,14 @@ import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; +import java.security.SecureRandom; +import java.util.Collections; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + public class UserUtils { private static final int DEF_COUNT = 20; @@ -17,6 +25,10 @@ public class UserUtils { return RandomStringUtils.randomNumeric(DEF_COUNT); } + public static String generatePassword(int length) { + return RandomStringUtils.randomAscii(length); + } + public static String getCurrentUserLogin(SecurityContext securityContext) { if (securityContext == null) { return null; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 75c8ef9..ffa9df9 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -33,6 +33,6 @@ spring.liquibase.enabled=true # Application Settings ng-tracker.base-url=http://127.0.0.1:8080 ng-tracker.undead-user-login=admin -ng-tracker.dev-mode=true +ng-tracker.dev-mode=false ng-tracker.use-https=false ng-tracker.check-run=false \ No newline at end of file diff --git a/src/main/resources/mail_templates/inviteUser.html b/src/main/resources/mail_templates/inviteUser.html new file mode 100644 index 0000000..68d359a --- /dev/null +++ b/src/main/resources/mail_templates/inviteUser.html @@ -0,0 +1,21 @@ + + + + Account activation + + + + +

+ Your account has been created, please click on the URL below to activate it: +

+

+ Your login details are: > +

+

+ Regards, +
+ Balance Team. +

+ + diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 9605631..b179738 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -55,24 +55,46 @@ Сайт кафедры +