minor flag rename
This commit is contained in:
parent
9133c66d63
commit
31b5fcf9e9
|
@ -1,6 +1,8 @@
|
||||||
package org.baeldung.spring;
|
package org.baeldung.spring;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.baeldung.persistence.dao.PrivilegeRepository;
|
import org.baeldung.persistence.dao.PrivilegeRepository;
|
||||||
import org.baeldung.persistence.dao.RoleRepository;
|
import org.baeldung.persistence.dao.RoleRepository;
|
||||||
|
@ -11,7 +13,6 @@ import org.baeldung.persistence.model.User;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.context.event.ContextRefreshedEvent;
|
import org.springframework.context.event.ContextRefreshedEvent;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
||||||
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.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
@ -19,7 +20,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
@Component
|
@Component
|
||||||
public class InitialDataLoader implements ApplicationListener<ContextRefreshedEvent> {
|
public class InitialDataLoader implements ApplicationListener<ContextRefreshedEvent> {
|
||||||
|
|
||||||
boolean alreadyExist = false;
|
boolean alreadySetup = false;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
|
@ -30,10 +31,13 @@ public class InitialDataLoader implements ApplicationListener<ContextRefreshedEv
|
||||||
@Autowired
|
@Autowired
|
||||||
private PrivilegeRepository privilegeRepository;
|
private PrivilegeRepository privilegeRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void onApplicationEvent(final ContextRefreshedEvent event) {
|
public void onApplicationEvent(final ContextRefreshedEvent event) {
|
||||||
if (alreadyExist)
|
if (alreadySetup)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// == create initial privileges
|
// == create initial privileges
|
||||||
|
@ -41,28 +45,25 @@ public class InitialDataLoader implements ApplicationListener<ContextRefreshedEv
|
||||||
final Privilege writePrivilege = createPrivilegeIfNotFound("WRITE_PRIVILEGE");
|
final Privilege writePrivilege = createPrivilegeIfNotFound("WRITE_PRIVILEGE");
|
||||||
|
|
||||||
// == create initial roles
|
// == create initial roles
|
||||||
final Role admin = createRoleIfNotFound("ROLE_ADMIN");
|
final List<Privilege> adminPrivileges = Arrays.asList(readPrivilege, writePrivilege);
|
||||||
final Role userRole = createRoleIfNotFound("ROLE_USER");
|
createRoleIfNotFound("ROLE_ADMIN", adminPrivileges);
|
||||||
|
createRoleIfNotFound("ROLE_USER", Arrays.asList(readPrivilege));
|
||||||
|
|
||||||
// == link roles and privileges
|
final Role adminRole = roleRepository.findByName("ROLE_ADMIN");
|
||||||
admin.setPrivileges(Arrays.asList(readPrivilege, writePrivilege));
|
final User user = new User();
|
||||||
userRole.setPrivileges(Arrays.asList(readPrivilege));
|
|
||||||
|
|
||||||
User user = new User();
|
|
||||||
user.setFirstName("Test");
|
user.setFirstName("Test");
|
||||||
user.setLastName("Test");
|
user.setLastName("Test");
|
||||||
PasswordEncoder encoder = new BCryptPasswordEncoder();
|
user.setPassword(passwordEncoder.encode("test"));
|
||||||
user.setPassword(encoder.encode("test"));
|
|
||||||
user.setEmail("test@test.com");
|
user.setEmail("test@test.com");
|
||||||
user.setRoles(Arrays.asList(admin));
|
user.setRoles(Arrays.asList(adminRole));
|
||||||
user.setEnabled(true);
|
user.setEnabled(true);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
alreadyExist = true;
|
alreadySetup = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
private final Privilege createPrivilegeIfNotFound(String name) {
|
private final Privilege createPrivilegeIfNotFound(final String name) {
|
||||||
Privilege privilege = privilegeRepository.findByName(name);
|
Privilege privilege = privilegeRepository.findByName(name);
|
||||||
if (privilege == null) {
|
if (privilege == null) {
|
||||||
privilege = new Privilege(name);
|
privilege = new Privilege(name);
|
||||||
|
@ -72,10 +73,11 @@ public class InitialDataLoader implements ApplicationListener<ContextRefreshedEv
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
private final Role createRoleIfNotFound(String name) {
|
private final Role createRoleIfNotFound(final String name, final Collection<Privilege> privileges) {
|
||||||
Role role = roleRepository.findByName(name);
|
Role role = roleRepository.findByName(name);
|
||||||
if (role == null) {
|
if (role == null) {
|
||||||
role = new Role(name);
|
role = new Role(name);
|
||||||
|
role.setPrivileges(privileges);
|
||||||
roleRepository.save(role);
|
roleRepository.save(role);
|
||||||
}
|
}
|
||||||
return role;
|
return role;
|
||||||
|
|
Loading…
Reference in New Issue