Search in sources :

Example 6 with NotFoundException

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

the class UserRoleDaoIT method testUpdateUser.

@Test
public void testUpdateUser() throws Exception {
    loginAsRepositoryAdmin();
    systemTenant = tenantManager.createTenant(null, ServerRepositoryPaths.getPentahoRootFolderName(), adminRoleName, authenticatedRoleName, "Anonymous");
    userRoleDaoProxy.createUser(systemTenant, sysAdminUserName, "password", "", new String[] { adminRoleName });
    login(sysAdminUserName, systemTenant, new String[] { adminRoleName, authenticatedRoleName });
    mainTenant_1 = tenantManager.createTenant(systemTenant, MAIN_TENANT_1, adminRoleName, authenticatedRoleName, "Anonymous");
    userRoleDaoProxy.createUser(mainTenant_1, "admin", "password", "", new String[] { adminRoleName });
    mainTenant_2 = tenantManager.createTenant(systemTenant, MAIN_TENANT_2, adminRoleName, authenticatedRoleName, "Anonymous");
    userRoleDaoProxy.createUser(mainTenant_2, "admin", "password", "", new String[] { adminRoleName });
    login("admin", mainTenant_1, new String[] { adminRoleName, authenticatedRoleName });
    IPentahoUser pentahoUser = userRoleDaoProxy.createUser(mainTenant_1, USER_5, PASSWORD_5, USER_DESCRIPTION_5, null);
    pentahoUser = userRoleDaoProxy.getUser(mainTenant_1, USER_5);
    assertEquals(pentahoUser.getDescription(), USER_DESCRIPTION_5);
    String changedDescription1 = USER_DESCRIPTION_5 + "change1";
    userRoleDaoProxy.setUserDescription(mainTenant_1, USER_5, changedDescription1);
    pentahoUser = userRoleDaoProxy.getUser(null, USER_5 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + mainTenant_1.getRootFolderAbsolutePath());
    assertEquals(changedDescription1, pentahoUser.getDescription());
    String changedDescription2 = USER_DESCRIPTION_5 + "change2";
    userRoleDaoProxy.setUserDescription(null, USER_5 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + mainTenant_1.getRootFolderAbsolutePath(), changedDescription2);
    pentahoUser = userRoleDaoProxy.getUser(mainTenant_1, USER_5);
    assertEquals(changedDescription2, pentahoUser.getDescription());
    userRoleDaoProxy.setUserDescription(null, USER_5 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + mainTenant_1.getRootFolderAbsolutePath(), null);
    pentahoUser = userRoleDaoProxy.getUser(mainTenant_1, USER_5);
    assertNull(pentahoUser.getDescription());
    try {
        userRoleDaoProxy.setUserDescription(null, null, changedDescription2);
        fail("Exception not thrown");
    } catch (Exception ex) {
    // Expected exception
    }
    try {
        userRoleDaoProxy.setUserDescription(null, USER_5, changedDescription2);
    } catch (Exception ex) {
    // Expected exception
    }
    try {
        userRoleDaoProxy.setUserDescription(mainTenant_1, UNKNOWN_USER, changedDescription2);
        fail("Exception not thrown");
    } catch (NotFoundException ex) {
    // Expected exception
    }
    logout();
    login("admin", mainTenant_2, new String[] { adminRoleName, authenticatedRoleName });
    try {
        changedDescription1 = USER_DESCRIPTION_5 + "change1";
        userRoleDaoProxy.setUserDescription(mainTenant_1, USER_5, changedDescription1);
        fail("Exception not thrown");
    } catch (Throwable th) {
        assertNotNull(th);
    }
    logout();
}
Also used : NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) AccessControlException(javax.jcr.security.AccessControlException) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with NotFoundException

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

the class UserRoleDaoServiceTest method testAssignRoleToUserNotFoundException.

@Test(expected = NotFoundException.class)
public void testAssignRoleToUserNotFoundException() 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 NotFoundException("expectedTestException"));
    PentahoSystem.registerObject(roleDao);
    userRoleService.assignRolesToUser(userName, roleNames);
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) Matchers.anyString(org.mockito.Matchers.anyString) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

