Search in sources :

Example 1 with SecurityShadowEntry

use of org.xwiki.security.authorization.cache.SecurityShadowEntry in project xwiki-platform by xwiki.

the class DefaultSecurityCacheTest method mockSecurityShadowEntry.

private SecurityShadowEntry mockSecurityShadowEntry(final UserSecurityReference user, final SecurityReference wiki) {
    SecurityShadowEntry entry = mock(SecurityShadowEntry.class, "Shadow for " + user.toString() + " on " + wiki.toString());
    when(entry.getReference()).thenReturn(user);
    when(entry.getWikiReference()).thenReturn(wiki);
    return entry;
}
Also used : SecurityShadowEntry(org.xwiki.security.authorization.cache.SecurityShadowEntry)

Example 2 with SecurityShadowEntry

use of org.xwiki.security.authorization.cache.SecurityShadowEntry in project xwiki-platform by xwiki.

the class DefaultSecurityCacheTest method InsertUsers.

private Map<String, SecurityEntry> InsertUsers() throws ConflictingInsertionException, ParentEntryEvictedException {
    Map<String, SecurityEntry> entries = InsertUsersWithouShadow();
    // Check inserting shadow users
    for (UserSecurityReference ref : userRefs) {
        if (ref.isGlobal()) {
            for (SecurityReference wiki : Arrays.asList(wikiRef, anotherWikiRef)) {
                SecurityShadowEntry entry = mockSecurityShadowEntry(ref, wiki);
                entries.put(AddUserEntry(entry), entry);
            }
        }
    }
    // Insert some groups
    for (GroupSecurityReference ref : groupRefs.keySet()) {
        if (ref.isGlobal()) {
            for (SecurityReference wiki : Arrays.asList(wikiRef, anotherWikiRef)) {
                SecurityShadowEntry entry = mockSecurityShadowEntry(ref, wiki);
                entries.put(AddUserEntry(entry), entry);
            }
        }
    }
    // Insert shadow users in shadow groups
    for (UserSecurityReference ref : groupUserRefs) {
        if (ref.isGlobal()) {
            for (SecurityReference wiki : Arrays.asList(wikiRef, anotherWikiRef)) {
                SecurityShadowEntry entry = mockSecurityShadowEntry(ref, wiki);
                entries.put(AddUserEntry(entry), entry);
            }
        }
    }
    return entries;
}
Also used : SecurityEntry(org.xwiki.security.authorization.SecurityEntry) GroupSecurityReference(org.xwiki.security.GroupSecurityReference) SecurityReference(org.xwiki.security.SecurityReference) UserSecurityReference(org.xwiki.security.UserSecurityReference) SecurityShadowEntry(org.xwiki.security.authorization.cache.SecurityShadowEntry) UserSecurityReference(org.xwiki.security.UserSecurityReference) GroupSecurityReference(org.xwiki.security.GroupSecurityReference)

Example 3 with SecurityShadowEntry

use of org.xwiki.security.authorization.cache.SecurityShadowEntry in project xwiki-platform by xwiki.

the class DefaultSecurityCacheTest method testAddSecurityShadowEntry.

@Test
public void testAddSecurityShadowEntry() throws Exception {
    InsertUsersWithouShadow();
    final List<SecurityShadowEntry> allEntries = new ArrayList<SecurityShadowEntry>();
    // Check inserting shadow users
    for (UserSecurityReference ref : userRefs) {
        if (ref.isGlobal()) {
            for (SecurityReference wiki : Arrays.asList(wikiRef, anotherWikiRef)) {
                SecurityShadowEntry entry = mockSecurityShadowEntry(ref, wiki);
                assertThat(((DefaultSecurityCache) securityCache).get(AddUserEntry(entry)), sameInstance((SecurityEntry) entry));
                allEntries.add(entry);
            }
        }
    }
    // Check inserting some shadow groups
    for (GroupSecurityReference ref : groupRefs.keySet()) {
        if (ref.isGlobal()) {
            for (SecurityReference wiki : Arrays.asList(wikiRef, anotherWikiRef)) {
                SecurityShadowEntry entry = mockSecurityShadowEntry(ref, wiki);
                assertThat(((DefaultSecurityCache) securityCache).get(AddUserEntry(entry)), sameInstance((SecurityEntry) entry));
                allEntries.add(entry);
            }
        }
    }
    // Check inserting shadow users in shadow groups
    for (UserSecurityReference ref : groupUserRefs) {
        if (ref.isGlobal()) {
            for (SecurityReference wiki : Arrays.asList(wikiRef, anotherWikiRef)) {
                SecurityShadowEntry entry = mockSecurityShadowEntry(ref, wiki);
                assertThat(((DefaultSecurityCache) securityCache).get(AddUserEntry(entry)), sameInstance((SecurityEntry) entry));
                allEntries.add(entry);
            }
        }
    }
    // Check a duplicate insertion
    try {
        AddUserEntry(allEntries.get(0));
    } catch (ConflictingInsertionException e) {
        fail("Inserting the same shadow entry twice should NOT throw a ConflictingInsertionException.");
    }
    // Check inserting a shadow for a missing user in an existing wiki
    try {
        AddUserEntry(mockSecurityShadowEntry(aMissingUserRef, wikiRef));
        fail("Inserting a shadow entry without inserting its global user first should throw" + " a ParentEntryEvictedException.");
    } catch (ParentEntryEvictedException ignore) {
    // Expected.
    }
    // Check inserting a shadow for a existing user in a missing wiki
    try {
        AddUserEntry(mockSecurityShadowEntry(xuserRef, aMissingWikiRef));
        fail("Inserting a shadow entry without inserting its wiki first should throw" + " a ParentEntryEvictedException.");
    } catch (ParentEntryEvictedException ignore) {
    // Expected.
    }
}
Also used : SecurityEntry(org.xwiki.security.authorization.SecurityEntry) ArrayList(java.util.ArrayList) GroupSecurityReference(org.xwiki.security.GroupSecurityReference) SecurityReference(org.xwiki.security.SecurityReference) UserSecurityReference(org.xwiki.security.UserSecurityReference) SecurityShadowEntry(org.xwiki.security.authorization.cache.SecurityShadowEntry) UserSecurityReference(org.xwiki.security.UserSecurityReference) GroupSecurityReference(org.xwiki.security.GroupSecurityReference) ConflictingInsertionException(org.xwiki.security.authorization.cache.ConflictingInsertionException) ParentEntryEvictedException(org.xwiki.security.authorization.cache.ParentEntryEvictedException) Test(org.junit.Test)

