Search in sources :

Example 1 with PrivilegeDefinitionImpl

use of org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl in project jackrabbit by apache.

the class RepositoryServiceImpl method internalGetPrivilegeDefinitions.

private PrivilegeDefinition[] internalGetPrivilegeDefinitions(SessionInfo sessionInfo, String uri) throws RepositoryException {
    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(SecurityConstants.SUPPORTED_PRIVILEGE_SET);
    HttpPropfind request = null;
    try {
        request = new HttpPropfind(uri, nameSet, DEPTH_0);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        MultiStatusResponse[] mresponses = request.getResponseBodyAsMultiStatus(response).getResponses();
        if (mresponses.length < 1) {
            throw new PathNotFoundException("Unable to retrieve privileges definitions.");
        }
        DavPropertyName displayName = SecurityConstants.SUPPORTED_PRIVILEGE_SET;
        DavProperty<?> p = mresponses[0].getProperties(DavServletResponse.SC_OK).get(displayName);
        if (p == null) {
            return new PrivilegeDefinition[0];
        } else {
            // build PrivilegeDefinition(s) from the supported-privileges dav property
            Map<Name, SupportedPrivilege> spMap = new HashMap<Name, SupportedPrivilege>();
            fillSupportedPrivilegeMap(new SupportedPrivilegeSetProperty(p).getValue(), spMap, getNameFactory());
            List<PrivilegeDefinition> pDefs = new ArrayList<PrivilegeDefinition>();
            for (Name privilegeName : spMap.keySet()) {
                SupportedPrivilege sp = spMap.get(privilegeName);
                Set<Name> aggrnames = null;
                SupportedPrivilege[] aggregates = sp.getSupportedPrivileges();
                if (aggregates != null && aggregates.length > 0) {
                    aggrnames = new HashSet<Name>();
                    for (SupportedPrivilege aggregate : aggregates) {
                        Name aggregateName = nameFactory.create(aggregate.getPrivilege().getNamespace().getURI(), aggregate.getPrivilege().getName());
                        aggrnames.add(aggregateName);
                    }
                }
                PrivilegeDefinition def = new PrivilegeDefinitionImpl(privilegeName, sp.isAbstract(), aggrnames);
                pDefs.add(def);
            }
            return pDefs.toArray(new PrivilegeDefinition[pDefs.size()]);
        }
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DavException(org.apache.jackrabbit.webdav.DavException) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) PrivilegeDefinition(org.apache.jackrabbit.spi.PrivilegeDefinition) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) DavPropertyName(org.apache.jackrabbit.webdav.property.DavPropertyName) DavPropertyName(org.apache.jackrabbit.webdav.property.DavPropertyName) Name(org.apache.jackrabbit.spi.Name) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) PrivilegeDefinitionImpl(org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) SupportedPrivilegeSetProperty(org.apache.jackrabbit.webdav.security.SupportedPrivilegeSetProperty) PathNotFoundException(javax.jcr.PathNotFoundException) SupportedPrivilege(org.apache.jackrabbit.webdav.security.SupportedPrivilege)

Example 2 with PrivilegeDefinitionImpl

use of org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl in project jackrabbit by apache.

the class PrivilegeRegistry method registerDefinition.

// -----------------------------------< methods used by PrivilegeManager >---
/**
 * Validates and registers a new custom privilege definition with the
 * specified characteristics. Upon successful registration the new custom
 * definition is persisted in the corresponding file system resource.
 * <p>
 * The validation includes the following steps:
 * <ul>
 * <li>assert uniqueness of the specified privilegeName</li>
 * <li>make sure the name doesn't use a reserved namespace</li>
 * <li>assert that all names referenced in the specified name set refer
 * to existing privilege definitions.</li>
 * </ul>
 *
 * @param privilegeName
 * @param isAbstract
 * @param declaredAggregateNames
 * @throws RepositoryException If the privilege could not be registered due
 * to constraint violations or if persisting the custom privilege fails.
 */
