Search in sources :

Example 16 with PentahoJcrConstants

use of org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants in project pentaho-platform by pentaho.

the class JcrRepositoryFileAclUtils method internalUpdateAcl.

private static RepositoryFileAcl internalUpdateAcl(final Session session, final PentahoJcrConstants pentahoJcrConstants, final Serializable fileId, final RepositoryFileAcl acl) throws RepositoryException {
    Node node = session.getNodeByIdentifier(fileId.toString());
    if (node == null) {
        // $NON-NLS-1$
        throw new RepositoryException("Node not found");
    }
    String absPath = node.getPath();
    AccessControlManager acMgr = session.getAccessControlManager();
    AccessControlList acList = getAccessControlList(acMgr, absPath);
    // clear all entries
    AccessControlEntry[] acEntries = acList.getAccessControlEntries();
    for (int i = 0; i < acEntries.length; i++) {
        acList.removeAccessControlEntry(acEntries[i]);
    }
    JcrRepositoryFileAclUtils.setAclMetadata(session, absPath, acList, new AclMetadata(acl.getOwner().getName(), acl.isEntriesInheriting()));
    // add entries to now empty list but only if not inheriting; force user to start with clean slate
    if (!acl.isEntriesInheriting()) {
        for (RepositoryFileAce ace : acl.getAces()) {
            Principal principal = null;
            if (RepositoryFileSid.Type.ROLE == ace.getSid().getType()) {
                principal = new SpringSecurityRolePrincipal(JcrTenantUtils.getTenantedRole(ace.getSid().getName()));
            } else {
                principal = new SpringSecurityUserPrincipal(JcrTenantUtils.getTenantedUser(ace.getSid().getName()));
            }
            IPermissionConversionHelper permissionConversionHelper = new DefaultPermissionConversionHelper(session);
            acList.addAccessControlEntry(principal, permissionConversionHelper.pentahoPermissionsToPrivileges(session, ace.getPermissions()));
        }
    }
    acMgr.setPolicy(absPath, acList);
    session.save();
    return getAcl(session, pentahoJcrConstants, fileId);
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) AccessControlList(javax.jcr.security.AccessControlList) RepositoryFileAce(org.pentaho.platform.api.repository2.unified.RepositoryFileAce) Node(javax.jcr.Node) AclMetadata(org.pentaho.platform.repository2.unified.jcr.IAclMetadataStrategy.AclMetadata) AccessControlEntry(javax.jcr.security.AccessControlEntry) RepositoryException(javax.jcr.RepositoryException) IPermissionConversionHelper(org.pentaho.platform.repository2.unified.jcr.JcrRepositoryFileAclDao.IPermissionConversionHelper) SpringSecurityRolePrincipal(org.pentaho.platform.repository2.unified.jcr.jackrabbit.security.SpringSecurityRolePrincipal) SpringSecurityRolePrincipal(org.pentaho.platform.repository2.unified.jcr.jackrabbit.security.SpringSecurityRolePrincipal) SpringSecurityUserPrincipal(org.pentaho.platform.repository2.unified.jcr.jackrabbit.security.SpringSecurityUserPrincipal) Principal(java.security.Principal) SpringSecurityUserPrincipal(org.pentaho.platform.repository2.unified.jcr.jackrabbit.security.SpringSecurityUserPrincipal)

Example 17 with PentahoJcrConstants

use of org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants in project pentaho-platform by pentaho.

the class JcrRepositoryFileUtils method getLocalizedString.

