Search in sources :

Example 11 with UncategorizedUserRoleDaoException

use of org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException in project pentaho-platform by pentaho.

the class UserRoleDaoServiceTest method testRemoveRolesFromUserUncategorizedUserRoleDaoException.

@Test(expected = UncategorizedUserRoleDaoException.class)
public void testRemoveRolesFromUserUncategorizedUserRoleDaoException() throws UserRoleListService.UnauthorizedException {
    String userName = "testUser";
    String roleNames = "Power User\tBusiness User\t";
    setupMockSessionUser(SESSION_USER_NAME, true);
    // Create session that will generate tenant
    IPentahoSession session = mock(IPentahoSession.class);
    when(session.getAttribute(IPentahoSession.TENANT_ID_KEY)).thenReturn("testTenantPath");
    PentahoSessionHolder.setSession(session);
    IUserRoleDao roleDao = mock(IUserRoleDao.class);
    when(roleDao.getUserRoles(any(ITenant.class), anyString())).thenThrow(new UncategorizedUserRoleDaoException("expectedTestException"));
    PentahoSystem.registerObject(roleDao);
    userRoleService.removeRolesFromUser(userName, roleNames);
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) UncategorizedUserRoleDaoException(org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException) Matchers.anyString(org.mockito.Matchers.anyString) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

Example 12 with UncategorizedUserRoleDaoException

use of org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException in project pentaho-platform by pentaho.

the class UserRoleDaoServiceTest method testDeleteRoleUncategorizedUserRoleDaoException.

@Test(expected = UncategorizedUserRoleDaoException.class)
public void testDeleteRoleUncategorizedUserRoleDaoException() {
    String roles = "role1\trole2\t";
    setupMockSessionUser(SESSION_USER_NAME, true);
    IUserRoleDao roleDao = mock(IUserRoleDao.class);
    when(roleDao.getRole(any(ITenant.class), anyString())).thenThrow(new UncategorizedUserRoleDaoException("expectedTestException"));
    PentahoSystem.registerObject(roleDao);
    userRoleService.deleteRoles(roles);
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) UncategorizedUserRoleDaoException(org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException) Matchers.anyString(org.mockito.Matchers.anyString) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

Example 13 with UncategorizedUserRoleDaoException

use of org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException in project pentaho-platform by pentaho.

the class UserRoleDaoUserDetailsService method loadUserByUsername.

// ~ Constructors
// ====================================================================================================
// ~ Methods
// =========================================================================================================
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"), // $NON-NLS-1$
        e);
    }
    if (user == null) {
        throw new UsernameNotFoundException(Messages.getInstance().getString(// $NON-NLS-1$
        "UserRoleDaoUserDetailsService.ERROR_0001_USER_NOT_FOUND"));
    }
    // 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 SimpleGrantedAuthority(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 SimpleGrantedAuthority(defaultRoleString);
    }
    if (defaultRole != null && !dbAuths.contains(defaultRole)) {
        dbAuths.add(defaultRole);
    }
    if (dbAuths.size() == 0) {
        throw new UsernameNotFoundException(Messages.getInstance().getString(// $NON-NLS-1$
        "UserRoleDaoUserDetailsService.ERROR_0002_NO_AUTHORITIES"));
    }
    return new User(user.getUsername(), user.getPassword(), user.isEnabled(), ACCOUNT_NON_EXPIRED, CREDS_NON_EXPIRED, ACCOUNT_NON_LOCKED, dbAuths);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.springframework.security.core.userdetails.User) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ITenant(org.pentaho.platform.api.mt.ITenant) UncategorizedUserRoleDaoException(org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole)

Aggregations

UncategorizedUserRoleDaoException (org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException)13 Test (org.junit.Test)11 Matchers.anyString (org.mockito.Matchers.anyString)10 WebApplicationException (javax.ws.rs.WebApplicationException)8 IUserRoleDao (org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao)5 ITenant (org.pentaho.platform.api.mt.ITenant)5 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)3 IPentahoRole (org.pentaho.platform.api.engine.security.userroledao.IPentahoRole)2 IPentahoUser (org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)2 ArrayList (java.util.ArrayList)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 Response (javax.ws.rs.core.Response)1 StatusCodes (org.codehaus.enunciate.jaxrs.StatusCodes)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 User (org.springframework.security.core.userdetails.User)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1