Search in sources :

Example 16 with NotFoundException

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

the class UserRoleDaoResourceTest method testRemoveRolesFromUserNotFoundException.

@Test
public void testRemoveRolesFromUserNotFoundException() {
    String user = "testUser1";
    String roles = "testRole1";
    doThrow(new NotFoundException("expectedTestException")).when(userRoleService).removeRolesFromUser(anyString(), anyString());
    try {
        userRoleResource.removeRolesFromUser(user, roles);
    } catch (WebApplicationException e) {
        assertEquals(Response.Status.NOT_FOUND.getStatusCode(), e.getResponse().getStatus());
        assertNotNull(e.getResponse().getEntity());
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 17 with NotFoundException

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

the class UserRoleDaoServiceTest method testRemoveRoleFromUserNotFoundException.

@Test(expected = NotFoundException.class)
public void testRemoveRoleFromUserNotFoundException() {
    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.removeRolesFromUser(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 18 with NotFoundException

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

the class AbstractJcrBackedRoleBindingDao method setRoleBindings.

public void setRoleBindings(Session session, ITenant tenant, String runtimeRoleName, List<String> logicalRoleNames) throws NamespaceException, RepositoryException {
    if (tenant == null) {
        tenant = JcrTenantUtils.getTenant(runtimeRoleName, false);
        runtimeRoleName = getPrincipalName(runtimeRoleName);
    }
    if (!TenantUtils.isAccessibleTenant(tenant)) {
        throw new NotFoundException("Tenant " + tenant.getId() + " not found");
    }
    PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
    // $NON-NLS-1$
    final String phoNsPrefix = session.getNamespacePrefix(PentahoJcrConstants.PHO_NS) + ":";
    // $NON-NLS-1$
    final String onlyPentahoPattern = phoNsPrefix + "*";
    Node runtimeRolesFolderNode = getRuntimeRolesFolderNode(session, tenant);
    NodeIterator runtimeRoleNodes = runtimeRolesFolderNode.getNodes(onlyPentahoPattern);
    int i = 0;
    while (runtimeRoleNodes.hasNext()) {
        runtimeRoleNodes.nextNode();
        i++;
    }
    if (i == 0) {
        // consulted
        for (Map.Entry<String, List<String>> entry : bootstrapRoleBindings.entrySet()) {
            JcrRoleAuthorizationPolicyUtils.internalSetBindings(pentahoJcrConstants, runtimeRolesFolderNode, entry.getKey(), entry.getValue(), phoNsPrefix);
        }
    }
    if (!isImmutable(runtimeRoleName)) {
        JcrRoleAuthorizationPolicyUtils.internalSetBindings(pentahoJcrConstants, runtimeRolesFolderNode, runtimeRoleName, logicalRoleNames, phoNsPrefix);
    } else {
        throw new RuntimeException(Messages.getInstance().getString("JcrRoleAuthorizationPolicyRoleBindingDao.ERROR_0001_ATTEMPT_MOD_IMMUTABLE", // $NON-NLS-1$
        runtimeRoleName));
    }
    session.save();
    Assert.isTrue(NodeHelper.hasNode(runtimeRolesFolderNode, phoNsPrefix, runtimeRoleName));
    // update cache
    String roleId = tenantedRoleNameUtils.getPrincipleId(tenant, runtimeRoleName);
    cacheManager.putInRegionCache(LOGICAL_ROLE_BINDINGS_REGION, roleId, logicalRoleNames);
}
Also used : NodeIterator(javax.jcr.NodeIterator) PentahoJcrConstants(org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants) Node(javax.jcr.Node) PathNotFoundException(javax.jcr.PathNotFoundException) NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with NotFoundException

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

the class AbstractJcrBackedUserRoleDao method setUserRoles.

public void setUserRoles(Session session, final ITenant theTenant, final String userName, final String[] roles) throws RepositoryException, NotFoundException {
    if ((isMyself(userName) || isDefaultAdminUser(userName)) && !adminRoleExist(roles)) {
        throw new RepositoryException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0005_YOURSELF_OR_DEFAULT_ADMIN_USER"));
    }
    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> currentlyAssignedGroups = new HashMap<String, Group>();
    Iterator<Group> currentGroups = jackrabbitUser.memberOf();
    while (currentGroups.hasNext()) {
        Group currentGroup = currentGroups.next();
        currentlyAssignedGroups.put(currentGroup.getID(), currentGroup);
    }
    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> groupsToRemove = new ArrayList<String>(currentlyAssignedGroups.keySet());
    groupsToRemove.removeAll(finalCollectionOfAssignedGroups.keySet());
    ArrayList<String> groupsToAdd = new ArrayList<String>(finalCollectionOfAssignedGroups.keySet());
    groupsToAdd.removeAll(currentlyAssignedGroups.keySet());
    for (String groupId : groupsToRemove) {
        currentlyAssignedGroups.get(groupId).removeMember(jackrabbitUser);
    }
    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) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) RepositoryException(javax.jcr.RepositoryException) ITenant(org.pentaho.platform.api.mt.ITenant) HashSet(java.util.HashSet)

Example 20 with NotFoundException

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

the class AbstractJcrBackedUserRoleDao method createUser.

public IPentahoUser createUser(Session session, final ITenant theTenant, final String userName, final String password, final String description, final String[] roles) throws AuthorizableExistsException, RepositoryException {
    ITenant tenant = theTenant;
    String user = userName;
    if (tenant == null) {
        tenant = JcrTenantUtils.getTenant(userName, true);
        user = JcrTenantUtils.getPrincipalName(userName, true);
    }
    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 userId = tenantedUserNameUtils.getPrincipleId(tenant, user);
    UserManager tenantUserMgr = getUserManager(tenant, session);
    // $NON-NLS-1$
    tenantUserMgr.createUser(userId, password, new PrincipalImpl(userId), "");
    session.save();
    /**
     * This call is absolutely necessary. setUserRolesForNewUser will never inspect what roles this user is a part of.
     * Since this is a new user it will not be a part of new roles
     */
    setUserRolesForNewUser(session, tenant, user, roles);
    setUserDescription(session, tenant, user, description);
    session.save();
    createUserHomeFolder(tenant, user, session);
    session.save();
    this.userDetailsCache.removeUserFromCache(userName);
    return getUser(session, tenant, userName);
}
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)

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