public static String getLocalizedString(final Session session, final PentahoJcrConstants pentahoJcrConstants, final Node localizedStringNode, IPentahoLocale pentahoLocale) throws RepositoryException {
    Assert.isTrue(isLocalizedString(session, pentahoJcrConstants, localizedStringNode));
    boolean isLocaleNull = pentahoLocale == null;
    if (pentahoLocale == null) {
        pentahoLocale = new PentahoLocale();
    }
    Locale locale = pentahoLocale.getLocale();
    // $NON-NLS-1$
    final String UNDERSCORE = "_";
    // $NON-NLS-1$
    final String COLON = ":";
    boolean hasLanguage = StringUtils.hasText(locale.getLanguage());
    boolean hasCountry = StringUtils.hasText(locale.getCountry());
    boolean hasVariant = StringUtils.hasText(locale.getVariant());
    List<String> candidatePropertyNames = new ArrayList<String>(3);
    if (hasVariant) {
        candidatePropertyNames.add(locale.getLanguage() + UNDERSCORE + locale.getCountry() + UNDERSCORE + locale.getVariant());
    }
    if (hasCountry) {
        candidatePropertyNames.add(locale.getLanguage() + UNDERSCORE + locale.getCountry());
    }
    if (hasLanguage) {
        candidatePropertyNames.add(locale.getLanguage());
    }
    for (String propertyName : candidatePropertyNames) {
        if (localizedStringNode.hasProperty(propertyName)) {
            return localizedStringNode.getProperty(propertyName).getString();
        }
    }
    String prefix = session.getNamespacePrefix(PentahoJcrConstants.PHO_NS);
    Assert.hasText(prefix);
    String propertyStr = isLocaleNull ? pentahoJcrConstants.getPHO_ROOTLOCALE() : prefix + COLON + locale.getLanguage();
    return localizedStringNode.getProperty(propertyStr).getString();
}
Also used : Locale(java.util.Locale) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) ArrayList(java.util.ArrayList) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale)

Example 18 with PentahoJcrConstants

use of org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants in project pentaho-platform by pentaho.

the class JcrRepositoryFileUtils method nodeToFileOld.

