use of org.xwiki.security.UserSecurityReference in project xwiki-platform by xwiki.
the class DefaultSecurityCacheLoaderTest method loadWithConflictingInsertionException.
@Test
public void loadWithConflictingInsertionException() throws Exception {
DocumentReference userReference = new DocumentReference("wiki", "Users", "mflorea");
UserSecurityReference user = securityReferenceFactory.newUserReference(userReference);
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Document");
SecurityReference entity = securityReferenceFactory.newEntityReference(documentReference);
SecurityRuleEntry documentEntry = mock(SecurityRuleEntry.class, "document");
when(documentEntry.getReference()).thenReturn(entity);
when(documentEntry.isEmpty()).thenReturn(true);
SecurityRuleEntry spaceEntry = mock(SecurityRuleEntry.class, "space");
when(spaceEntry.getReference()).thenReturn(entity.getParentSecurityReference());
when(spaceEntry.isEmpty()).thenReturn(true);
SecurityRuleEntry wikiEntry = mock(SecurityRuleEntry.class, "wiki");
when(wikiEntry.getReference()).thenReturn(entity.getParentSecurityReference().getParentSecurityReference());
when(wikiEntry.isEmpty()).thenReturn(true);
SecurityCache securityCache = mocker.getInstance(SecurityCache.class);
when(securityCache.get(entity)).thenReturn(documentEntry);
when(securityCache.get(entity.getParentSecurityReference())).thenReturn(spaceEntry);
when(securityCache.get(entity.getParentSecurityReference().getParentSecurityReference())).thenReturn(wikiEntry);
when(securityCache.getGroupsFor(user, null)).thenReturn(null);
UserBridge userBridge = mocker.getInstance(UserBridge.class);
DocumentReference groupReference = new DocumentReference("wiki", "Groups", "AllGroup");
Set<GroupSecurityReference> groups = Collections.singleton(securityReferenceFactory.newGroupReference(groupReference));
when(userBridge.getAllGroupsFor(user, userReference.getWikiReference())).thenReturn(groups);
SecurityAccessEntry securityAccessEntry = mock(SecurityAccessEntry.class);
AuthorizationSettler authorizationSettler = mocker.getInstance(AuthorizationSettler.class);
Deque<SecurityRuleEntry> securityRuleEntries = new LinkedList<SecurityRuleEntry>(Arrays.asList(documentEntry, spaceEntry, wikiEntry));
when(authorizationSettler.settle(user, groups, securityRuleEntries)).thenReturn(securityAccessEntry);
doThrow(ConflictingInsertionException.class).when(securityCache).add(securityAccessEntry);
doThrow(ConflictingInsertionException.class).when(securityCache).add(securityAccessEntry, null);
try {
securityCacheLoader.load(user, entity);
fail();
} catch (AuthorizationException e) {
assertEquals("Failed to load the cache in 5 attempts. Giving up. when checking " + "access to [wiki:Space.Document] for user [wiki:Users.mflorea]", e.getMessage());
assertTrue(ExceptionUtils.getRootCauseMessage(e).contains("ConflictingInsertionException"));
}
// Assert that we've also emitted a log
assertEquals(1, this.logRule.size());
assertEquals("Failed to load the cache in 5 attempts. Giving up.", this.logRule.getMessage(0));
}
use of org.xwiki.security.UserSecurityReference in project xwiki-platform by xwiki.
the class AbstractAuthorizationSettler method settle.
@Override
public SecurityAccessEntry settle(UserSecurityReference user, Collection<GroupSecurityReference> groups, Deque<SecurityRuleEntry> ruleEntries) {
XWikiSecurityAccess access = new XWikiSecurityAccess();
SecurityReference reference = null;
Policies policies = new Policies();
for (SecurityRuleEntry entry : ruleEntries) {
if (!entry.isEmpty()) {
// Chose the highest possible level to store the resulting access
if (reference == null) {
reference = entry.getReference();
}
// Compute access of this level and merge it with previous access result
merge(settle(user, groups, entry, policies), access, entry.getReference(), policies);
}
if (reference == null && entry.getReference().getType() == EntityType.WIKI) {
reference = entry.getReference();
}
}
// Apply defaults and return the resulting access entry
return new InternalSecurityAccessEntry(user, reference, applyDefaults(user, reference, access));
}
use of org.xwiki.security.UserSecurityReference in project xwiki-platform by xwiki.
the class DefaultAuthorizationSettlerTest method getMockedSecurityRule.
private SecurityRule getMockedSecurityRule(String name, Iterable<UserSecurityReference> users, Iterable<GroupSecurityReference> groups, Iterable<Right> rights, final RuleState state) {
final SecurityRule rule = mock(SecurityRule.class, name);
final List<Matcher<? super UserSecurityReference>> userMatchers = new ArrayList<Matcher<? super UserSecurityReference>>();
final List<Matcher<? super GroupSecurityReference>> groupMatchers = new ArrayList<Matcher<? super GroupSecurityReference>>();
final List<Matcher<? super Right>> rightMatchers = new ArrayList<Matcher<? super Right>>();
for (UserSecurityReference user : users) {
userMatchers.add(is(user));
}
for (GroupSecurityReference group : groups) {
groupMatchers.add(is(group));
}
for (Right right : rights) {
rightMatchers.add(is(right));
}
when(rule.match(argThat(anyOf(userMatchers)))).thenReturn(true);
when(rule.match(argThat(anyOf(groupMatchers)))).thenReturn(true);
when(rule.match(argThat(anyOf(rightMatchers)))).thenReturn(true);
when(rule.match(argThat(not(anyOf(userMatchers))))).thenReturn(false);
when(rule.match(argThat(not(anyOf(groupMatchers))))).thenReturn(false);
when(rule.match(argThat(not(anyOf(rightMatchers))))).thenReturn(false);
when(rule.getState()).thenReturn(state);
return rule;
}
Aggregations