void registerDefinition(Name privilegeName, boolean isAbstract, Set<Name> declaredAggregateNames) throws RepositoryException {
    PrivilegeDefinition def = new PrivilegeDefinitionImpl(privilegeName, isAbstract, declaredAggregateNames);
    Map<Name, PrivilegeDefinition> stubs = Collections.singletonMap(privilegeName, def);
    registerCustomDefinitions(stubs);
    // inform clustering about the new privilege.
    if (eventChannel != null) {
        eventChannel.registeredPrivileges(stubs.values());
    }
}
Also used : PrivilegeDefinitionImpl(org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl) PrivilegeDefinition(org.apache.jackrabbit.spi.PrivilegeDefinition) Name(org.apache.jackrabbit.spi.Name)

Example 3 with PrivilegeDefinitionImpl

use of org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl in project jackrabbit by apache.

the class ClusterRecordTest method testPrivilegeRegistration.

/**
 * Test producing and consuming a privilege registration.
 * @throws Exception
 */
public void testPrivilegeRegistration() throws Exception {
    PrivilegeDefinition pdf = new PrivilegeDefinitionImpl(NameFactoryImpl.getInstance().create("", "test"), false, null);
    PrivilegeEvent event = new PrivilegeEvent(Collections.singletonList(pdf));
    master.registeredPrivileges(event.getDefinitions());
    SimpleEventListener listener = new SimpleEventListener();
    slave.setListener((PrivilegeEventListener) listener);
    slave.sync();
    assertEquals(1, listener.getClusterEvents().size());
    assertEquals(listener.getClusterEvents().get(0), event);
}
Also used : PrivilegeDefinitionImpl(org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl) PrivilegeDefinition(org.apache.jackrabbit.spi.PrivilegeDefinition) PrivilegeEvent(org.apache.jackrabbit.core.cluster.SimpleEventListener.PrivilegeEvent)

Example 4 with PrivilegeDefinitionImpl

use of org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl in project jackrabbit by apache.

the class RepositoryServiceImpl method getSupportedPrivileges.

public PrivilegeDefinition[] getSupportedPrivileges(SessionInfo sessionInfo, NodeId nodeId) throws RepositoryException {
    SessionInfoImpl sInfo = getSessionInfoImpl(sessionInfo);
    String path = (nodeId == null) ? null : pathForId(nodeId, sInfo);
    Privilege[] privs = sInfo.getSession().getAccessControlManager().getSupportedPrivileges(path);
    PrivilegeDefinition[] pDefs = new PrivilegeDefinition[privs.length];
    NamePathResolver npResolver = sInfo.getNamePathResolver();
    for (int i = 0; i < privs.length; i++) {
        Set<Name> aggrnames = null;
        if (privs[i].isAggregate()) {
            aggrnames = new HashSet<Name>();
            for (Privilege dap : privs[i].getDeclaredAggregatePrivileges()) {
                aggrnames.add(npResolver.getQName(dap.getName()));
            }
        }
        PrivilegeDefinition def = new PrivilegeDefinitionImpl(npResolver.getQName(privs[i].getName()), privs[i].isAbstract(), aggrnames);
        pDefs[i] = def;
    }
    return pDefs;
}
Also used : NamePathResolver(org.apache.jackrabbit.spi.commons.conversion.NamePathResolver) DefaultNamePathResolver(org.apache.jackrabbit.spi.commons.conversion.DefaultNamePathResolver) PrivilegeDefinitionImpl(org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl) PrivilegeDefinition(org.apache.jackrabbit.spi.PrivilegeDefinition) Privilege(javax.jcr.security.Privilege) Name(org.apache.jackrabbit.spi.Name)

Example 5 with PrivilegeDefinitionImpl

use of org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl in project jackrabbit by apache.

the class CustomPrivilegeTest method testCustomEquivalentDefinitions.