Example 8 with NotFoundException

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

the class AbstractJcrBackedUserRoleDao method createRole.

public IPentahoRole createRole(Session session, final ITenant theTenant, final String roleName, final String description, final String[] memberUserNames) throws AuthorizableExistsException, RepositoryException {
    ITenant tenant = theTenant;
    String role = roleName;
    if (tenant == null) {
        tenant = JcrTenantUtils.getTenant(roleName, false);
        role = JcrTenantUtils.getPrincipalName(roleName, false);
    }
    if (tenant == null || tenant.getId() == null) {
        tenant = JcrTenantUtils.getCurrentTenant();
    }
    if (!TenantUtils.isAccessibleTenant(tenant)) {
        throw new NotFoundException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0006_TENANT_NOT_FOUND", theTenant.getId()));
    }
    String roleId = tenantedRoleNameUtils.getPrincipleId(tenant, role);
    UserManager tenantUserMgr = getUserManager(tenant, session);
    // Intermediate path will always be an empty string. The path is already provided while creating a user manager
    // $NON-NLS-1$
    tenantUserMgr.createGroup(new PrincipalImpl(roleId), "");
    setRoleMembers(session, tenant, role, memberUserNames);
    setRoleDescription(session, tenant, role, description);
    return getRole(session, theTenant, roleName);
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) UserManager(org.apache.jackrabbit.api.security.user.UserManager) NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) PrincipalImpl(org.apache.jackrabbit.core.security.principal.PrincipalImpl)

Example 9 with NotFoundException

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

the class AbstractJcrBackedUserRoleDao method setRoleMembers.

public void setRoleMembers(Session session, final ITenant theTenant, final String roleName, final String[] memberUserNames) throws RepositoryException, NotFoundException {
    List<IPentahoUser> currentRoleMembers = getRoleMembers(session, theTenant, roleName);
    String[] usersToBeRemoved = findRemovedUsers(currentRoleMembers, memberUserNames);
    // will display a message to the user.
    if ((oneOfUserIsMySelf(usersToBeRemoved) || oneOfUserIsDefaultAdminUser(usersToBeRemoved)) && tenantAdminRoleName.equals(roleName)) {
        throw new RepositoryException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0009_USER_REMOVE_FAILED_YOURSELF_OR_DEFAULT_ADMIN_USER"));
    }
    // If this is the last user from the Administrator role, we will not let the user remove.
    if (tenantAdminRoleName.equals(roleName) && (currentRoleMembers != null && currentRoleMembers.size() > 0) && memberUserNames.length == 0) {
        throw new RepositoryException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0001_LAST_ADMIN_ROLE", tenantAdminRoleName));
    }
    Group jackrabbitGroup = getJackrabbitGroup(theTenant, roleName, session);
    if ((jackrabbitGroup == null) || !TenantUtils.isAccessibleTenant(theTenant == null ? tenantedRoleNameUtils.getTenant(jackrabbitGroup.getID()) : theTenant)) {
        throw new NotFoundException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0002_ROLE_NOT_FOUND"));
    }
    HashMap<String, User> currentlyAssignedUsers = new HashMap<String, User>();
    Iterator<Authorizable> currentMembers = jackrabbitGroup.getMembers();
    while (currentMembers.hasNext()) {
        Authorizable member = currentMembers.next();
        if (member instanceof User) {
            currentlyAssignedUsers.put(member.getID(), (User) member);
        }
    }
    HashMap<String, User> finalCollectionOfAssignedUsers = new HashMap<String, User>();
    if (memberUserNames != null) {
        ITenant tenant = theTenant == null ? JcrTenantUtils.getTenant(roleName, false) : theTenant;
        for (String user : memberUserNames) {
            User jackrabbitUser = getJackrabbitUser(tenant, user, session);
            if (jackrabbitUser != null) {
                finalCollectionOfAssignedUsers.put(getTenantedUserNameUtils().getPrincipleId(tenant, user), jackrabbitUser);
            }
        }
    }
    ArrayList<String> usersToRemove = new ArrayList<String>(currentlyAssignedUsers.keySet());
    usersToRemove.removeAll(finalCollectionOfAssignedUsers.keySet());
    ArrayList<String> usersToAdd = new ArrayList<String>(finalCollectionOfAssignedUsers.keySet());
    usersToAdd.removeAll(currentlyAssignedUsers.keySet());
    for (String userId : usersToRemove) {
        jackrabbitGroup.removeMember(currentlyAssignedUsers.get(userId));
        purgeUserFromCache(userId);
    }
    for (String userId : usersToAdd) {
        jackrabbitGroup.addMember(finalCollectionOfAssignedUsers.get(userId));
        // Purge the UserDetails cache
        purgeUserFromCache(userId);
    }
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) User(org.apache.jackrabbit.api.security.user.User) PentahoUser(org.pentaho.platform.security.userroledao.PentahoUser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) RepositoryException(javax.jcr.RepositoryException) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) ITenant(org.pentaho.platform.api.mt.ITenant) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable)