Example 4 with SecurityShadowEntry

use of org.xwiki.security.authorization.cache.SecurityShadowEntry in project xwiki-platform by xwiki.

the class DefaultSecurityCacheTest method checkEntries.

private void checkEntries(Map<String, SecurityEntry> entries, KeepEntries keeper) {
    for (Iterator<Map.Entry<String, SecurityEntry>> it = entries.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<String, SecurityEntry> entry = it.next();
        if (entry.getValue() instanceof SecurityRuleEntry) {
            SecurityRuleEntry sentry = (SecurityRuleEntry) entry.getValue();
            if (keeper.keepRule(sentry)) {
                assertThat(((DefaultSecurityCache) securityCache).get(entry.getKey()), sameInstance(entry.getValue()));
            } else {
                it.remove();
                assertThat(((DefaultSecurityCache) securityCache).get(entry.getKey()), nullValue());
            }
        } else if (entry.getValue() instanceof SecurityAccessEntry) {
            SecurityAccessEntry sentry = (SecurityAccessEntry) entry.getValue();
            if (keeper.keepAccess(sentry)) {
                assertThat(((DefaultSecurityCache) securityCache).get(entry.getKey()), sameInstance(entry.getValue()));
            } else {
                it.remove();
                assertThat(((DefaultSecurityCache) securityCache).get(entry.getKey()), nullValue());
            }
        } else {
            SecurityShadowEntry sentry = (SecurityShadowEntry) entry.getValue();
            if (keeper.keepShadow(sentry)) {
                assertThat(((DefaultSecurityCache) securityCache).get(entry.getKey()), sameInstance(entry.getValue()));
            } else {
                it.remove();
                assertThat(((DefaultSecurityCache) securityCache).get(entry.getKey()), nullValue());
            }
        }
    }
}
Also used : SecurityAccessEntry(org.xwiki.security.authorization.SecurityAccessEntry) SecurityRuleEntry(org.xwiki.security.authorization.SecurityRuleEntry) SecurityShadowEntry(org.xwiki.security.authorization.cache.SecurityShadowEntry) SecurityEntry(org.xwiki.security.authorization.SecurityEntry) SecurityAccessEntry(org.xwiki.security.authorization.SecurityAccessEntry) SecurityRuleEntry(org.xwiki.security.authorization.SecurityRuleEntry) SecurityEntry(org.xwiki.security.authorization.SecurityEntry) SecurityShadowEntry(org.xwiki.security.authorization.cache.SecurityShadowEntry) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

SecurityShadowEntry (org.xwiki.security.authorization.cache.SecurityShadowEntry)4 SecurityEntry (org.xwiki.security.authorization.SecurityEntry)3 GroupSecurityReference (org.xwiki.security.GroupSecurityReference)2 SecurityReference (org.xwiki.security.SecurityReference)2 UserSecurityReference (org.xwiki.security.UserSecurityReference)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 SecurityAccessEntry (org.xwiki.security.authorization.SecurityAccessEntry)1 SecurityRuleEntry (org.xwiki.security.authorization.SecurityRuleEntry)1 ConflictingInsertionException (org.xwiki.security.authorization.cache.ConflictingInsertionException)1 ParentEntryEvictedException (org.xwiki.security.authorization.cache.ParentEntryEvictedException)1