public void testCustomEquivalentDefinitions() throws RepositoryException, FileSystemException, IOException {
    // setup the custom privilege file with cyclic references
    FileSystem fs = ((RepositoryImpl) superuser.getRepository()).getConfig().getFileSystem();
    FileSystemResource resource = new FileSystemResource(fs, "/privileges/custom_privileges.xml");
    if (!resource.exists()) {
        resource.makeParentDirs();
    }
    NameFactory nf = NameFactoryImpl.getInstance();
    Name test = nf.create(Name.NS_DEFAULT_URI, "test");
    Name test2 = nf.create(Name.NS_DEFAULT_URI, "test2");
    Name test3 = nf.create(Name.NS_DEFAULT_URI, "test3");
    Name test4 = nf.create(Name.NS_DEFAULT_URI, "test4");
    Name test5 = nf.create(Name.NS_DEFAULT_URI, "test5");
    Name test6 = nf.create(Name.NS_DEFAULT_URI, "test6");
    OutputStream out = resource.getOutputStream();
    try {
        List<PrivilegeDefinition> defs = new ArrayList<PrivilegeDefinition>();
        defs.add(new PrivilegeDefinitionImpl(test, false, createNameSet(test2, test3)));
        defs.add(new PrivilegeDefinitionImpl(test2, true, Collections.singleton(test4)));
        defs.add(new PrivilegeDefinitionImpl(test3, true, Collections.singleton(test5)));
        defs.add(new PrivilegeDefinitionImpl(test4, true, Collections.<Name>emptySet()));
        defs.add(new PrivilegeDefinitionImpl(test5, true, Collections.<Name>emptySet()));
        // the equivalent definition to 'test'
        defs.add(new PrivilegeDefinitionImpl(test6, false, createNameSet(test2, test5)));
        PrivilegeDefinitionWriter pdw = new PrivilegeDefinitionWriter("text/xml");
        pdw.writeDefinitions(out, defs.toArray(new PrivilegeDefinition[defs.size()]), Collections.<String, String>emptyMap());
        new PrivilegeRegistry(superuser.getWorkspace().getNamespaceRegistry(), fs);
        fail("Equivalent definitions must be detected upon registry startup.");
    } catch (RepositoryException e) {
    // success
    } finally {
        out.close();
        fs.deleteFolder("/privileges");
    }
}
Also used : PrivilegeDefinitionImpl(org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl) FileSystem(org.apache.jackrabbit.core.fs.FileSystem) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) PrivilegeDefinition(org.apache.jackrabbit.spi.PrivilegeDefinition) RepositoryException(javax.jcr.RepositoryException) FileSystemResource(org.apache.jackrabbit.core.fs.FileSystemResource) PrivilegeDefinitionWriter(org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionWriter) NameFactory(org.apache.jackrabbit.spi.NameFactory) Name(org.apache.jackrabbit.spi.Name)

Aggregations

PrivilegeDefinition (org.apache.jackrabbit.spi.PrivilegeDefinition)7 PrivilegeDefinitionImpl (org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl)7 Name (org.apache.jackrabbit.spi.Name)6 ArrayList (java.util.ArrayList)3 RepositoryException (javax.jcr.RepositoryException)3 OutputStream (java.io.OutputStream)2 Privilege (javax.jcr.security.Privilege)2 FileSystem (org.apache.jackrabbit.core.fs.FileSystem)2 FileSystemResource (org.apache.jackrabbit.core.fs.FileSystemResource)2 NameFactory (org.apache.jackrabbit.spi.NameFactory)2 DefaultNamePathResolver (org.apache.jackrabbit.spi.commons.conversion.DefaultNamePathResolver)2 NamePathResolver (org.apache.jackrabbit.spi.commons.conversion.NamePathResolver)2 PrivilegeDefinitionWriter (org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionWriter)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PathNotFoundException (javax.jcr.PathNotFoundException)1 Session (javax.jcr.Session)1 Workspace (javax.jcr.Workspace)1 HttpResponse (org.apache.http.HttpResponse)1