Skip to content

Commit 3b96771

Browse files
committed
#503 Using own interface AuthInstallerUserManager to avoid future
problems for in case oak UserManager interface gets extended
1 parent 1413e93 commit 3b96771

File tree

5 files changed

+68
-100
lines changed

5 files changed

+68
-100
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package biz.netcentric.cq.tools.actool.authorizableinstaller.impl;
2+
3+
import java.security.Principal;
4+
import java.util.Set;
5+
6+
import javax.jcr.RepositoryException;
7+
8+
import org.apache.jackrabbit.api.security.user.Authorizable;
9+
import org.apache.jackrabbit.api.security.user.Group;
10+
import org.apache.jackrabbit.api.security.user.User;
11+
import org.apache.jackrabbit.api.security.user.UserManager;
12+
13+
/** Replicates the methods from {@link UserManager} but does not extend this interface because {@link UserManager} is a provider type that
14+
* may receive additional methods in the future (and hence it would break this code). See {@link AuthInstallerUserManagerPrefetchingImpl}
15+
* for the motivation to use this special user manager (performance). */
16+
interface AuthInstallerUserManager {
17+
18+
// -- AC Tool special methods
19+
Set<String> getDeclaredIsMemberOf(String id) throws RepositoryException;
20+
21+
Set<String> getDeclaredMembersWithoutRegularUsers(String id);
22+
23+
UserManager getOakUserManager();
24+
25+
// -- methods from oak UserManager that the AC tool uses (delegated, only the relevant methods are listed here)
26+
27+
Authorizable getAuthorizable(String id) throws RepositoryException;
28+
29+
User createUser(String userID, String password, Principal principal, String intermediatePath) throws RepositoryException;
30+
31+
User createSystemUser(String userID, String intermediatePath) throws RepositoryException;
32+
33+
Group createGroup(Principal principal) throws RepositoryException;
34+
35+
Group createGroup(Principal principal, String intermediatePath) throws RepositoryException;
36+
37+
}

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

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@
4545
* to groups).
4646
* </p>
4747
*/
48-
class PrefetchingUserManager implements UserManager {
48+
class AuthInstallerUserManagerPrefetchingImpl implements AuthInstallerUserManager {
4949

50-
private static final Logger LOG = LoggerFactory.getLogger(PrefetchingUserManager.class);
50+
private static final Logger LOG = LoggerFactory.getLogger(AuthInstallerUserManagerPrefetchingImpl.class);
5151

5252
private final UserManager delegate;
5353
private final Map<String, Authorizable> authorizableCache = new HashMap<>();
5454

5555
private final Map<String, Set<String>> nonRegularUserMembersByAuthorizableId = new HashMap<>();
5656
private final Map<String, Set<String>> isMemberOfByAuthorizableId = new HashMap<>();
5757

58-
public PrefetchingUserManager(UserManager delegate, final ValueFactory valueFactory, InstallationLogger installLog)
58+
public AuthInstallerUserManagerPrefetchingImpl(UserManager delegate, final ValueFactory valueFactory, InstallationLogger installLog)
5959
throws RepositoryException {
6060
this.delegate = delegate;
6161

6262
long startPrefetch = System.currentTimeMillis();
63-
Iterator<Authorizable> allGroupsAndSystemUsersAndAnonymous = delegate.findAuthorizables(new Query() {
63+
Iterator<Authorizable> authorizablesToPrefetchIt = delegate.findAuthorizables(new Query() {
6464
public <T> void build(QueryBuilder<T> builder) {
6565
builder.setCondition(
6666
builder.or( //
@@ -69,8 +69,8 @@ public <T> void build(QueryBuilder<T> builder) {
6969
);
7070
}
7171
});
72-
while (allGroupsAndSystemUsersAndAnonymous.hasNext()) {
73-
Authorizable auth = allGroupsAndSystemUsersAndAnonymous.next();
72+
while (authorizablesToPrefetchIt.hasNext()) {
73+
Authorizable auth = authorizablesToPrefetchIt.next();
7474
String authId = auth.getID();
7575
authorizableCache.put(authId, auth);
7676

@@ -100,7 +100,6 @@ public <T> void build(QueryBuilder<T> builder) {
100100
+ msHumanReadable(System.currentTimeMillis() - startPrefetchMemberships));
101101
}
102102

103-
@Override
104103
public Authorizable getAuthorizable(String id) throws RepositoryException {
105104
Authorizable authorizable = authorizableCache.get(id);
106105
if (authorizable == null) {
@@ -137,28 +136,8 @@ public int getCacheSize() {
137136

138137
// --- delegated methods
139138

140-
public Authorizable getAuthorizable(Principal principal) throws RepositoryException {
141-
return delegate.getAuthorizable(principal);
142-
}
143-
144-
public Authorizable getAuthorizableByPath(String path) throws RepositoryException {
145-
return delegate.getAuthorizableByPath(path);
146-
}
147-
148-
public Iterator<Authorizable> findAuthorizables(String relPath, String value) throws RepositoryException {
149-
return delegate.findAuthorizables(relPath, value);
150-
}
151-
152-
public Iterator<Authorizable> findAuthorizables(String relPath, String value, int searchType) throws RepositoryException {
153-
return delegate.findAuthorizables(relPath, value, searchType);
154-
}
155-
156-
public Iterator<Authorizable> findAuthorizables(Query query) throws RepositoryException {
157-
return delegate.findAuthorizables(query);
158-
}
159-
160-
public User createUser(String userID, String password) throws RepositoryException {
161-
return delegate.createUser(userID, password);
139+
public UserManager getOakUserManager() {
140+
return delegate;
162141
}
163142

164143
public User createUser(String userID, String password, Principal principal, String intermediatePath) throws RepositoryException {
@@ -169,28 +148,12 @@ public User createSystemUser(String userID, String intermediatePath) throws Repo
169148
return delegate.createSystemUser(userID, intermediatePath);
170149
}
171150

172-
public Group createGroup(String groupID) throws RepositoryException {
173-
return delegate.createGroup(groupID);
174-
}
175-
176151
public Group createGroup(Principal principal) throws RepositoryException {
177152
return delegate.createGroup(principal);
178153
}
179-
154+
180155
public Group createGroup(Principal principal, String intermediatePath) throws RepositoryException {
181156
return delegate.createGroup(principal, intermediatePath);
182157
}
183158

184-
public Group createGroup(String groupID, Principal principal, String intermediatePath) throws RepositoryException {
185-
return delegate.createGroup(groupID, principal, intermediatePath);
186-
}
187-
188-
public boolean isAutoSave() {
189-
return delegate.isAutoSave();
190-
}
191-
192-
public void autoSave(boolean enable) throws RepositoryException {
193-
delegate.autoSave(enable);
194-
}
195-
196159
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void installAuthorizables(
111111
final Session session, InstallationLogger installLog)
112112
throws RepositoryException, AuthorizableCreatorException, LoginException, IOException, GeneralSecurityException {
113113

114-
PrefetchingUserManager userManager = new PrefetchingUserManager(AccessControlUtils.getUserManagerAutoSaveDisabled(session), session.getValueFactory(), installLog);
114+
AuthInstallerUserManager userManager = new AuthInstallerUserManagerPrefetchingImpl(AccessControlUtils.getUserManagerAutoSaveDisabled(session), session.getValueFactory(), installLog);
115115

116116
Set<String> authorizablesFromConfigurations = authorizablesConfigBeans.getAuthorizableIds();
117117
for (AuthorizableConfigBean authorizableConfigBean : authorizablesConfigBeans) {
@@ -125,7 +125,7 @@ public void installAuthorizables(
125125
}
126126

127127
private void installAuthorizableConfigurationBean(final Session session,
128-
PrefetchingUserManager userManager,
128+
AuthInstallerUserManager userManager,
129129
AcConfiguration acConfiguration,
130130
AuthorizableConfigBean authorizableConfigBean,
131131
InstallationLogger installLog, Set<String> authorizablesFromConfigurations)
@@ -269,7 +269,7 @@ private String getPassword(final AuthorizableConfigBean authorizableConfigBean)
269269
* regular relationships between groups contained in config are kept in isMemberOf */
270270
@SuppressWarnings("unchecked")
271271
void applyGroupMembershipConfigMembers(AcConfiguration acConfiguration, AuthorizableConfigBean authorizableConfigBean, InstallationLogger installLog,
272-
String authorizableId, PrefetchingUserManager userManager, Set<String> authorizablesFromConfigurations) throws RepositoryException {
272+
String authorizableId, AuthInstallerUserManager userManager, Set<String> authorizablesFromConfigurations) throws RepositoryException {
273273
if (authorizableConfigBean.isGroup()) {
274274

275275
String[] membersInConfigArr = authorizableConfigBean.getMembers();
@@ -351,7 +351,7 @@ private Set<String> removeExternalMembersUnmanagedByConfiguration(AcConfiguratio
351351
}
352352

353353

354-
private void migrateFromOldGroup(AuthorizableConfigBean authorizableConfigBean, UserManager userManager,
354+
private void migrateFromOldGroup(AuthorizableConfigBean authorizableConfigBean, AuthInstallerUserManager userManager,
355355
InstallationLogger installLog) throws RepositoryException {
356356
Authorizable groupForMigration = userManager.getAuthorizable(authorizableConfigBean.getMigrateFrom());
357357

@@ -399,7 +399,7 @@ private void handleRecreationOfAuthorizableIfNecessary(final Session session,
399399
AcConfiguration acConfiguration,
400400
AuthorizableConfigBean principalConfigBean,
401401
InstallationLogger installLog,
402-
UserManager userManager) throws RepositoryException, AuthorizableCreatorException {
402+
AuthInstallerUserManager userManager) throws RepositoryException, AuthorizableCreatorException {
403403

404404
String authorizableId = principalConfigBean.getAuthorizableId();
405405

@@ -504,7 +504,7 @@ private void deleteOldIntermediatePath(final Session session,
504504

505505
private void applyGroupMembershipConfigIsMemberOf(InstallationLogger installLog,
506506
AcConfiguration acConfiguration,
507-
AuthorizableConfigBean authorizableConfigBean, PrefetchingUserManager userManager, Session session,
507+
AuthorizableConfigBean authorizableConfigBean, AuthInstallerUserManager userManager, Session session,
508508
Set<String> authorizablesFromConfigurations) throws RepositoryException, AuthorizableCreatorException {
509509

510510
String authId = authorizableConfigBean.getAuthorizableId();
@@ -520,7 +520,7 @@ private Authorizable createNewAuthorizable(
520520
AcConfiguration acConfiguration,
521521
AuthorizableConfigBean principalConfigBean,
522522
InstallationLogger installLog,
523-
UserManager userManager, Session session)
523+
AuthInstallerUserManager userManager, Session session)
524524
throws AuthorizableExistsException, RepositoryException,
525525
AuthorizableCreatorException {
526526

@@ -561,7 +561,7 @@ private Set<String> getMembershipGroupsFromConfig(String[] memberOf) {
561561
@SuppressWarnings("unchecked")
562562
void applyGroupMembershipConfigIsMemberOf(AuthorizableConfigBean authorizableConfigBean,
563563
AcConfiguration acConfiguration,
564-
InstallationLogger installLog, UserManager userManager, Session session,
564+
InstallationLogger installLog, AuthInstallerUserManager userManager, Session session,
565565
Set<String> membershipGroupsFromConfig,
566566
Set<String> membershipGroupsFromRepository, Set<String> authorizablesFromConfigurations)
567567
throws RepositoryException, AuthorizableExistsException,
@@ -637,7 +637,7 @@ void applyGroupMembershipConfigIsMemberOf(AuthorizableConfigBean authorizableCon
637637
}
638638

639639
private Authorizable createNewGroup(
640-
final UserManager userManager,
640+
final AuthInstallerUserManager userManager,
641641
AuthorizablesConfig authorizablesConfig,
642642
AuthorizableConfigBean principalConfigBean,
643643
InstallationLogger installLog, Session session)
@@ -657,7 +657,7 @@ private Authorizable createNewGroup(
657657
throw new IllegalStateException("External IDs are not available for your AEM version ("
658658
+ principalConfigBean.getAuthorizableId() + " is using '" + principalConfigBean.getExternalId() + "')");
659659
}
660-
newGroup = (Group) externalGroupCreatorService.createGroupWithExternalId(userManager, principalConfigBean, installLog, session);
660+
newGroup = (Group) externalGroupCreatorService.createGroupWithExternalId(userManager.getOakUserManager(), principalConfigBean, installLog, session);
661661
LOG.info("Successfully created new external group: {}", groupID);
662662
} else {
663663

@@ -776,7 +776,7 @@ void setAuthorizableProperties(Authorizable authorizable, AuthorizableConfigBean
776776
}
777777

778778
private Authorizable createNewUser(
779-
final UserManager userManager,
779+
final AuthInstallerUserManager userManager,
780780
AuthorizablesConfig authorizablesConfig,
781781
AuthorizableConfigBean principalConfigBean,
782782
InstallationLogger installLog,
@@ -807,7 +807,7 @@ private Authorizable createNewUser(
807807
}
808808

809809
private void addMembersToReferencingAuthorizables(Authorizable authorizable, AuthorizablesConfig authorizablesConfig, AuthorizableConfigBean principalConfigBean,
810-
final UserManager userManager, Session session, InstallationLogger installLog)
810+
final AuthInstallerUserManager userManager, Session session, InstallationLogger installLog)
811811
throws RepositoryException, AuthorizableCreatorException {
812812
String authorizableId = principalConfigBean.getAuthorizableId();
813813
String[] memberOf = principalConfigBean.getIsMemberOf();
@@ -839,7 +839,7 @@ private void addMembersToReferencingAuthorizables(Authorizable authorizable, Aut
839839
* @throws RepositoryException
840840
* @throws AuthorizableCreatorException if one of the authorizables contained in membersOf array is a user */
841841
Set<String> validateAssignedGroups(
842-
final UserManager userManager, AuthorizablesConfig authorizablesConfig, Session session, final String authorizablelId,
842+
final AuthInstallerUserManager userManager, AuthorizablesConfig authorizablesConfig, Session session, final String authorizablelId,
843843
final Set<String> isMemberOf, InstallationLogger installLog) throws RepositoryException,
844844
AuthorizableCreatorException {
845845

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

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import biz.netcentric.cq.tools.actool.history.InstallationLogger;
3030

3131
@RunWith(MockitoJUnitRunner.class)
32-
public class PrefetchingUserManagerTest {
32+
public class AuthInstallerUserManagerPrefetchingImplTest {
3333

3434
@Mock
3535
UserManager userManager;
@@ -58,7 +58,7 @@ public class PrefetchingUserManagerTest {
5858
@Mock
5959
User userNonCached;
6060

61-
PrefetchingUserManager prefetchingUserManager;
61+
AuthInstallerUserManagerPrefetchingImpl prefetchingUserManager;
6262

6363
@Before
6464
public void before() throws RepositoryException {
@@ -80,7 +80,7 @@ private void setupAuthorizable(Authorizable auth, String id, List<Group> memberO
8080

8181
@Test
8282
public void testPrefetchedAuthorizablesUserManager() throws RepositoryException {
83-
prefetchingUserManager = new PrefetchingUserManager(userManager, valueFactory, installationLogger);
83+
prefetchingUserManager = new AuthInstallerUserManagerPrefetchingImpl(userManager, valueFactory, installationLogger);
8484

8585
assertEquals(5, prefetchingUserManager.getCacheSize());
8686
assertEquals(group1, prefetchingUserManager.getAuthorizable("group1"));
@@ -92,59 +92,27 @@ public void testPrefetchedAuthorizablesUserManager() throws RepositoryException
9292

9393
@Test
9494
public void testDelegation() throws RepositoryException {
95-
prefetchingUserManager = new PrefetchingUserManager(userManager, valueFactory, installationLogger);
95+
prefetchingUserManager = new AuthInstallerUserManagerPrefetchingImpl(userManager, valueFactory, installationLogger);
9696

9797
String testuser = "test";
9898
PrincipalImpl testPrincipal = new PrincipalImpl(testuser);
99-
prefetchingUserManager.getAuthorizable(testPrincipal);
100-
verify(userManager, times(1)).getAuthorizable(testPrincipal);
10199

102100
String userPath = "/home/users/path";
103-
prefetchingUserManager.getAuthorizableByPath(userPath);
104-
verify(userManager, times(1)).getAuthorizableByPath(userPath);
105-
106-
String relPath = "profile/test";
107-
String testVal = "testval";
108-
prefetchingUserManager.findAuthorizables(relPath, testVal);
109-
verify(userManager, times(1)).findAuthorizables(relPath, testVal);
110-
prefetchingUserManager.findAuthorizables(relPath, testVal, UserManager.SEARCH_TYPE_AUTHORIZABLE);
111-
verify(userManager, times(1)).findAuthorizables(relPath, testVal, UserManager.SEARCH_TYPE_AUTHORIZABLE);
112-
113-
Query testQuery = new Query() {
114-
public <T> void build(QueryBuilder<T> builder) {
115-
// fetch all authorizables
116-
}
117-
};
118-
prefetchingUserManager.findAuthorizables(testQuery);
119-
verify(userManager, times(1)).findAuthorizables(testQuery);
120-
121101
String testpw = "pw";
122-
prefetchingUserManager.createUser(testuser, testpw);
123-
verify(userManager, times(1)).createUser(testuser, testpw);
124-
125102
prefetchingUserManager.createUser(testuser, testpw, testPrincipal, userPath);
126103
verify(userManager, times(1)).createUser(testuser, testpw, testPrincipal, userPath);
127104

128105
prefetchingUserManager.createSystemUser(testuser, userPath);
129106
verify(userManager, times(1)).createSystemUser(testuser, userPath);
130107

131-
prefetchingUserManager.createGroup(testuser);
132-
verify(userManager, times(1)).createGroup(testuser);
133108

134109
prefetchingUserManager.createGroup(testPrincipal);
135110
verify(userManager, times(1)).createGroup(testPrincipal);
136111

137112
prefetchingUserManager.createGroup(testPrincipal, userPath);
138113
verify(userManager, times(1)).createGroup(testPrincipal, userPath);
139114

140-
prefetchingUserManager.createGroup(testuser, testPrincipal, userPath);
141-
verify(userManager, times(1)).createGroup(testuser, testPrincipal, userPath);
142-
143-
prefetchingUserManager.autoSave(true);
144-
verify(userManager, times(1)).autoSave(true);
145115

146-
prefetchingUserManager.isAutoSave();
147-
verify(userManager, times(1)).isAutoSave();
148116
}
149117

150118
}

0 commit comments

Comments
 (0)