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