public UserDetails loadUserByUsername( String username ) throws UsernameNotFoundException, DataAccessException {
final boolean ACCOUNT_NON_EXPIRED = true;
final boolean CREDS_NON_EXPIRED = true;
final boolean ACCOUNT_NON_LOCKED = true;
IPentahoUser user;
try {
if ( userRoleDao == null ) {
userRoleDao = PentahoSystem.get( IUserRoleDao.class, "userRoleDaoProxy", PentahoSessionHolder.getSession() );
}
user = userRoleDao.getUser( null, username );
} catch ( UncategorizedUserRoleDaoException e ) {
throw new UserRoleDaoUserDetailsServiceException( Messages.getInstance().getString(
"UserRoleDaoUserDetailsService.ERROR_0003_DATA_ACCESS_EXCEPTION" ), e ); //$NON-NLS-1$
}
if ( user == null ) {
throw new UsernameNotFoundException( Messages.getInstance().getString(
"UserRoleDaoUserDetailsService.ERROR_0001_USER_NOT_FOUND" ) ); //$NON-NLS-1$
}
// convert IPentahoUser to a UserDetails instance
List<IPentahoRole> userRoles = userRoleDao.getUserRoles( null, username );
int authsSize = userRoles != null ? userRoles.size() : 0;
GrantedAuthority[] auths = new GrantedAuthority[authsSize];
int i = 0;
for ( IPentahoRole role : userRoles ) {
auths[i++] = new GrantedAuthorityImpl( role.getName() );
}
List<GrantedAuthority> dbAuths = new ArrayList<GrantedAuthority>( Arrays.asList( auths ) );
addCustomAuthorities( user.getUsername(), dbAuths );
// Store the Tenant ID in the session
IPentahoSession session = PentahoSessionHolder.getSession();
String tenantId = (String) session.getAttribute( IPentahoSession.TENANT_ID_KEY );
if ( tenantId == null ) {
ITenant tenant = JcrTenantUtils.getTenant( username, true );
session.setAttribute( IPentahoSession.TENANT_ID_KEY, tenant.getId() );
}
if ( !StringUtils.isEmpty( defaultRoleString ) ) {
defaultRole = new GrantedAuthorityImpl( defaultRoleString );
}
if ( defaultRole != null && !dbAuths.contains( defaultRole ) ) {
dbAuths.add( defaultRole );
}
if ( dbAuths.size() == 0 ) {
throw new UsernameNotFoundException( Messages.getInstance().getString(
"UserRoleDaoUserDetailsService.ERROR_0002_NO_AUTHORITIES" ) ); //$NON-NLS-1$
}
GrantedAuthority[] arrayAuths = dbAuths.toArray( new GrantedAuthority[dbAuths.size()] );
return new User( user.getUsername(), user.getPassword(), user.isEnabled(), ACCOUNT_NON_EXPIRED, CREDS_NON_EXPIRED,
ACCOUNT_NON_LOCKED, arrayAuths );
}