Search in sources :

Example 1 with CurrentUserPrivilegeSetProperty

use of org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty in project jackrabbit by apache.

the class JcrUserPrivilegesProperty method asDavProperty.

public CurrentUserPrivilegeSetProperty asDavProperty() throws RepositoryException {
    List<Privilege> davPrivs = new ArrayList<Privilege>();
    for (javax.jcr.security.Privilege privilege : session.getAccessControlManager().getPrivileges(absPath)) {
        String privilegeName = privilege.getName();
        String prefix = Text.getNamespacePrefix(privilegeName);
        Namespace ns = (prefix.isEmpty()) ? Namespace.EMPTY_NAMESPACE : Namespace.getNamespace(prefix, session.getNamespaceURI(prefix));
        davPrivs.add(Privilege.getPrivilege(Text.getLocalName(privilegeName), ns));
    }
    return new CurrentUserPrivilegeSetProperty(davPrivs.toArray(new Privilege[davPrivs.size()]));
}
Also used : CurrentUserPrivilegeSetProperty(org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty) ArrayList(java.util.ArrayList) Privilege(org.apache.jackrabbit.webdav.security.Privilege) Namespace(org.apache.jackrabbit.webdav.xml.Namespace)

Example 2 with CurrentUserPrivilegeSetProperty

use of org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty in project jackrabbit by apache.

the class JcrPrivilegeReport method addResponses.

