Skip to content

Commit

Permalink
JCR-2887 : Split PrivilegeRegistry in a per-session manager instance …
Browse files Browse the repository at this point in the history
…and a repository level registry [work in progress]

git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/trunk@1076600 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
anchela committed Mar 3, 2011
1 parent a7795a0 commit b0b0bed
Show file tree
Hide file tree
Showing 27 changed files with 453 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.jackrabbit.api;

import org.apache.jackrabbit.api.security.authorization.PrivilegeManager;
import org.xml.sax.InputSource;

import javax.jcr.AccessDeniedException;
Expand Down Expand Up @@ -55,4 +56,12 @@ void createWorkspace(String workspaceName)
*/
void createWorkspace(String workspaceName, InputSource workspaceTemplate)
throws AccessDeniedException, RepositoryException;

/**
* Returns the privilege manager.
*
* @return the privilege manager.
* @throws RepositoryException If an error occurs.
*/
PrivilegeManager getPrivilegeManager() throws RepositoryException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jackrabbit.api.security.authorization;

import javax.jcr.AccessDeniedException;
import javax.jcr.NamespaceException;
import javax.jcr.RepositoryException;
import javax.jcr.security.AccessControlException;
import javax.jcr.security.Privilege;

/**
* <code>PrivilegeManager</code> is a jackrabbit specific extensions to
* JCR access control management that allows to retrieve privileges known
* by this JCR implementation and to register new custom privileges according
* to implementation specific rules.
*
* @see javax.jcr.security.AccessControlManager#privilegeFromName(String)
*/
public interface PrivilegeManager {

/**
* Returns all registered privileges.
*
* @return all registered privileges.
* @throws RepositoryException If an error occurs.
*/
Privilege[] getRegisteredPrivileges() throws RepositoryException;

/**
* Returns the privilege with the specified <code>privilegeName</code>.
*
* @param privilegeName Name of the principal.
* @return the privilege with the specified <code>privilegeName</code>.
* @throws javax.jcr.security.AccessControlException If no privilege with the given name exists.
* @throws javax.jcr.RepositoryException If another error occurs.
*/
Privilege getPrivilege(String privilegeName) throws AccessControlException, RepositoryException;

/**
* Creates and registers a new custom privilege with the specified
* characteristics and returns the new privilege.<p/>
* If the registration succeeds, the changes are immediately effective;
* there is no need to call <code>save</code>.
*
* @param privilegeName The name of the new custom privilege.
* @param isAbstract Boolean flag indicating if the privilege is abstract.
* @param declaredAggregateNames An array of privilege names referring to
* registered privileges being aggregated by this new custom privilege.
* In case of a non aggregate privilege an empty array should be passed.
* @return the new privilege.
* @throws AccessDeniedException If the session this manager has been created
* for is not allowed to register new privileges.
* @throws NamespaceException If any of the specified JCR names is illegal.
* @throws RepositoryException If the privilege could not be registered due
* to any implementation specific constraint violations or if persisting the
* custom privilege fails.
*/
Privilege registerPrivilege(String privilegeName, boolean isAbstract,
String[] declaredAggregateNames)
throws AccessDeniedException, NamespaceException, RepositoryException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import javax.jcr.security.Privilege;
import javax.security.auth.Subject;

import org.apache.jackrabbit.api.security.authorization.PrivilegeManager;
import org.apache.jackrabbit.core.config.WorkspaceConfig;
import org.apache.jackrabbit.core.id.ItemId;
import org.apache.jackrabbit.core.security.AMContext;
import org.apache.jackrabbit.core.security.AbstractAccessControlManager;
import org.apache.jackrabbit.core.security.AccessManager;
import org.apache.jackrabbit.core.security.SystemPrincipal;
import org.apache.jackrabbit.core.security.authorization.AccessControlProvider;
import org.apache.jackrabbit.core.security.authorization.PrivilegeManager;
import org.apache.jackrabbit.core.security.authorization.WorkspaceAccessManager;
import org.apache.jackrabbit.spi.Name;
import org.apache.jackrabbit.spi.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import javax.jcr.version.VersionManager;

import org.apache.jackrabbit.api.JackrabbitWorkspace;
import org.apache.jackrabbit.api.security.authorization.PrivilegeManager;
import org.apache.jackrabbit.commons.AbstractWorkspace;
import org.apache.jackrabbit.core.config.WorkspaceConfig;
import org.apache.jackrabbit.core.id.ItemId;
Expand All @@ -53,7 +54,6 @@
import org.apache.jackrabbit.core.observation.ObservationManagerImpl;
import org.apache.jackrabbit.core.query.QueryManagerImpl;
import org.apache.jackrabbit.core.retention.RetentionRegistry;
import org.apache.jackrabbit.core.security.authorization.PrivilegeManager;
import org.apache.jackrabbit.core.session.SessionContext;
import org.apache.jackrabbit.core.state.ItemStateCacheFactory;
import org.apache.jackrabbit.core.state.LocalItemStateManager;
Expand Down Expand Up @@ -344,6 +344,18 @@ public void createWorkspace(
workspaceName, configTemplate);
}

