diff --git jackrabbit-api/src/main/java/org/apache/jackrabbit/api/security/user/UserManager.java jackrabbit-api/src/main/java/org/apache/jackrabbit/api/security/user/UserManager.java index e2e7157..2208531 100644 --- jackrabbit-api/src/main/java/org/apache/jackrabbit/api/security/user/UserManager.java +++ jackrabbit-api/src/main/java/org/apache/jackrabbit/api/security/user/UserManager.java @@ -68,6 +68,17 @@ public interface UserManager { Authorizable getAuthorizable(String id) throws RepositoryException; /** + * Get the Authorizable of a specific type by its id. + * + * @param id the user of group id. + * @param authorizableClass the type of Authorizable required. + * @return Authorizable or null, if not present. + * @throws AuthorizableTypeException If an authorizable exists but is not of the requested type. + * @throws RepositoryException If an error occurs + */ + T getAuthorizable(String id, Class authorizableClass) throws AuthorizableTypeException, RepositoryException; + + /** * Get the Authorizable by its Principal. * * @param principal The principal of the authorizable to retrieve. @@ -77,6 +88,17 @@ public interface UserManager { Authorizable getAuthorizable(Principal principal) throws RepositoryException; /** + * Get the Authorizable of a specific type by its Principal. + * + * @param principal The principal of the Authorizable to retrieve. + * @param authorizableClass the type of Authorizable required. + * @return Authorizable or null, if not present. + * @throws AuthorizableTypeException If an authorizable exists but is not of the requested type. + * @throws RepositoryException If an error occurs. + */ + T getAuthorizable(Principal principal, Class authorizableClass) throws AuthorizableTypeException, RepositoryException; + + /** * In accordance to {@link org.apache.jackrabbit.api.security.user.Authorizable#getPath()} * this method allows to retrieve an given authorizable by it's path. * @@ -90,6 +112,18 @@ public interface UserManager { Authorizable getAuthorizableByPath(String path) throws UnsupportedRepositoryOperationException, RepositoryException; /** + * In accordance to {@link org.apache.jackrabbit.api.security.user.Authorizable#getPath()} + * this method allows to retrieve an authorizable of a specific type by it's path. + * + * @param path The path to an authorizable. + * @param authorizableClass the type of Authorizable required. + * @return Authorizable or null, if not present. + * @throws AuthorizableTypeException If an authorizable exists but is not of the requested type. + * @throws RepositoryException If another error occurs. + */ + T getAuthorizableByPath(String path, Class authorizableClass) throws AuthorizableTypeException, RepositoryException; + + /** * Returns all Authorizables that have a * {@link Authorizable#getProperty(String) property} with the given relative * path (or name) that matches the specified value. diff --git jackrabbit-api/src/main/java/org/apache/jackrabbit/api/security/user/package-info.java jackrabbit-api/src/main/java/org/apache/jackrabbit/api/security/user/package-info.java index 7dfcdf8..21316bf 100644 --- jackrabbit-api/src/main/java/org/apache/jackrabbit/api/security/user/package-info.java +++ jackrabbit-api/src/main/java/org/apache/jackrabbit/api/security/user/package-info.java @@ -18,5 +18,5 @@ /** * Jackrabbit extensions for user management. */ -@aQute.bnd.annotation.Version("2.3.2") +@aQute.bnd.annotation.Version("2.4.0") package org.apache.jackrabbit.api.security.user; diff --git jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserManagerImpl.java jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserManagerImpl.java index 7a0a656..bdfcaf0 100644 --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserManagerImpl.java +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserManagerImpl.java @@ -19,6 +19,7 @@ package org.apache.jackrabbit.core.security.user; import org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal; import org.apache.jackrabbit.api.security.user.Authorizable; import org.apache.jackrabbit.api.security.user.AuthorizableExistsException; +import org.apache.jackrabbit.api.security.user.AuthorizableTypeException; import org.apache.jackrabbit.api.security.user.Group; import org.apache.jackrabbit.api.security.user.Query; import org.apache.jackrabbit.api.security.user.User; @@ -451,6 +452,13 @@ public class UserManagerImpl extends ProtectedItemModifier } /** + * @see UserManager#getAuthorizable(String, Class) + */ + public T getAuthorizable(String id, Class authorizableClass) throws AuthorizableTypeException, RepositoryException { + return getAuthorizableByType(getAuthorizable(id), authorizableClass); + } + + /** * @see UserManager#getAuthorizable(Principal) */ public Authorizable getAuthorizable(Principal principal) throws RepositoryException { @@ -490,6 +498,13 @@ public class UserManagerImpl extends ProtectedItemModifier } /** + * @see UserManager#getAuthorizable(Principal, Class) + */ + public T getAuthorizable(Principal principal, Class authorizableClass) throws AuthorizableTypeException, RepositoryException { + return getAuthorizableByType(getAuthorizable(principal), authorizableClass); + } + + /** * Always throws UnsupportedRepositoryOperationException since * this implementation of the user management API does not allow to retrieve * the path of an authorizable. @@ -501,6 +516,13 @@ public class UserManagerImpl extends ProtectedItemModifier } /** + * @see UserManager#getAuthorizableByPath(String, Class) + */ + public T getAuthorizableByPath(String path, Class authorizableClass) throws AuthorizableTypeException, RepositoryException { + return getAuthorizableByType(getAuthorizableByPath(path), authorizableClass); + } + + /** * @see UserManager#findAuthorizables(String,String) */ public Iterator findAuthorizables(String relPath, String value) throws RepositoryException { @@ -911,6 +933,13 @@ public class UserManagerImpl extends ProtectedItemModifier return getAuthorizable(n); } + private T getAuthorizableByType(Authorizable authorizable, Class authorizableClass) throws AuthorizableTypeException { + if (authorizable == null || authorizableClass.isInstance(authorizable)) { + return authorizableClass.cast(authorizable); + } + throw new AuthorizableTypeException("Invalid authorizable type '" + authorizable.getClass() + "'"); + } + private Value getValue(String strValue) { return session.getValueFactory().createValue(strValue); } diff --git jackrabbit-core/src/test/java/org/apache/jackrabbit/api/security/user/UserManagerTest.java jackrabbit-core/src/test/java/org/apache/jackrabbit/api/security/user/UserManagerTest.java index fdc5446..b43c02e 100644 --- jackrabbit-core/src/test/java/org/apache/jackrabbit/api/security/user/UserManagerTest.java +++ jackrabbit-core/src/test/java/org/apache/jackrabbit/api/security/user/UserManagerTest.java @@ -70,4 +70,51 @@ public class UserManagerTest extends AbstractUserTest { throw new NotExecutableException(); } } + + public void testGetAuthorizableByPathAndType() throws NotExecutableException, RepositoryException { + String uid = superuser.getUserID(); + Authorizable a = userMgr.getAuthorizable(uid); + if (a == null) { + throw new NotExecutableException(); + } + try { + String path = a.getPath(); + Authorizable a2 = userMgr.getAuthorizableByPath(path, a.getClass()); + assertNotNull(a2); + assertEquals(a.getID(), a2.getID()); + } catch (UnsupportedRepositoryOperationException e) { + throw new NotExecutableException(); + } + } + + public void testGetAuthorizableByIdAndType() throws NotExecutableException, RepositoryException { + for (Principal principal : getPrincipalSetFromSession(superuser)) { + Principal p = principal; + Authorizable auth = userMgr.getAuthorizable(p); + if (auth != null) { + Authorizable authByID = userMgr.getAuthorizable(auth.getID(), auth.getClass()); + assertEquals("Equal ID expected", auth.getID(), authByID.getID()); + } + } + } + + public void testGetAuthorizableByIdAndWrongType() throws NotExecutableException, RepositoryException { + for (Principal principal : getPrincipalSetFromSession(superuser)) { + Principal p = principal; + Authorizable auth = userMgr.getAuthorizable(p); + if (auth != null) { + Class otherType = auth.isGroup() ? User.class : Group.class; + try { + userMgr.getAuthorizable(auth.getID(), otherType); + fail("Wrong Authorizable type is not detected."); + } catch (AuthorizableTypeException ignore) { + } + } + } + } + + public void testGetNonExistingAuthorizableByIdAndType() throws NotExecutableException, RepositoryException { + Authorizable auth = userMgr.getAuthorizable("nonExistingAuthorizable", User.class); + assertNull(auth); + } } \ No newline at end of file diff --git jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java index 651cb3d..ee61c1a 100644 --- jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java +++ jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java @@ -21,6 +21,7 @@ import org.apache.jackrabbit.api.security.principal.PrincipalIterator; import org.apache.jackrabbit.api.security.principal.PrincipalManager; import org.apache.jackrabbit.api.security.user.Authorizable; import org.apache.jackrabbit.api.security.user.AuthorizableExistsException; +import org.apache.jackrabbit.api.security.user.AuthorizableTypeException; import org.apache.jackrabbit.api.security.user.Group; import org.apache.jackrabbit.api.security.user.Impersonation; import org.apache.jackrabbit.api.security.user.Query; @@ -1593,14 +1594,26 @@ public class UserImporterTest extends AbstractJCRTest { return null; } + public T getAuthorizable(String id, Class authorizableClass) throws AuthorizableTypeException, RepositoryException { + return null; + } + public Authorizable getAuthorizable(Principal principal) throws RepositoryException { return null; } + public T getAuthorizable(Principal principal, Class authorizableClass) throws AuthorizableTypeException, RepositoryException { + return null; + } + public Authorizable getAuthorizableByPath(String path) throws UnsupportedRepositoryOperationException, RepositoryException { return null; } + public T getAuthorizableByPath(String path, Class authorizableClass) throws AuthorizableTypeException, RepositoryException { + return null; + } + public Iterator findAuthorizables(String relPath, String value) throws RepositoryException { return null; }