ImpersonationImpl implements Impersonation, UserConstants { @Override public boolean allows(@NotNull Subject subject) { if (subject == null) { return false; } Set<String> principalNames = new HashSet<>(); for (Principal principal : subject.getPrincipals()) { principalNames.add(principal.getName()); } boolean allows = getImpersonatorNames().removeAll(principalNames); if (!allows) { for (Principal principal : subject.getPrincipals()) { if (isAdmin(principal)) { allows = true; break; } } } return allows; } ImpersonationImpl(@NotNull UserImpl user); @NotNull @Override PrincipalIterator getImpersonators(); @Override boolean grantImpersonation(@NotNull Principal principal); @Override boolean revokeImpersonation(@NotNull Principal principal); @Override boolean allows(@NotNull Subject subject);  }
@Test public void testAllows() throws Exception { assertTrue(impersonation.allows(createSubject(impersonator.getPrincipal()))); }
@Test public void testAllowsIncludingGroup() throws Exception { Group gr = getUserManager(root).createGroup("gId"); assertTrue(impersonation.allows(createSubject(impersonator.getPrincipal(), gr.getPrincipal()))); }
@Test public void testAllowsExistingGroup() throws Exception { Group gr = getUserManager(root).createGroup("gId"); try { root.commit(); assertFalse(impersonation.allows(createSubject(new PrincipalImpl(gr.getPrincipal().getName())))); } finally { gr.remove(); root.commit(); } }
@Test public void testAllowsIncludingNonExistingGroup() throws Exception { assertTrue(impersonation.allows(createSubject(impersonator.getPrincipal(), groupPrincipal))); }
@Test public void testAllowsImpersonatorRemoved() throws Exception { Subject s = createSubject(impersonator.getPrincipal()); impersonator.remove(); assertTrue(impersonation.allows(s)); }
@Test public void testAllowsNonExistingUser() { assertFalse(impersonation.allows(createSubject(new PrincipalImpl("nonExisting")))); }
@Test public void testAllowsUserLookupFails() throws Exception { Principal nonExisting = new PrincipalImpl("nonExisting"); UserManagerImpl uMgr = spy((UserManagerImpl) getUserManager(root)); when(uMgr.getAuthorizable(nonExisting)).thenThrow(new RepositoryException()); ImpersonationImpl imp = new ImpersonationImpl(new UserImpl(user.getID(), user.getTree(), uMgr)); assertFalse(imp.allows(createSubject(nonExisting))); }