private void addResponses(DavResourceLocator locator) {
    String repositoryPath = locator.getRepositoryPath();
    MultiStatusResponse resp = new MultiStatusResponse(locator.getHref(false), null);
    List<Privilege> currentPrivs = new ArrayList<Privilege>();
    for (Privilege priv : PRIVS) {
        try {
            if (getRepositorySession().hasPermission(repositoryPath, priv.getName())) {
                currentPrivs.add(priv);
            }
        } catch (RepositoryException e) {
            // ignore
            log.debug(e.toString());
        }
    }
    resp.add(new CurrentUserPrivilegeSetProperty(currentPrivs.toArray(new Privilege[currentPrivs.size()])));
    ms.addResponse(resp);
}
Also used : CurrentUserPrivilegeSetProperty(org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Privilege(org.apache.jackrabbit.webdav.security.Privilege)

Example 3 with CurrentUserPrivilegeSetProperty

use of org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty in project jackrabbit by apache.

the class RepositoryServiceImpl method isGranted.

@Override
public boolean isGranted(SessionInfo sessionInfo, ItemId itemId, String[] actions) throws RepositoryException {
    HttpReport request = null;
    try {
        String uri = obtainAbsolutePathFromUri(getItemUri(itemId, sessionInfo));
        ReportInfo reportInfo = new ReportInfo(JcrRemotingConstants.REPORT_PRIVILEGES, ItemResourceConstants.NAMESPACE);
        reportInfo.setContentElement(DomUtil.hrefToXml(uri, DomUtil.createDocument()));
        request = new HttpReport(uriResolver.getWorkspaceUri(sessionInfo.getWorkspaceName()), reportInfo);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        MultiStatusResponse[] responses = request.getResponseBodyAsMultiStatus(response).getResponses();
        if (responses.length < 1) {
            throw new ItemNotFoundException("Unable to retrieve permissions for item " + saveGetIdString(itemId, sessionInfo));
        }
        DavProperty<?> p = responses[0].getProperties(DavServletResponse.SC_OK).get(SecurityConstants.CURRENT_USER_PRIVILEGE_SET);
        if (p == null) {
            return false;
        }
        // build set of privileges from given actions. NOTE: since the actions
        // have no qualifying namespace, the {@link ItemResourceConstants#NAMESPACE}
        // is used.
        Set<Privilege> requiredPrivileges = new HashSet<Privilege>();
        for (String action : actions) {
            requiredPrivileges.add(Privilege.getPrivilege(action, ItemResourceConstants.NAMESPACE));
        }
        // build set of privileges granted to the current user.
        CurrentUserPrivilegeSetProperty privSet = new CurrentUserPrivilegeSetProperty(p);
        Collection<Privilege> privileges = privSet.getValue();
        // check privileges present against required privileges.
        return privileges.containsAll(requiredPrivileges);
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (ParserConfigurationException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : HttpReport(org.apache.jackrabbit.webdav.client.methods.HttpReport) DavException(org.apache.jackrabbit.webdav.DavException) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) ReportInfo(org.apache.jackrabbit.webdav.version.report.ReportInfo) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) CurrentUserPrivilegeSetProperty(org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SupportedPrivilege(org.apache.jackrabbit.webdav.security.SupportedPrivilege) Privilege(org.apache.jackrabbit.webdav.security.Privilege) ItemNotFoundException(javax.jcr.ItemNotFoundException) HashSet(java.util.HashSet)

Example 4 with CurrentUserPrivilegeSetProperty

use of org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty in project jackrabbit by apache.

the class RepositoryServiceImpl method getPrivilegeNames.

@Override
public Name[] getPrivilegeNames(SessionInfo sessionInfo, NodeId nodeId) throws RepositoryException {
    String uri = (nodeId == null) ? uriResolver.getWorkspaceUri(sessionInfo.getWorkspaceName()) : getItemUri(nodeId, sessionInfo);
    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(SecurityConstants.CURRENT_USER_PRIVILEGE_SET);
    HttpPropfind propfindRequest = null;
    try {
        propfindRequest = new HttpPropfind(uri, nameSet, DEPTH_0);
        HttpResponse response = execute(propfindRequest, sessionInfo);
        propfindRequest.checkSuccess(response);
        MultiStatusResponse[] mresponses = propfindRequest.getResponseBodyAsMultiStatus(response).getResponses();
        if (mresponses.length < 1) {
            throw new PathNotFoundException("Unable to retrieve privileges definitions.");
        }
        DavPropertyName displayName = SecurityConstants.CURRENT_USER_PRIVILEGE_SET;
        DavProperty<?> p = mresponses[0].getProperties(DavServletResponse.SC_OK).get(displayName);
        if (p == null) {
            return new Name[0];
        } else {
            Collection<Privilege> privs = new CurrentUserPrivilegeSetProperty(p).getValue();
            Set<Name> privNames = new HashSet<Name>(privs.size());
            for (Privilege priv : privs) {
                privNames.add(nameFactory.create(priv.getNamespace().getURI(), priv.getName()));
            }
            return privNames.toArray(new Name[privNames.size()]);
        }
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (propfindRequest != null) {
            propfindRequest.releaseConnection();
        }
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) HttpResponse(org.apache.http.HttpResponse) 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) CurrentUserPrivilegeSetProperty(org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) PathNotFoundException(javax.jcr.PathNotFoundException) SupportedPrivilege(org.apache.jackrabbit.webdav.security.SupportedPrivilege) Privilege(org.apache.jackrabbit.webdav.security.Privilege) HashSet(java.util.HashSet)

Aggregations

CurrentUserPrivilegeSetProperty (org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty)4 Privilege (org.apache.jackrabbit.webdav.security.Privilege)4 RepositoryException (javax.jcr.RepositoryException)3 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 HttpResponse (org.apache.http.HttpResponse)2 DavException (org.apache.jackrabbit.webdav.DavException)2 SupportedPrivilege (org.apache.jackrabbit.webdav.security.SupportedPrivilege)2 ItemNotFoundException (javax.jcr.ItemNotFoundException)1 PathNotFoundException (javax.jcr.PathNotFoundException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Name (org.apache.jackrabbit.spi.Name)1 HttpPropfind (org.apache.jackrabbit.webdav.client.methods.HttpPropfind)1 HttpReport (org.apache.jackrabbit.webdav.client.methods.HttpReport)1 DavPropertyName (org.apache.jackrabbit.webdav.property.DavPropertyName)1 DavPropertyNameSet (org.apache.jackrabbit.webdav.property.DavPropertyNameSet)1 ReportInfo (org.apache.jackrabbit.webdav.version.report.ReportInfo)1 Namespace (org.apache.jackrabbit.webdav.xml.Namespace)1