incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jus...@apache.org
Subject svn commit: r956174 - in /sling/trunk: bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/resource/ launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/
Date Sat, 19 Jun 2010 02:11:16 GMT
Author: justin
Date: Sat Jun 19 02:11:16 2010
New Revision: 956174

URL: http://svn.apache.org/viewvc?rev=956174&view=rev
Log:
SLING-1557 - adding members and declaredMembers properties for groups and memberOf and declaredMemberOf
properties for users and groups

Modified:
    sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/resource/AuthorizableValueMap.java
    sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateGroupTest.java

Modified: sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/resource/AuthorizableValueMap.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/resource/AuthorizableValueMap.java?rev=956174&r1=956173&r2=956174&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/resource/AuthorizableValueMap.java
(original)
+++ sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/resource/AuthorizableValueMap.java
Sat Jun 19 02:11:16 2010
@@ -33,6 +33,7 @@ import javax.jcr.Value;
 import javax.jcr.ValueFormatException;
 
 import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.jackrabbit.api.security.user.Group;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.jcr.resource.JcrResourceUtil;
 import org.slf4j.Logger;
@@ -42,6 +43,15 @@ import org.slf4j.LoggerFactory;
  * ValueMap implementation for Authorizable Resources
  */
 public class AuthorizableValueMap implements ValueMap {
+
+    private static final String DECLARED_MEMBERS_KEY = "declaredMembers";
+
+    private static final String MEMBERS_KEY = "members";
+
+    private static final String DECLARED_MEMBER_OF_KEY = "declaredMemberOf";
+
+    private static final String MEMBER_OF_KEY = "memberOf";
+
     private Logger logger = LoggerFactory.getLogger(AuthorizableValueMap.class);
 
     private boolean fullyRead;
@@ -126,7 +136,6 @@ public class AuthorizableValueMap implem
     }
 
     protected Object read(String key) {
-
         // if the item has been completely read, we need not check
         // again, as we certainly will not find the key
         if (fullyRead) {
@@ -134,6 +143,19 @@ public class AuthorizableValueMap implem
         }
 
         try {
+            if (key.equals(MEMBERS_KEY) && authorizable.isGroup()) {
+                return getMembers((Group) authorizable, true);
+            }
+            if (key.equals(DECLARED_MEMBERS_KEY) && authorizable.isGroup()) {
+                return getMembers((Group) authorizable, false);
+            }
+            if (key.equals(MEMBER_OF_KEY)) {
+                return getMemberships(authorizable, true);
+            }
+            if (key.equals(DECLARED_MEMBER_OF_KEY)) {
+                return getMemberships(authorizable, false);
+            }
+
             if (authorizable.hasProperty(key)) {
                 final Value[] property = authorizable.getProperty(key);
                 final Object value = valuesToJavaObject(property);
@@ -167,6 +189,13 @@ public class AuthorizableValueMap implem
     protected void readFully() {
         if (!fullyRead) {
             try {
+                if (authorizable.isGroup()) {
+                    cache.put(MEMBERS_KEY, getMembers((Group) authorizable, true));
+                    cache.put(DECLARED_MEMBERS_KEY, getMembers((Group) authorizable, false));
+                }
+                cache.put(MEMBER_OF_KEY, getMemberships(authorizable, true));
+                cache.put(DECLARED_MEMBER_OF_KEY, getMemberships(authorizable, false));
+
                 Iterator pi = authorizable.getPropertyNames();
                 while (pi.hasNext()) {
                     String key = (String) pi.next();
@@ -319,4 +348,28 @@ public class AuthorizableValueMap implem
         return type;
     }
 
+    private String[] getMembers(Group group, boolean includeAll) throws RepositoryException
{
+        List<String> results = new ArrayList<String>();
+        for (Iterator<Authorizable> it = includeAll ? group.getMembers() : group.getDeclaredMembers();
+                it.hasNext();) {
+            Authorizable auth = it.next();
+            if (auth.isGroup()) {
+                results.add(AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PREFIX
+ auth.getID());
+            } else {
+                results.add(AuthorizableResourceProvider.SYSTEM_USER_MANAGER_USER_PREFIX
+ auth.getID());
+            }
+        }
+        return results.toArray(new String[results.size()]);
+    }
+
+    private String[] getMemberships(Authorizable authorizable, boolean includeAll) throws
RepositoryException {
+        List<String> results = new ArrayList<String>();
+        for (Iterator<Group> it = includeAll ? authorizable.memberOf() : authorizable.declaredMemberOf();
+                it.hasNext();) {
+            Group group = it.next();
+            results.add(AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PREFIX + group.getID());
+        }
+        return results.toArray(new String[results.size()]);
+    }
+
 }
\ No newline at end of file

Modified: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateGroupTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateGroupTest.java?rev=956174&r1=956173&r2=956174&view=diff
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateGroupTest.java
(original)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateGroupTest.java
Sat Jun 19 02:11:16 2010
@@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRes
 import org.apache.commons.httpclient.Credentials;
 import org.apache.commons.httpclient.NameValuePair;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.sling.commons.json.JSONArray;
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.JSONObject;
 
@@ -35,11 +36,20 @@ import org.apache.sling.commons.json.JSO
 public class UpdateGroupTest extends AbstractUserManagerTest {
 
 	String testGroupId = null;
-	
+
+	String testUserId = null;
+
 	@Override
 	protected void tearDown() throws Exception {
+        if (testUserId != null) {
+            //remove the test user if it exists.
+            String postUrl = HTTP_BASE_URL + "/system/userManager/user/" + testUserId + ".delete.html";
+            List<NameValuePair> postParams = new ArrayList<NameValuePair>();
+            assertAuthenticatedAdminPostStatus(postUrl, HttpServletResponse.SC_OK, postParams,
null);
+        }
+
 		if (testGroupId != null) {
-			//remove the test user if it exists.
+			//remove the test group if it exists.
 			String postUrl = HTTP_BASE_URL + "/system/userManager/group/" + testGroupId + ".delete.html";
 			List<NameValuePair> postParams = new ArrayList<NameValuePair>();
 			assertAuthenticatedAdminPostStatus(postUrl, HttpServletResponse.SC_OK, postParams, null);
@@ -50,16 +60,16 @@ public class UpdateGroupTest extends Abs
 
 	public void testUpdateGroup() throws IOException, JSONException {
 		testGroupId = createTestGroup();
-		
+
         String postUrl = HTTP_BASE_URL + "/system/userManager/group/" + testGroupId + ".update.html";
 
 		List<NameValuePair> postParams = new ArrayList<NameValuePair>();
 		postParams.add(new NameValuePair("displayName", "My Updated Test Group"));
 		postParams.add(new NameValuePair("url", "http://www.apache.org/updated"));
-		
+
 		Credentials creds = new UsernamePasswordCredentials("admin", "admin");
 		assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
-		
+
 		//fetch the user profile json to verify the settings
 		String getUrl = HTTP_BASE_URL + "/system/userManager/group/" + testGroupId + ".json";
 		assertAuthenticatedHttpStatus(creds, getUrl, HttpServletResponse.SC_OK, null); //make sure
the profile request returns some data
@@ -69,22 +79,67 @@ public class UpdateGroupTest extends Abs
 		assertEquals("My Updated Test Group", jsonObj.getString("displayName"));
 		assertEquals("http://www.apache.org/updated", jsonObj.getString("url"));
 	}
-	
+
 	public void testUpdateGroupMembers() throws IOException, JSONException {
 		testGroupId = createTestGroup();
-		
+		testUserId = createTestUser();
+
+        Credentials creds = new UsernamePasswordCredentials("admin", "admin");
+
+		// verify that the members array exists, but is empty
+		JSONArray members = getTestGroupMembers(creds);
+        assertEquals(0, members.length());
+
+        JSONArray memberships = getTestUserMemberships(creds);
+        assertEquals(0, memberships.length());
+
         String postUrl = HTTP_BASE_URL + "/system/userManager/group/" + testGroupId + ".update.html";
 
-        //TODO: verify this works....
+        // add a group member
 		List<NameValuePair> postParams = new ArrayList<NameValuePair>();
-		postParams.add(new NameValuePair(":member", "../user/testUser"));
-		postParams.add(new NameValuePair(":member@Delete", "testGroup"));
-		
-		Credentials creds = new UsernamePasswordCredentials("admin", "admin");
-		assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
-		
-        //TODO: verify the group membership is correct....
+		postParams.add(new NameValuePair(":member", testUserId));
+        assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams,
null);
+
+        members = getTestGroupMembers(creds);
+        assertEquals(1, members.length());
+        assertEquals("/system/userManager/user/" + testUserId, members.getString(0));
+
+        memberships = getTestUserMemberships(creds);
+        assertEquals(1, memberships.length());
+        assertEquals("/system/userManager/group/" + testGroupId, memberships.getString(0));
+
+        // delete a group member
+		postParams.clear();
+		postParams.add(new NameValuePair(":member@Delete", testUserId));
+        assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams,
null);
+
+        members = getTestGroupMembers(creds);
+        assertEquals(0, members.length());
+
+        memberships = getTestUserMemberships(creds);
+        assertEquals(0, memberships.length());
+
 	}
-	
+
+	JSONArray getTestUserMemberships(Credentials creds) throws IOException, JSONException {
+	    String getUrl = HTTP_BASE_URL + "/system/userManager/user/" + testUserId + ".json";
+        assertAuthenticatedHttpStatus(creds, getUrl, HttpServletResponse.SC_OK, null); //make
sure the profile request returns some data
+        String json = getAuthenticatedContent(creds, getUrl, CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
+        assertNotNull(json);
+        JSONObject jsonObj = new JSONObject(json);
+        JSONArray memberships = jsonObj.getJSONArray("memberOf");
+        return memberships;
+    }
+
+    JSONArray getTestGroupMembers(Credentials creds) throws IOException, JSONException {
+        String getUrl = HTTP_BASE_URL + "/system/userManager/group/" + testGroupId + ".json";
+		assertAuthenticatedHttpStatus(creds, getUrl, HttpServletResponse.SC_OK, null); //make sure
the profile request returns some data
+        String json = getAuthenticatedContent(creds, getUrl, CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
+        assertNotNull(json);
+        JSONObject jsonObj = new JSONObject(json);
+        JSONArray members = jsonObj.getJSONArray("members");
+        return members;
+    }
+
 }
 



Mime
View raw message