public static RepositoryFile nodeToFileOld(final Session session, final PentahoJcrConstants pentahoJcrConstants, final IPathConversionHelper pathConversionHelper, final ILockHelper lockHelper, final Node node, final boolean loadMaps, IPentahoLocale pentahoLocale) throws RepositoryException {
    if (session.getRootNode().isSame(node)) {
        return getRootFolder(session);
    }
    Serializable id = null;
    String name = null;
    String path = null;
    long fileSize = 0;
    Date created = null;
    String creatorId = null;
    Boolean hidden = RepositoryFile.HIDDEN_BY_DEFAULT;
    Boolean schedulable = RepositoryFile.SCHEDULABLE_BY_DEFAULT;
    Date lastModified = null;
    boolean folder = false;
    boolean versioned = false;
    Serializable versionId = null;
    boolean locked = false;
    String lockOwner = null;
    Date lockDate = null;
    String lockMessage = null;
    String title = null;
    String description = null;
    Boolean aclNode = false;
    Map<String, Properties> localePropertiesMap = null;
    id = getNodeId(session, pentahoJcrConstants, node);
    if (logger.isDebugEnabled()) {
        // $NON-NLS-1$
        logger.debug(String.format("reading file with id '%s' and path '%s'", id, node.getPath()));
    }
    path = pathConversionHelper.absToRel((getAbsolutePath(session, pentahoJcrConstants, node)));
    // if the rel path is / then name the folder empty string instead of its true name (this hides the tenant name)
    // $NON-NLS-1$
    name = RepositoryFile.SEPARATOR.equals(path) ? "" : getNodeName(session, pentahoJcrConstants, node);
    if (isPentahoFolder(pentahoJcrConstants, node)) {
        folder = true;
    }
    // jcr:created nodes have OnParentVersion values of INITIALIZE
    if (node.hasProperty(pentahoJcrConstants.getJCR_CREATED())) {
        Calendar tmpCal = node.getProperty(pentahoJcrConstants.getJCR_CREATED()).getDate();
        if (tmpCal != null) {
            created = tmpCal.getTime();
        }
    }
    // Expensive
    Map<String, Serializable> metadata = getFileMetadata(session, id);
    if (metadata != null) {
        creatorId = (String) metadata.get(PentahoJcrConstants.PHO_CONTENTCREATOR);
        Serializable schedulableValue = metadata.get(RepositoryFile.SCHEDULABLE_KEY);
        if (schedulableValue instanceof String) {
            schedulable = BooleanUtils.toBoolean((String) schedulableValue);
        }
    }
    if (node.hasProperty(pentahoJcrConstants.getPHO_HIDDEN())) {
        hidden = node.getProperty(pentahoJcrConstants.getPHO_HIDDEN()).getBoolean();
    }
    if (node.hasProperty(pentahoJcrConstants.getPHO_FILESIZE())) {
        fileSize = node.getProperty(pentahoJcrConstants.getPHO_FILESIZE()).getLong();
    }
    if (node.hasProperty(pentahoJcrConstants.getPHO_ACLNODE())) {
        aclNode = node.getProperty(pentahoJcrConstants.getPHO_ACLNODE()).getBoolean();
    }
    if (isPentahoFile(pentahoJcrConstants, node)) {
        // pho:lastModified nodes have OnParentVersion values of IGNORE; i.e. they don't exist in frozen nodes
        if (!node.isNodeType(pentahoJcrConstants.getNT_FROZENNODE())) {
            Calendar tmpCal = node.getProperty(pentahoJcrConstants.getPHO_LASTMODIFIED()).getDate();
            if (tmpCal != null) {
                lastModified = tmpCal.getTime();
            }
        }
    }
    // Get default locale if null
    if (pentahoLocale == null) {
        Locale currentLocale = LocaleHelper.getLocale();
        if (currentLocale != null) {
            pentahoLocale = new PentahoLocale(currentLocale);
        } else {
            pentahoLocale = new PentahoLocale();
        }
    }
    // Not needed for content generators and the like
    if (isPentahoHierarchyNode(session, pentahoJcrConstants, node)) {
        if (node.hasNode(pentahoJcrConstants.getPHO_LOCALES())) {
            // Expensive
            localePropertiesMap = getLocalePropertiesMap(session, pentahoJcrConstants, node.getNode(pentahoJcrConstants.getPHO_LOCALES()));
            // [BISERVER-8337] localize title and description
            LocalePropertyResolver lpr = new LocalePropertyResolver(name);
            LocalizationUtil localizationUtil = new LocalizationUtil(localePropertiesMap, pentahoLocale.getLocale());
            title = localizationUtil.resolveLocalizedString(lpr.resolveDefaultTitleKey(), null);
            if (org.apache.commons.lang.StringUtils.isBlank(title)) {
                title = localizationUtil.resolveLocalizedString(lpr.resolveTitleKey(), null);
                if (org.apache.commons.lang.StringUtils.isBlank(title)) {
                    title = localizationUtil.resolveLocalizedString(lpr.resolveNameKey(), title);
                }
            }
            description = localizationUtil.resolveLocalizedString(lpr.resolveDefaultDescriptionKey(), null);
            if (org.apache.commons.lang.StringUtils.isBlank(description)) {
                description = localizationUtil.resolveLocalizedString(lpr.resolveDescriptionKey(), description);
            }
        }
        // found
        if (title == null && node.hasNode(pentahoJcrConstants.getPHO_TITLE())) {
            title = getLocalizedString(session, pentahoJcrConstants, node.getNode(pentahoJcrConstants.getPHO_TITLE()), pentahoLocale);
        }
        if (description == null && node.hasNode(pentahoJcrConstants.getPHO_DESCRIPTION())) {
            description = getLocalizedString(session, pentahoJcrConstants, node.getNode(pentahoJcrConstants.getPHO_DESCRIPTION()), pentahoLocale);
        }
    }
    if (!loadMaps) {
        // remove reference, allow garbage collection
        localePropertiesMap = null;
    }
    versioned = isVersioned(session, pentahoJcrConstants, node);
    if (versioned) {
        versionId = getVersionId(session, pentahoJcrConstants, node);
    }
    locked = isLocked(pentahoJcrConstants, node);
    if (locked) {
        Lock lock = session.getWorkspace().getLockManager().getLock(node.getPath());
        lockOwner = lockHelper.getLockOwner(session, pentahoJcrConstants, lock);
        lockDate = lockHelper.getLockDate(session, pentahoJcrConstants, lock);
        lockMessage = lockHelper.getLockMessage(session, pentahoJcrConstants, lock);
    }
    RepositoryFile file = new RepositoryFile.Builder(id, name).createdDate(created).creatorId(creatorId).lastModificationDate(lastModified).folder(folder).versioned(versioned).path(path).versionId(versionId).fileSize(fileSize).locked(locked).lockDate(lockDate).hidden(hidden).schedulable(schedulable).lockMessage(lockMessage).lockOwner(lockOwner).title(title).description(description).locale(pentahoLocale.toString()).localePropertiesMap(localePropertiesMap).aclNode(aclNode).build();
    return file;
}
Also used : Locale(java.util.Locale) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) Serializable(java.io.Serializable) Calendar(java.util.Calendar) Properties(java.util.Properties) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) Date(java.util.Date) Lock(javax.jcr.lock.Lock) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean)

