Search in sources :

Example 1 with Authority

use of org.martynas.blogapp.model.Authority in project JavaSpringBoot by jeunefeu.

the class BlogUserServiceImpl method saveNewBlogUser.

@Override
public BlogUser saveNewBlogUser(BlogUser blogUser) throws RoleNotFoundException {
    // for testing debugging purposes
    System.err.println("saveNewBlogUser: " + blogUser);
    // Encode plaintext password with password encoder
    // blogUser.setPassword(PasswordEncoderFactories.createDelegatingPasswordEncoder().encode(blogUser.getPassword()).substring(8));
    // explicit bcrypt encoder so better approach ?
    blogUser.setPassword(this.bcryptEncoder.encode(blogUser.getPassword()));
    // set account to enabled by default
    blogUser.setEnabled(true);
    // Set default Authority/Role to new blog user
    Optional<Authority> optionalAuthority = this.authorityRepository.findByAuthority(DEFAULT_ROLE);
    // for testing debugging purposes
    System.err.println("optionalAuthority: " + optionalAuthority);
    if (optionalAuthority.isPresent()) {
        Authority authority = optionalAuthority.get();
        Collection<Authority> authorities = Collections.singletonList(authority);
        blogUser.setAuthorities(authorities);
        // for testing debugging purposes
        System.err.println("blogUser after Roles: " + blogUser);
        // return blogUserRepository.save(blogUser);
        return this.blogUserRepository.saveAndFlush(blogUser);
    } else {
        throw new RoleNotFoundException("Default role not found for blog user with username " + blogUser.getUsername());
    }
}
Also used : Authority(org.martynas.blogapp.model.Authority) RoleNotFoundException(javax.management.relation.RoleNotFoundException)

Aggregations

RoleNotFoundException (javax.management.relation.RoleNotFoundException)1 Authority (org.martynas.blogapp.model.Authority)1