Search in sources :

Example 36 with ITenant

use of org.pentaho.platform.api.mt.ITenant 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 37 with ITenant

use of org.pentaho.platform.api.mt.ITenant 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)

Example 38 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class AbstractJcrBackedUserRoleDao method getJackrabbitUser.

private User getJackrabbitUser(ITenant theTenant, String name, Session session) throws RepositoryException {
    User jackrabbitUser = null;
    String userId = name;
    String userName = name;
    ITenant tenant = theTenant;
    if (tenant == null) {
        tenant = JcrTenantUtils.getTenant(userName, true);
        userName = JcrTenantUtils.getPrincipalName(userName, true);
    }
    if (tenant == null || tenant.getId() == null) {
        tenant = JcrTenantUtils.getCurrentTenant();
    }
    if (tenant == null || tenant.getId() == null) {
        tenant = JcrTenantUtils.getDefaultTenant();
    }
    if (tenant != null) {
        userId = tenantedUserNameUtils.getPrincipleId(tenant, userName);
        UserManager userMgr = getUserManager(tenant, session);
        Authorizable authorizable = userMgr.getAuthorizable(userId);
        if (authorizable instanceof User) {
            jackrabbitUser = (User) authorizable;
        }
    }
    return jackrabbitUser;
}
Also used : 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) UserManager(org.apache.jackrabbit.api.security.user.UserManager) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable)

Example 39 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class AbstractJcrBackedUserRoleDao method getJackrabbitGroup.

private Group getJackrabbitGroup(ITenant theTenant, String name, Session session) throws RepositoryException {
    Group jackrabbitGroup = null;
    String roleId = name;
    String roleName = name;
    ITenant tenant = theTenant;
    if (tenant == null) {
        tenant = JcrTenantUtils.getTenant(roleName, false);
        roleName = JcrTenantUtils.getPrincipalName(roleName, false);
    }
    if (tenant == null || tenant.getId() == null) {
        tenant = JcrTenantUtils.getCurrentTenant();
    }
    if (tenant == null || tenant.getId() == null) {
        tenant = JcrTenantUtils.getDefaultTenant();
    }
    roleId = tenantedRoleNameUtils.getPrincipleId(tenant, roleName);
    UserManager userMgr = getUserManager(tenant, session);
    Authorizable authorizable = userMgr.getAuthorizable(roleId);
    if (authorizable instanceof Group) {
        jackrabbitGroup = (Group) authorizable;
    }
    return jackrabbitGroup;
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) ITenant(org.pentaho.platform.api.mt.ITenant) UserManager(org.apache.jackrabbit.api.security.user.UserManager) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable)

Example 40 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class DefaultBackingRepositoryLifecycleManager method startup.

@Override
public synchronized void startup() {
    ITenant defaultTenant = null;
    loginAsRepositoryAdmin();
    createCustomPrivilege();
    ITenantManager tenantMgr = getTenantManager();
    ITenant systemTenant = tenantMgr.createTenant(null, ServerRepositoryPaths.getPentahoRootFolderName(), tenantAdminRoleName, tenantAuthenticatedRoleName, tenantAnonymousRoleName);
    if (systemTenant != null) {
        try {
            userRoleDao.createUser(systemTenant, systemTenantAdminUserName, passwordService.decrypt(systemTenantAdminPassword), "System Tenant User", new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
            defaultTenant = tenantMgr.getTenant(JcrTenantUtils.getDefaultTenant().getId());
            if (defaultTenant == null) {
                // We'll create the default tenant here... maybe this isn't the best place.
                defaultTenant = tenantMgr.createTenant(systemTenant, TenantUtils.TENANTID_SINGLE_TENANT, tenantAdminRoleName, tenantAuthenticatedRoleName, tenantAnonymousRoleName);
            }
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) ITenantManager(org.pentaho.platform.api.mt.ITenantManager)

Aggregations

ITenant (org.pentaho.platform.api.mt.ITenant)174 Test (org.junit.Test)120 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)86 Matchers.anyString (org.mockito.Matchers.anyString)47 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)27 ArrayList (java.util.ArrayList)21 Tenant (org.pentaho.platform.core.mt.Tenant)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)17 RepositoryFileSid (org.pentaho.platform.api.repository2.unified.RepositoryFileSid)15 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)14 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)14 IPentahoUser (org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)12 WebResource (com.sun.jersey.api.client.WebResource)11 JerseyTest (com.sun.jersey.test.framework.JerseyTest)11 ITenantedPrincipleNameResolver (org.pentaho.platform.api.mt.ITenantedPrincipleNameResolver)10 SampleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.sample.SampleRepositoryFileData)10 Serializable (java.io.Serializable)9 Date (java.util.Date)9 HashMap (java.util.HashMap)9