Example 10 with NotFoundException

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

the class AbstractJcrBackedUserRoleDao method setUserRolesForNewUser.

private void setUserRolesForNewUser(Session session, final ITenant theTenant, final String userName, final String[] roles) throws RepositoryException, NotFoundException {
    Set<String> roleSet = new HashSet<String>();
    if (roles != null) {
        roleSet.addAll(Arrays.asList(roles));
    }
    roleSet.add(authenticatedRoleName);
    User jackrabbitUser = getJackrabbitUser(theTenant, userName, session);
    if ((jackrabbitUser == null) || !TenantUtils.isAccessibleTenant(theTenant == null ? tenantedUserNameUtils.getTenant(jackrabbitUser.getID()) : theTenant)) {
        throw new NotFoundException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0003_USER_NOT_FOUND"));
    }
    HashMap<String, Group> finalCollectionOfAssignedGroups = new HashMap<String, Group>();
    ITenant tenant = theTenant == null ? JcrTenantUtils.getTenant(userName, true) : theTenant;
    for (String role : roleSet) {
        Group jackrabbitGroup = getJackrabbitGroup(tenant, role, session);
        if (jackrabbitGroup != null) {
            finalCollectionOfAssignedGroups.put(tenantedRoleNameUtils.getPrincipleId(tenant, role), jackrabbitGroup);
        }
    }
    ArrayList<String> groupsToAdd = new ArrayList<String>(finalCollectionOfAssignedGroups.keySet());
    for (String groupId : groupsToAdd) {
        finalCollectionOfAssignedGroups.get(groupId).addMember(jackrabbitUser);
        // Purge the UserDetails cache
        purgeUserFromCache(userName);
    }
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) User(org.apache.jackrabbit.api.security.user.User) PentahoUser(org.pentaho.platform.security.userroledao.PentahoUser) ITenant(org.pentaho.platform.api.mt.ITenant) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) HashSet(java.util.HashSet)

Aggregations

NotFoundException (org.pentaho.platform.api.engine.security.userroledao.NotFoundException)25 Test (org.junit.Test)12 IPentahoUser (org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)12 IOException (java.io.IOException)9 RepositoryException (javax.jcr.RepositoryException)9 AccessControlException (javax.jcr.security.AccessControlException)8 AlreadyExistsException (org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException)8 ITenant (org.pentaho.platform.api.mt.ITenant)8 BeansException (org.springframework.beans.BeansException)8 PentahoUser (org.pentaho.platform.security.userroledao.PentahoUser)7 Group (org.apache.jackrabbit.api.security.user.Group)5 User (org.apache.jackrabbit.api.security.user.User)5 IPentahoRole (org.pentaho.platform.api.engine.security.userroledao.IPentahoRole)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Matchers.anyString (org.mockito.Matchers.anyString)4 DefaultTenantedPrincipleNameResolver (org.pentaho.platform.security.userroledao.DefaultTenantedPrincipleNameResolver)4 HashSet (java.util.HashSet)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 UserManager (org.apache.jackrabbit.api.security.user.UserManager)2