Example 19 with PentahoJcrConstants

use of org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants 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 20 with PentahoJcrConstants

use of org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants in project pentaho-platform by pentaho.

the class AbstractJcrBackedRoleBindingDao method getRoleBindings.

protected Map<String, List<String>> getRoleBindings(Session session, ITenant tenant) throws RepositoryException {
    Map<String, List<String>> map = new HashMap<String, List<String>>();
    if (tenant == null) {
        tenant = JcrTenantUtils.getTenant();
    }
    if (!TenantUtils.isAccessibleTenant(tenant)) {
        return map;
    }
    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);
    if (!runtimeRoleNodes.hasNext()) {
        // no bindings setup yet; fall back on bootstrap bindings
        map.putAll(bootstrapRoleBindings);
    } else {
        while (runtimeRoleNodes.hasNext()) {
            Node runtimeRoleNode = runtimeRoleNodes.nextNode();
            if (runtimeRoleNode.hasProperty(pentahoJcrConstants.getPHO_BOUNDROLES())) {
                // get clean runtime role name
                String runtimeRoleName = JcrStringHelper.fileNameDecode(runtimeRoleNode.getName().substring(phoNsPrefix.length()));
                // get logical role names
                List<String> logicalRoleNames = new ArrayList<String>();
                Value[] values = runtimeRoleNode.getProperty(pentahoJcrConstants.getPHO_BOUNDROLES()).getValues();
                for (Value value : values) {
                    logicalRoleNames.add(value.getString());
                }
                map.put(runtimeRoleName, logicalRoleNames);
            }
        }
    }
    // add all immutable bindings
    map.putAll(immutableRoleBindingNames);
    return map;
}
Also used : NodeIterator(javax.jcr.NodeIterator) HashMap(java.util.HashMap) PentahoJcrConstants(org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) Value(javax.jcr.Value) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Node (javax.jcr.Node)15 RepositoryException (javax.jcr.RepositoryException)9 PentahoJcrConstants (org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants)9 RepositoryFileDaoFileExistsException (org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoFileExistsException)7 RepositoryFileDaoReferentialIntegrityException (org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException)7 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 Matchers.anyString (org.mockito.Matchers.anyString)6 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)6 Property (javax.jcr.Property)5 Workspace (javax.jcr.Workspace)5 Serializable (java.io.Serializable)4 NodeIterator (javax.jcr.NodeIterator)4 PropertyIterator (javax.jcr.PropertyIterator)4 Session (javax.jcr.Session)4 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)4 ITenant (org.pentaho.platform.api.mt.ITenant)4 Tenant (org.pentaho.platform.core.mt.Tenant)4 JcrCallback (org.springframework.extensions.jcr.JcrCallback)4 Lock (javax.jcr.lock.Lock)3