/**
* Return the <code>PrivilegeManager</code>.
*
* @return
* @throws RepositoryException
* @see org.apache.jackrabbit.api.JackrabbitWorkspace#getPrivilegeManager()
*/
public PrivilegeManager getPrivilegeManager() throws RepositoryException {
sanityCheck();
return context.getPrivilegeManager();
}


/**
* Returns the configuration of this workspace.
Expand Down Expand Up @@ -545,17 +557,6 @@ synchronized RetentionRegistry getRetentionRegistry() throws RepositoryException
return retentionRegistry;
}

/**
* Return the <code>PrivilegeManager</code>.
*
* @return
* @throws RepositoryException
*/
public PrivilegeManager getPrivilegeManager() throws RepositoryException {
sanityCheck();
return context.getPrivilegeManager();
}

//------------------------------------------------------------< Workspace >
/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package org.apache.jackrabbit.core.security;

import org.apache.jackrabbit.api.security.authorization.PrivilegeManager;
import org.apache.jackrabbit.core.HierarchyManager;
import org.apache.jackrabbit.core.fs.FileSystem;
import org.apache.jackrabbit.core.security.authorization.PrivilegeManager;
import org.apache.jackrabbit.spi.commons.conversion.NamePathResolver;

import javax.jcr.Session;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import org.apache.jackrabbit.api.security.JackrabbitAccessControlManager;
import org.apache.jackrabbit.api.security.JackrabbitAccessControlPolicy;
import org.apache.jackrabbit.api.security.authorization.PrivilegeManager;
import org.apache.jackrabbit.commons.iterator.AccessControlPolicyIteratorAdapter;
import org.apache.jackrabbit.core.security.authorization.Permission;
import org.apache.jackrabbit.core.security.authorization.PrivilegeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.jackrabbit.core.security;

import org.apache.jackrabbit.api.security.JackrabbitAccessControlPolicy;
import org.apache.jackrabbit.api.security.authorization.PrivilegeManager;
import org.apache.jackrabbit.commons.iterator.AccessControlPolicyIteratorAdapter;
import org.apache.jackrabbit.core.HierarchyManager;
import org.apache.jackrabbit.core.SessionImpl;
Expand All @@ -25,7 +26,7 @@
import org.apache.jackrabbit.core.security.authorization.AccessControlProvider;
import org.apache.jackrabbit.core.security.authorization.CompiledPermissions;
import org.apache.jackrabbit.core.security.authorization.Permission;
import org.apache.jackrabbit.core.security.authorization.PrivilegeManager;
import org.apache.jackrabbit.core.security.authorization.PrivilegeManagerImpl;
import org.apache.jackrabbit.core.security.authorization.PrivilegeRegistry;
import org.apache.jackrabbit.core.security.authorization.WorkspaceAccessManager;
import org.apache.jackrabbit.spi.Name;
Expand Down Expand Up @@ -277,7 +278,7 @@ public boolean hasPrivileges(String absPath, Privilege[] privileges) throws Path
log.debug("No privileges passed -> allowed.");
return true;
} else {
int privs = privilegeManager.getBits(privileges);
int privs = ((PrivilegeManagerImpl) privilegeManager).getBits(privileges);
Path p = resolver.getQPath(absPath);
return (compiledPermissions.getPrivileges(p) | ~privs) == -1;
}
Expand All @@ -292,7 +293,7 @@ public Privilege[] getPrivileges(String absPath) throws PathNotFoundException, R
int bits = compiledPermissions.getPrivileges(resolver.getQPath(absPath));
return (bits == PrivilegeRegistry.NO_PRIVILEGE) ?
new Privilege[0] :
privilegeManager.getPrivileges(bits);
((PrivilegeManagerImpl) privilegeManager).getPrivileges(bits);
}

