use of org.baeldung.rolesauthorities.model.Privilege in project tutorials by eugenp.
the class SetupDataLoader method createPrivilegeIfNotFound.
@Transactional
private Privilege createPrivilegeIfNotFound(String name) {
Privilege privilege = privilegeRepository.findByName(name);
if (privilege == null) {
privilege = new Privilege(name);
privilegeRepository.save(privilege);
}
return privilege;
}
use of org.baeldung.rolesauthorities.model.Privilege in project tutorials by eugenp.
the class SetupDataLoader method onApplicationEvent.
@Override
@Transactional
public void onApplicationEvent(ContextRefreshedEvent event) {
if (alreadySetup) {
return;
}
// == create initial privileges
Privilege readPrivilege = createPrivilegeIfNotFound("READ_PRIVILEGE");
Privilege writePrivilege = createPrivilegeIfNotFound("WRITE_PRIVILEGE");
// == create initial roles
List<Privilege> adminPrivileges = Arrays.asList(readPrivilege, writePrivilege);
createRoleIfNotFound("ROLE_ADMIN", adminPrivileges);
List<Privilege> rolePrivileges = new ArrayList<>();
createRoleIfNotFound("ROLE_USER", rolePrivileges);
Role adminRole = roleRepository.findByName("ROLE_ADMIN");
User user = new User();
user.setFirstName("Admin");
user.setLastName("Admin");
user.setEmail("admin@test.com");
user.setPassword(passwordEncoder.encode("admin"));
user.setRoles(Arrays.asList(adminRole));
user.setEnabled(true);
userRepository.save(user);
Role basicRole = roleRepository.findByName("ROLE_USER");
User basicUser = new User();
basicUser.setFirstName("User");
basicUser.setLastName("User");
basicUser.setEmail("user@test.com");
basicUser.setPassword(passwordEncoder.encode("user"));
basicUser.setRoles(Arrays.asList(basicRole));
basicUser.setEnabled(true);
userRepository.save(basicUser);
alreadySetup = true;
}
Aggregations