Search in sources :

Example 1 with Role

use of org.baeldung.rolesauthorities.model.Role 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;
}
Also used : Role(org.baeldung.rolesauthorities.model.Role) User(org.baeldung.rolesauthorities.model.User) ArrayList(java.util.ArrayList) Privilege(org.baeldung.rolesauthorities.model.Privilege) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Role

use of org.baeldung.rolesauthorities.model.Role in project tutorials by eugenp.

the class SetupDataLoader method createRoleIfNotFound.

@Transactional
private Role createRoleIfNotFound(String name, Collection<Privilege> privileges) {
    Role role = roleRepository.findByName(name);
    if (role == null) {
        role = new Role(name);
        role.setPrivileges(privileges);
        roleRepository.save(role);
    }
    return role;
}
Also used : Role(org.baeldung.rolesauthorities.model.Role) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Role

use of org.baeldung.rolesauthorities.model.Role in project tutorials by eugenp.

the class MyUserDetailsService method getAuthorities.

// UTIL
private final Collection<? extends GrantedAuthority> getAuthorities(Collection<Role> roles) {
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    for (Role role : roles) {
        authorities.add(new SimpleGrantedAuthority(role.getName()));
        authorities.addAll(role.getPrivileges().stream().map(p -> new SimpleGrantedAuthority(p.getName())).collect(Collectors.toList()));
    }
    return authorities;
}
Also used : Role(org.baeldung.rolesauthorities.model.Role) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList)

Aggregations

Role (org.baeldung.rolesauthorities.model.Role)3 ArrayList (java.util.ArrayList)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Privilege (org.baeldung.rolesauthorities.model.Privilege)1 User (org.baeldung.rolesauthorities.model.User)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1