gl_88 inviting user

This commit is contained in:
arefiev1997 2019-05-13 03:16:37 +04:00
parent 9abcf7792e
commit 48d7131593
9 changed files with 124 additions and 21 deletions

View File

@ -125,6 +125,7 @@ dependencies {
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.6.0' 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: '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.springframework.boot', name: 'spring-boot-starter-test'
testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.3.1' testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.3.1'

View File

@ -158,4 +158,10 @@ public class UserController extends OdinController<UserListDto, UserDto> {
log.debug("REST: UserController.requestPasswordReset( {} )", key); log.debug("REST: UserController.requestPasswordReset( {} )", key);
return new Response<>(userService.completeUserPasswordReset(key, userResetPasswordDto)); return new Response<>(userService.completeUserPasswordReset(key, userResetPasswordDto));
} }
@PostMapping("invite")
public Response<Boolean> inviteUser(@RequestParam("email") String email) {
log.debug("REST: UserController.inviteUser( {} )", email);
return new Response<>(userService.inviteUser(email));
}
} }

View File

@ -85,4 +85,9 @@ 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);
} }
@Async
public void sendInviteEmail(User user, Map<String, Object> variables) {
sendEmailFromTemplate(variables, user, "inviteUser", Constants.MAIL_ACTIVATE);
}
} }

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;
@ -37,13 +38,7 @@ 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 java.util.Collections; import java.util.*;
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.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
@ -329,4 +324,23 @@ 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 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<String, Object> variables = ImmutableMap.of("password", password);
mailService.sendInviteEmail(user, variables );
return true;
}
} }

View File

@ -6,6 +6,14 @@ 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 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 { public class UserUtils {
private static final int DEF_COUNT = 20; private static final int DEF_COUNT = 20;
@ -17,6 +25,10 @@ public class UserUtils {
return RandomStringUtils.randomNumeric(DEF_COUNT); return RandomStringUtils.randomNumeric(DEF_COUNT);
} }
public static String generatePassword(int length) {
return RandomStringUtils.randomAscii(length);
}
public static String getCurrentUserLogin(SecurityContext securityContext) { public static String getCurrentUserLogin(SecurityContext securityContext) {
if (securityContext == null) { if (securityContext == null) {
return null; return null;

View File

@ -33,6 +33,6 @@ spring.liquibase.enabled=true
# Application Settings # Application Settings
ng-tracker.base-url=http://127.0.0.1:8080 ng-tracker.base-url=http://127.0.0.1:8080
ng-tracker.undead-user-login=admin ng-tracker.undead-user-login=admin
ng-tracker.dev-mode=true ng-tracker.dev-mode=false
ng-tracker.use-https=false ng-tracker.use-https=false
ng-tracker.check-run=false ng-tracker.check-run=false

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>
Your account has been created, please click on the URL below to activate it:
</p>
<p>
Your login details are: <span th:text="${user.email + ' ' + password}"></span>>
</p>
<p>
Regards,
<br/>
<em>Balance Team.</em>
</p>
</body>
</html>

View File

@ -55,24 +55,46 @@
<a class="nav-link js-scroll-trigger" target="_blank" href="http://is.ulstu.ru">Сайт кафедры</a> <a class="nav-link js-scroll-trigger" target="_blank" href="http://is.ulstu.ru">Сайт кафедры</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link js-scroll-trigger" target="_blank" th:href="@{'http://timetable.athene.tech?filter='+${currentUser}}">Расписание</a> <a class="nav-link js-scroll-trigger" target="_blank"
th:href="@{'http://timetable.athene.tech?filter='+${currentUser}}">Расписание</a>
</li> </li>
<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 dropdown"> <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="false">
Профиль Профиль
</a> </a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown"> <div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/profile/profile">Личный кабинет</a> <a class="dropdown-item" href="/profile/profile">Личный кабинет</a>
<a class="dropdown-item" href="/logout">Выход</a> <a class="dropdown-item" href="/logout">Выход</a>
<a class="dropdown-item" data-toggle="modal" href="invite.html" data-target="#exampleModal">Пригласить</a>
</div> </div>
</li> </li>
</ul> </ul>
</div> </div>
</div> </div>
</nav> </nav>
<div id="exampleModal" class="modal fade text-center">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Пригласить пользователя</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<form id="invite-form" method="post" action="/api/1.0/users/invite">
<input class="form-control" id="email" type="text"
placeholder="email" name="email"/>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</form>
</div>
</div>
</div>
<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">
@ -106,33 +128,40 @@
<th:block layout:fragment="scripts"> <th:block layout:fragment="scripts">
</th:block> </th:block>
<!-- Yandex.Metrika counter --> <!-- Yandex.Metrika counter -->
<script type="text/javascript" > <script type="text/javascript">
(function (d, w, c) { (function (d, w, c) {
(w[c] = w[c] || []).push(function() { (w[c] = w[c] || []).push(function () {
try { try {
w.yaCounter49387279 = new Ya.Metrika2({ w.yaCounter49387279 = new Ya.Metrika2({
id:49387279, id: 49387279,
clickmap:true, clickmap: true,
trackLinks:true, trackLinks: true,
accurateTrackBounce:true, accurateTrackBounce: true,
webvisor:true webvisor: true
}); });
} catch(e) { } } catch (e) {
}
}); });
var n = d.getElementsByTagName("script")[0], var n = d.getElementsByTagName("script")[0],
s = d.createElement("script"), s = d.createElement("script"),
f = function () { n.parentNode.insertBefore(s, n); }; f = function () {
n.parentNode.insertBefore(s, n);
};
s.type = "text/javascript"; s.type = "text/javascript";
s.async = true; s.async = true;
s.src = "https://mc.yandex.ru/metrika/tag.js"; s.src = "https://mc.yandex.ru/metrika/tag.js";
if (w.opera == "[object Opera]") { if (w.opera == "[object Opera]") {
d.addEventListener("DOMContentLoaded", f, false); d.addEventListener("DOMContentLoaded", f, false);
} else { f(); } } else {
f();
}
})(document, window, "yandex_metrika_callbacks2"); })(document, window, "yandex_metrika_callbacks2");
</script> </script>
<noscript><div><img src="https://mc.yandex.ru/watch/49387279" style="position:absolute; left:-9999px;" alt="" /></div></noscript> <noscript>
<div><img src="https://mc.yandex.ru/watch/49387279" style="position:absolute; left:-9999px;" alt=""/></div>
</noscript>
<!-- /Yandex.Metrika counter --> <!-- /Yandex.Metrika counter -->
</body> </body>
</html> </html>

View File

@ -0,0 +1,15 @@
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Пригласить пользователя</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<form id="invite-form" method="post" action="@{/api/1.0/users/invite}">
<div class="modal-body">
<input class="form-control" id="email" type="text" placeholder="Email"/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>