@Override
@Transactional
public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) {
log.info("Get roles for user " + username + " from local DB");
UserEntity user = null;
user = userDAO.findLdapUserByName(username);
if (user == null) {
log.info("User " + username + " not present in local DB - creating");
UserEntity newUser = new UserEntity();
newUser.setLdapUser(true);
newUser.setUserName(username);
String roleName = (configuration.getConfigsMap().get(Configuration.USER_ROLE_NAME_KEY));
log.info("Using default role name " + roleName);
RoleEntity role = roleDAO.findByName(roleName);
if (role == null) {
log.info("Role " + roleName + " not present in local DB - creating");
role = new RoleEntity();
role.setRoleName(roleName);
roleDAO.create(role);
role = roleDAO.findByName(role.getRoleName());
}
userDAO.create(newUser);
user = userDAO.findLdapUserByName(newUser.getUserName());
user.getRoleEntities().add(role);
role.getUserEntities().add(user);
roleDAO.merge(role);
userDAO.merge(user);