Search in sources :

Example 11 with UsernamePasswordAuthenticationToken

use of net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken in project alfresco-repository by Alfresco.

the class TestAuthenticationServiceImpl method setCurrentUser.

/**
 * Explicitly set the current user to be authenticated.
 *
 * @param userName
 *            String
 * @return Authentication
 */
public Authentication setCurrentUser(String userName) throws AuthenticationException {
    if (userName == null) {
        throw new AuthenticationException("Null user name");
    }
    try {
        UserDetails ud = null;
        if (userName.equals(SYSTEM_USER_NAME)) {
            GrantedAuthority[] gas = new GrantedAuthority[1];
            gas[0] = new GrantedAuthorityImpl("ROLE_SYSTEM");
            ud = new User(SYSTEM_USER_NAME, "", true, true, true, true, gas);
        } else if (userName.equalsIgnoreCase(AuthenticationUtil.getGuestUserName())) {
            GrantedAuthority[] gas = new GrantedAuthority[0];
            ud = new User(AuthenticationUtil.getGuestUserName().toLowerCase(), "", true, true, true, true, gas);
        } else {
            ud = getUserDetails(userName);
        }
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(ud, "", ud.getAuthorities());
        auth.setDetails(ud);
        auth.setAuthenticated(true);
        return setCurrentAuthentication(auth);
    } catch (net.sf.acegisecurity.AuthenticationException ae) {
        throw new AuthenticationException(ae.getMessage(), ae);
    }
}
Also used : UserDetails(net.sf.acegisecurity.UserDetails) User(net.sf.acegisecurity.providers.dao.User) GrantedAuthorityImpl(net.sf.acegisecurity.GrantedAuthorityImpl) GrantedAuthority(net.sf.acegisecurity.GrantedAuthority) UsernamePasswordAuthenticationToken(net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken)

Aggregations

UsernamePasswordAuthenticationToken (net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken)11 Authentication (net.sf.acegisecurity.Authentication)8 AccountExpiredException (net.sf.acegisecurity.AccountExpiredException)2 CredentialsExpiredException (net.sf.acegisecurity.CredentialsExpiredException)2 DisabledException (net.sf.acegisecurity.DisabledException)2 LockedException (net.sf.acegisecurity.LockedException)2 UserDetails (net.sf.acegisecurity.UserDetails)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 BadCredentialsException (net.sf.acegisecurity.BadCredentialsException)1 GrantedAuthority (net.sf.acegisecurity.GrantedAuthority)1 GrantedAuthorityImpl (net.sf.acegisecurity.GrantedAuthorityImpl)1 User (net.sf.acegisecurity.providers.dao.User)1 TenantDisabledException (org.alfresco.repo.tenant.TenantDisabledException)1 TenantRunAsWork (org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork)1