Skip to content

Commit 1413e93

Browse files
committed
#503 Improved documentation
1 parent dd13954 commit 1413e93

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/authorizableinstaller/impl/PrefetchingUserManager.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,27 @@
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626

27-
import com.google.common.annotations.VisibleForTesting;
28-
2927
import biz.netcentric.cq.tools.actool.helper.Constants;
3028
import biz.netcentric.cq.tools.actool.history.InstallationLogger;
3129

32-
/** Prefetches a) all groups and system users (but not regular users) and b) all memberships between these authorizables in constructor. */
30+
/**
31+
* <p>
32+
* Special user manager that prefetches
33+
* <ul>
34+
* <li>all groups, system users and anonymous (but not regular users)</li>
35+
* <li>all memberships between the preloaded authorizables</li>
36+
* <ul>
37+
* upon creation.
38+
* </p>
39+
* <p>
40+
* The membership relationships are cached in lookup maps to quickly be able to query the repository state for both
41+
* authorizable.declaredMemberOf() (this we call in this class also) and group.getDeclaredMembers(). Having called declaredMemberOf() for
42+
* all groups of the repository has also retrieved all memberships (expect regular user memberships), hence the method getDeclaredMembers()
43+
* does not have to be called anymore. This is particularly useful because that way the AC tool does not have to iterate over a potentially
44+
* huge number of users in production to then only filter out a few relevant groups in the end (the AC Tool does not touch user memberships
45+
* to groups).
46+
* </p>
47+
*/
3348
class PrefetchingUserManager implements UserManager {
3449

3550
private static final Logger LOG = LoggerFactory.getLogger(PrefetchingUserManager.class);
@@ -45,7 +60,7 @@ public PrefetchingUserManager(UserManager delegate, final ValueFactory valueFact
4560
this.delegate = delegate;
4661

4762
long startPrefetch = System.currentTimeMillis();
48-
Iterator<Authorizable> allAuthorizables = delegate.findAuthorizables(new Query() {
63+
Iterator<Authorizable> allGroupsAndSystemUsersAndAnonymous = delegate.findAuthorizables(new Query() {
4964
public <T> void build(QueryBuilder<T> builder) {
5065
builder.setCondition(
5166
builder.or( //
@@ -54,8 +69,8 @@ public <T> void build(QueryBuilder<T> builder) {
5469
);
5570
}
5671
});
57-
while (allAuthorizables.hasNext()) {
58-
Authorizable auth = allAuthorizables.next();
72+
while (allGroupsAndSystemUsersAndAnonymous.hasNext()) {
73+
Authorizable auth = allGroupsAndSystemUsersAndAnonymous.next();
5974
String authId = auth.getID();
6075
authorizableCache.put(authId, auth);
6176

@@ -96,11 +111,11 @@ public Authorizable getAuthorizable(String id) throws RepositoryException {
96111
}
97112

98113
public Set<String> getDeclaredIsMemberOf(String id) throws RepositoryException {
99-
100-
if(isMemberOfByAuthorizableId.containsKey(id)) {
114+
115+
if (isMemberOfByAuthorizableId.containsKey(id)) {
101116
return isMemberOfByAuthorizableId.get(id);
102117
} else {
103-
// for users fall back to retrieve on demand
118+
// for users fall back to retrieve on demand
104119
Set<String> memberOfSet = new HashSet<String>();
105120
Iterator<Group> memberOfIt = getAuthorizable(id).declaredMemberOf();
106121
while (memberOfIt.hasNext()) {
@@ -112,7 +127,8 @@ public Set<String> getDeclaredIsMemberOf(String id) throws RepositoryException {
112127
}
113128

114129
public Set<String> getDeclaredMembersWithoutRegularUsers(String id) {
115-
return nonRegularUserMembersByAuthorizableId.containsKey(id) ? nonRegularUserMembersByAuthorizableId.get(id): Collections.<String>emptySet();
130+
return nonRegularUserMembersByAuthorizableId.containsKey(id) ? nonRegularUserMembersByAuthorizableId.get(id)
131+
: Collections.<String> emptySet();
116132
}
117133

118134
public int getCacheSize() {

accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/authorizableinstaller/impl/PrefetchingUserManagerTest.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public class PrefetchingUserManagerTest {
3636

3737
@Mock
3838
ValueFactory valueFactory;
39-
39+
4040
@Mock
4141
InstallationLogger installationLogger;
42-
42+
4343
@Mock
4444
Group group1;
4545

@@ -48,22 +48,22 @@ public class PrefetchingUserManagerTest {
4848

4949
@Mock
5050
Group group3;
51-
51+
5252
@Mock
5353
User user1;
54-
54+
5555
@Mock
5656
User user2;
5757

5858
@Mock
5959
User userNonCached;
60-
60+
6161
PrefetchingUserManager prefetchingUserManager;
62-
62+
6363
@Before
6464
public void before() throws RepositoryException {
65-
66-
setupAuthorizable(group1, "group1", Collections.<Group>emptyList());
65+
66+
setupAuthorizable(group1, "group1", Collections.<Group> emptyList());
6767
setupAuthorizable(group2, "group2", Arrays.asList(group1));
6868
setupAuthorizable(group3, "group3", Arrays.asList(group2));
6969
setupAuthorizable(user1, "user1", Arrays.asList(group3));
@@ -72,7 +72,7 @@ public void before() throws RepositoryException {
7272

7373
when(userManager.findAuthorizables(any(Query.class))).thenReturn(Arrays.asList(group1, group2, group3, user1, user2).iterator());
7474
}
75-
75+
7676
private void setupAuthorizable(Authorizable auth, String id, List<Group> memberOf) throws RepositoryException {
7777
when(auth.getID()).thenReturn(id);
7878
when(auth.declaredMemberOf()).thenReturn(memberOf.iterator());
@@ -81,7 +81,7 @@ private void setupAuthorizable(Authorizable auth, String id, List<Group> memberO
8181
@Test
8282
public void testPrefetchedAuthorizablesUserManager() throws RepositoryException {
8383
prefetchingUserManager = new PrefetchingUserManager(userManager, valueFactory, installationLogger);
84-
84+
8585
assertEquals(5, prefetchingUserManager.getCacheSize());
8686
assertEquals(group1, prefetchingUserManager.getAuthorizable("group1"));
8787
assertEquals(null, prefetchingUserManager.getAuthorizable("userNonCached"));
@@ -99,60 +99,52 @@ public void testDelegation() throws RepositoryException {
9999
prefetchingUserManager.getAuthorizable(testPrincipal);
100100
verify(userManager, times(1)).getAuthorizable(testPrincipal);
101101

102-
103-
104102
String userPath = "/home/users/path";
105103
prefetchingUserManager.getAuthorizableByPath(userPath);
106104
verify(userManager, times(1)).getAuthorizableByPath(userPath);
107-
105+
108106
String relPath = "profile/test";
109107
String testVal = "testval";
110108
prefetchingUserManager.findAuthorizables(relPath, testVal);
111109
verify(userManager, times(1)).findAuthorizables(relPath, testVal);
112110
prefetchingUserManager.findAuthorizables(relPath, testVal, UserManager.SEARCH_TYPE_AUTHORIZABLE);
113111
verify(userManager, times(1)).findAuthorizables(relPath, testVal, UserManager.SEARCH_TYPE_AUTHORIZABLE);
114-
112+
115113
Query testQuery = new Query() {
116114
public <T> void build(QueryBuilder<T> builder) {
117115
// fetch all authorizables
118116
}
119117
};
120118
prefetchingUserManager.findAuthorizables(testQuery);
121119
verify(userManager, times(1)).findAuthorizables(testQuery);
122-
120+
123121
String testpw = "pw";
124122
prefetchingUserManager.createUser(testuser, testpw);
125123
verify(userManager, times(1)).createUser(testuser, testpw);
126124

127125
prefetchingUserManager.createUser(testuser, testpw, testPrincipal, userPath);
128126
verify(userManager, times(1)).createUser(testuser, testpw, testPrincipal, userPath);
129-
127+
130128
prefetchingUserManager.createSystemUser(testuser, userPath);
131129
verify(userManager, times(1)).createSystemUser(testuser, userPath);
132-
130+
133131
prefetchingUserManager.createGroup(testuser);
134132
verify(userManager, times(1)).createGroup(testuser);
135133

136134
prefetchingUserManager.createGroup(testPrincipal);
137135
verify(userManager, times(1)).createGroup(testPrincipal);
138-
136+
139137
prefetchingUserManager.createGroup(testPrincipal, userPath);
140138
verify(userManager, times(1)).createGroup(testPrincipal, userPath);
141139

142140
prefetchingUserManager.createGroup(testuser, testPrincipal, userPath);
143141
verify(userManager, times(1)).createGroup(testuser, testPrincipal, userPath);
144-
142+
145143
prefetchingUserManager.autoSave(true);
146144
verify(userManager, times(1)).autoSave(true);
147-
145+
148146
prefetchingUserManager.isAutoSave();
149147
verify(userManager, times(1)).isAutoSave();
150148
}
151149

152-
153-
154-
155-
156-
157-
158150
}

0 commit comments

Comments
 (0)