Make all new users users by default

master
esensar 2018-01-17 22:12:22 +01:00
parent a0db1a1631
commit 7df33fd942
3 changed files with 17 additions and 1 deletions

View File

@ -28,7 +28,7 @@ dependencies {
compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.cloud:spring-cloud-starter-eureka') compile('org.springframework.cloud:spring-cloud-starter-eureka')
testCompile('org.springframework.cloud:spring-cloud-starter-eureka-server') 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 { dependencyManagement {

View File

@ -2,14 +2,17 @@ package ba.steleks.security;
import ba.steleks.AutowireHelper; import ba.steleks.AutowireHelper;
import ba.steleks.model.User; import ba.steleks.model.User;
import ba.steleks.repository.UserRolesJpaRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport; import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import javax.persistence.PostPersist;
import javax.persistence.PrePersist; import javax.persistence.PrePersist;
import javax.persistence.PreUpdate; import javax.persistence.PreUpdate;
import java.util.HashSet;
/** /**
* Created by ensar on 30/05/17. * Created by ensar on 30/05/17.
@ -21,6 +24,9 @@ public class UserPasswordEntityListener {
@Autowired @Autowired
private PasswordEncoder passwordEncoder; private PasswordEncoder passwordEncoder;
@Autowired
private UserRolesJpaRepository userRolesJpaRepository;
@PrePersist @PrePersist
@PreUpdate @PreUpdate
public void onUserUpdate(User user) { 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));
}
} }

View File

@ -39,6 +39,7 @@ public class UserRoleFactory {
.stream() .stream()
// get role name // get role name
.map(UserRole::getRoleName) .map(UserRole::getRoleName)
.map(role -> "ROLE_" + role)
// create authority // create authority
.map(SimpleGrantedAuthority::new) .map(SimpleGrantedAuthority::new)
.collect(Collectors.toList()); .collect(Collectors.toList());