From 7df33fd942807d27f24791a3f22b6082e5ab1633 Mon Sep 17 00:00:00 2001 From: esensar Date: Wed, 17 Jan 2018 22:12:22 +0100 Subject: [PATCH] Make all new users users by default --- common/build.gradle | 2 +- .../security/UserPasswordEntityListener.java | 15 +++++++++++++++ .../java/ba/steleks/security/UserRoleFactory.java | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/common/build.gradle b/common/build.gradle index 0ecb6b8..3310289 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -28,7 +28,7 @@ dependencies { compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.cloud:spring-cloud-starter-eureka') testCompile('org.springframework.cloud:spring-cloud-starter-eureka-server') - testCompile('rg.springframework.boot:spring-boot-starter-test') + testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { diff --git a/users/src/main/java/ba/steleks/security/UserPasswordEntityListener.java b/users/src/main/java/ba/steleks/security/UserPasswordEntityListener.java index 3b80734..005c6de 100644 --- a/users/src/main/java/ba/steleks/security/UserPasswordEntityListener.java +++ b/users/src/main/java/ba/steleks/security/UserPasswordEntityListener.java @@ -2,14 +2,17 @@ package ba.steleks.security; import ba.steleks.AutowireHelper; import ba.steleks.model.User; +import ba.steleks.repository.UserRolesJpaRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Component; import org.springframework.web.context.support.SpringBeanAutowiringSupport; +import javax.persistence.PostPersist; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; +import java.util.HashSet; /** * Created by ensar on 30/05/17. @@ -21,6 +24,9 @@ public class UserPasswordEntityListener { @Autowired private PasswordEncoder passwordEncoder; + @Autowired + private UserRolesJpaRepository userRolesJpaRepository; + @PrePersist @PreUpdate public void onUserUpdate(User user) { @@ -30,4 +36,13 @@ public class UserPasswordEntityListener { } } + @PostPersist + public void postUserCreated(User user) { + AutowireHelper.autowire(this, userRolesJpaRepository); + if (user.getUserRoles() == null) { + user.setUserRoles(new HashSet<>()); + } + user.getUserRoles().add(userRolesJpaRepository.findOne(1L)); + } + } diff --git a/users/src/main/java/ba/steleks/security/UserRoleFactory.java b/users/src/main/java/ba/steleks/security/UserRoleFactory.java index 976ce44..77b90bb 100644 --- a/users/src/main/java/ba/steleks/security/UserRoleFactory.java +++ b/users/src/main/java/ba/steleks/security/UserRoleFactory.java @@ -39,6 +39,7 @@ public class UserRoleFactory { .stream() // get role name .map(UserRole::getRoleName) + .map(role -> "ROLE_" + role) // create authority .map(SimpleGrantedAuthority::new) .collect(Collectors.toList());