/**
Expand Down Expand Up @@ -414,7 +415,7 @@ public boolean hasPrivileges(String absPath, Set<Principal> principals, Privileg
log.debug("No privileges passed -> allowed.");
return true;
} else {
int privs = privilegeManager.getBits(privileges);
int privs = ((PrivilegeManagerImpl) privilegeManager).getBits(privileges);
Path p = resolver.getQPath(absPath);
CompiledPermissions perms = acProvider.compilePermissions(principals);
try {
Expand All @@ -438,7 +439,7 @@ public Privilege[] getPrivileges(String absPath, Set<Principal> principals) thro
int bits = perms.getPrivileges(resolver.getQPath(absPath));
return (bits == PrivilegeRegistry.NO_PRIVILEGE) ?
new Privilege[0] :
privilegeManager.getPrivileges(bits);
((PrivilegeManagerImpl) privilegeManager).getPrivileges(bits);
} finally {
perms.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import javax.jcr.observation.ObservationManager;
import javax.jcr.security.Privilege;

import org.apache.jackrabbit.api.JackrabbitWorkspace;
import org.apache.jackrabbit.core.ItemImpl;
import org.apache.jackrabbit.core.NodeImpl;
import org.apache.jackrabbit.core.SessionImpl;
import org.apache.jackrabbit.core.WorkspaceImpl;
import org.apache.jackrabbit.core.id.ItemId;
import org.apache.jackrabbit.core.nodetype.NodeTypeImpl;
import org.apache.jackrabbit.core.security.SystemPrincipal;
Expand Down Expand Up @@ -96,7 +96,7 @@ public boolean grants(Path absPath, int permissions) {
return true;
}
public int getPrivileges(Path absPath) throws RepositoryException {
return ((WorkspaceImpl) session.getWorkspace()).getPrivilegeManager().getBits(new String[] {Privilege.JCR_ALL});
return getPrivilegeManagerImpl().getBits(new String[] {Privilege.JCR_ALL});
}
public boolean canReadAll() {
return true;
Expand Down Expand Up @@ -131,7 +131,7 @@ public int getPrivileges(Path absPath) throws RepositoryException {
if (isAcItem(absPath)) {
return PrivilegeRegistry.NO_PRIVILEGE;
} else {
return ((WorkspaceImpl) session.getWorkspace()).getPrivilegeManager().getBits(new String[] {Privilege.JCR_READ});
return getPrivilegeManagerImpl().getBits(new String[] {Privilege.JCR_READ});
}
}
public boolean canReadAll() {
Expand All @@ -147,6 +147,9 @@ public boolean canRead(Path itemPath, ItemId itemId) throws RepositoryException
};
}

private PrivilegeManagerImpl getPrivilegeManagerImpl() throws RepositoryException {
return (PrivilegeManagerImpl) ((JackrabbitWorkspace) session.getWorkspace()).getPrivilegeManager();
}
//-------------------------------------------------< AccessControlUtils >---
/**
* @see org.apache.jackrabbit.core.security.authorization.AccessControlUtils#isAcItem(Path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public Value getRestriction(Name restrictionName) {
/**
* @return The privilege manager in use.
*/
protected abstract PrivilegeManager getPrivilegeManager();
protected abstract PrivilegeManagerImpl getPrivilegeManager();

/**
* Build the hash code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.jackrabbit.core.security.authorization;

import org.apache.jackrabbit.api.security.authorization.PrivilegeManager;
import org.apache.jackrabbit.core.SessionImpl;
import org.apache.jackrabbit.spi.Name;
import org.apache.jackrabbit.spi.commons.conversion.IllegalNameException;
Expand All @@ -39,12 +40,12 @@
/**
* <code>PrivilegeManager</code>...
*/
public final class PrivilegeManager implements PrivilegeRegistry.Listener {
public final class PrivilegeManagerImpl implements PrivilegeManager, PrivilegeRegistry.Listener {

/**
* logger instance
*/
private static final Logger log = LoggerFactory.getLogger(PrivilegeManager.class);
private static final Logger log = LoggerFactory.getLogger(PrivilegeManagerImpl.class);

private static final Privilege[] EMPTY_ARRAY = new Privilege[0];

Expand All @@ -66,7 +67,7 @@ public final class PrivilegeManager implements PrivilegeRegistry.Listener {
*/
private final Map<Name, Privilege> cache;

public PrivilegeManager(PrivilegeRegistry registry, NameResolver nameResolver) {
public PrivilegeManagerImpl(PrivilegeRegistry registry, NameResolver nameResolver) {
this.registry = registry;
this.resolver = nameResolver;
this.cache = new HashMap<Name, Privilege>();
Expand All @@ -76,6 +77,7 @@ public PrivilegeManager(PrivilegeRegistry registry, NameResolver nameResolver) {
registry.addListener(this);
}

//---------------------------------------------------< PrivilegeManager >---
/**
* Returns all registered privileges.
*
Expand Down Expand Up @@ -165,6 +167,7 @@ public Privilege registerPrivilege(String privilegeName, boolean isAbstract,
return getPrivilege(privilegeName);
}

//-----------------------------< implementation specific public methods >---
/**
* @param privileges An array of privileges.
* @return The privilege bits.
Expand Down
Loading

0 comments on commit b0b0bed

Please sign in to comment.