Search in sources :

Example 1 with TestEntity

use of org.xwiki.security.authorization.testwikis.TestEntity in project xwiki-platform by xwiki.

the class TestGroupUserFactory method getNewInstance.

@Override
TestGroup getNewInstance(ElementParser parser, String name, TestEntity parent, Attributes attributes) throws SAXException {
    EntityReference reference = parser.getResolver().resolve(attributes.getValue("name"), parent.getType(), parent.getReference());
    TestEntity entity = parser.getWikis().searchEntity(reference);
    if (entity == null) {
        throw new SAXException(parser.getLocatedMessage("User/Group [%s] is undefined.", reference));
    }
    new DefaultTestGroup(parser.getSerializer().serialize(parent.getReference()), parent.getReference(), entity);
    return null;
}
Also used : TestEntity(org.xwiki.security.authorization.testwikis.TestEntity) DefaultTestGroup(org.xwiki.security.authorization.testwikis.internal.entities.DefaultTestGroup) EntityReference(org.xwiki.model.reference.EntityReference) SAXException(org.xml.sax.SAXException)

Example 2 with TestEntity

use of org.xwiki.security.authorization.testwikis.TestEntity in project xwiki-platform by xwiki.

the class AbstractAuthorizationTestCase method getUserReadableName.

/**
 * @param user user reference.
 * @return a pretty name for the user based on entities "alt" attributes or their names.
 */
protected String getUserReadableName(DocumentReference user) {
    if (user == null) {
        return "Public";
    }
    TestEntity userEntity = testDefinition.searchEntity(user);
    String result = (userEntity != null && userEntity instanceof TestDocument) ? ((TestDocument) userEntity).getDescription() : null;
    result = (result != null) ? result : user.getName();
    TestWiki wiki = testDefinition.getWiki(user.getWikiReference());
    String name = (wiki != null) ? wiki.getDescription() : null;
    name = (name != null) ? name : user.getWikiReference().getName();
    if (!name.startsWith("any")) {
        result += " from " + name;
    }
    return result;
}
Also used : TestEntity(org.xwiki.security.authorization.testwikis.TestEntity) TestDocument(org.xwiki.security.authorization.testwikis.TestDocument) TestWiki(org.xwiki.security.authorization.testwikis.TestWiki)

Example 3 with TestEntity

use of org.xwiki.security.authorization.testwikis.TestEntity in project xwiki-platform by xwiki.

the class AbstractTestEntity method searchEntity.

@Override
public TestEntity searchEntity(EntityReference reference) {
    EntityReference parentRef;
    EntityReference currentRef = reference;
    do {
        parentRef = currentRef.getParent();
        if (this.reference == parentRef || (this.reference != null && this.reference.equals(parentRef))) {
            if (currentRef == reference) {
                return entities.get(reference);
            } else {
                TestEntity child = entities.get(currentRef);
                if (child == null) {
                    return null;
                }
                return child.searchEntity(reference);
            }
        }
        currentRef = parentRef;
    } while (currentRef != null);
    return null;
}
Also used : TestEntity(org.xwiki.security.authorization.testwikis.TestEntity) EntityReference(org.xwiki.model.reference.EntityReference)

Example 4 with TestEntity

use of org.xwiki.security.authorization.testwikis.TestEntity in project xwiki-platform by xwiki.

the class AbstractAuthorizationTestCase method getEntityReadableName.

/**
 * @param entity user reference.
 * @return a pretty name for the user based on entities "alt" attributes or their names.
 */
protected String getEntityReadableName(EntityReference entity) {
    if (entity == null) {
        return "Main Wiki";
    }
    StringBuilder result = null;
    if (entity.getType() == EntityType.DOCUMENT) {
        TestEntity docEntity = testDefinition.searchEntity(entity);
        String name = (docEntity != null && docEntity instanceof TestDocument) ? ((TestDocument) docEntity).getDescription() : null;
        name = (name != null) ? name : entity.getName();
        result = new StringBuilder(name);
        entity = entity.getParent();
    }
    if (entity.getType() == EntityType.SPACE) {
        TestEntity spaceEntity = testDefinition.searchEntity(entity);
        String name = (spaceEntity != null && spaceEntity instanceof TestSpace) ? ((TestSpace) spaceEntity).getDescription() : null;
        name = (name != null) ? name : entity.getName();
        if (result == null || !name.startsWith("any")) {
            if (result != null) {
                result.append(" in ");
                result.append(name);
            } else {
                result = new StringBuilder(name);
            }
        }
        entity = entity.getParent();
    }
    if (entity.getType() == EntityType.WIKI) {
        TestEntity wikiEntity = testDefinition.getWiki(new WikiReference(entity));
        String name = (wikiEntity != null) ? ((TestWiki) wikiEntity).getDescription() : null;
        name = (name != null) ? name : entity.getName();
        if (result == null || !name.startsWith("any")) {
            if (result != null) {
                result.append(" from ");
                result.append(name);
            } else {
                result = new StringBuilder(name);
            }
        }
    }
    return result.toString();
}
Also used : TestEntity(org.xwiki.security.authorization.testwikis.TestEntity) TestDocument(org.xwiki.security.authorization.testwikis.TestDocument) TestSpace(org.xwiki.security.authorization.testwikis.TestSpace) WikiReference(org.xwiki.model.reference.WikiReference)

Example 5 with TestEntity

use of org.xwiki.security.authorization.testwikis.TestEntity in project xwiki-platform by xwiki.

the class DefaultAuthorizationManagerIntegrationTest method initialiseWikiMock.

@Override
public TestDefinition initialiseWikiMock(String filename) throws Exception {
    super.initialiseWikiMock(filename);
    when(xWikiBridge.getMainWikiReference()).thenReturn(testDefinition.getMainWiki().getWikiReference());
    when(xWikiBridge.isWikiReadOnly()).thenReturn(false);
    when(userBridge.getAllGroupsFor(any(UserSecurityReference.class), any(WikiReference.class))).thenAnswer(new Answer<Collection<GroupSecurityReference>>() {

        @Override
        public Collection<GroupSecurityReference> answer(InvocationOnMock invocationOnMock) throws Throwable {
            UserSecurityReference userReference = (UserSecurityReference) invocationOnMock.getArguments()[0];
            WikiReference wikiReference = (WikiReference) invocationOnMock.getArguments()[1];
            if (userReference.getOriginalReference() == null) {
                // Public users (not logged in) may not appears in any group
                return Collections.emptyList();
            }
            TestWiki wiki = testDefinition.getWiki(userReference.getOriginalReference().getWikiReference());
            if (wiki == null) {
                throw new AuthorizationException(String.format("Failed to get groups for user or group [%s] in wiki [%s]. Unknown wiki.", userReference, wikiReference), null);
            }
            TestUserDocument user = wiki.getUser(userReference.getName());
            if (user == null) {
                return Collections.emptyList();
            }
            Collection<GroupSecurityReference> groups = new ArrayList<GroupSecurityReference>();
            for (TestGroup group : user.getGroups()) {
                // Ensure we return only group of the requested wiki
                if (group.getGroupReference().getWikiReference().equals(wikiReference)) {
                    groups.add(securityReferenceFactory.newGroupReference(group.getGroupReference()));
                }
            }
            return groups;
        }
    });
    when(securityEntryReader.read(any(SecurityReference.class))).thenAnswer(new Answer<SecurityRuleEntry>() {

        @Override
        public SecurityRuleEntry answer(InvocationOnMock invocationOnMock) throws Throwable {
            final SecurityReference reference = (SecurityReference) invocationOnMock.getArguments()[0];
            TestEntity entity = testDefinition.searchEntity(reference);
            Collection<TestAccessRule> rules = (entity != null && entity instanceof SecureTestEntity) ? ((SecureTestEntity) entity).getAccessRules() : Collections.<TestAccessRule>emptyList();
            final Collection<SecurityRule> mockedRules = new ArrayList<SecurityRule>();
            for (final TestAccessRule rule : rules) {
                mockedRules.add(mockSecurityRule(reference, rule.getRight(), rule.getState(), rule.getUser(), rule.isUser()));
            }
            if (entity instanceof TestWiki) {
                TestWiki wiki = (TestWiki) entity;
                if (wiki.getOwner() != null) {
                    mockedRules.add(mockSecurityRule(reference, Right.ADMIN, RuleState.ALLOW, wiki.getOwner(), true));
                }
            }
            if (entity instanceof TestDocument) {
                TestDocument document = (TestDocument) entity;
                if (document.getCreator() != null) {
                    mockedRules.add(mockSecurityRule(reference, Right.CREATOR, RuleState.ALLOW, document.getCreator(), true));
                }
            }
            return new AbstractSecurityRuleEntry() {

                @Override
                public Collection<SecurityRule> getRules() {
                    return mockedRules;
                }

                @Override
                public SecurityReference getReference() {
                    return reference;
                }

                @Override
                public String toString() {
                    return String.format("Rule entry for %s containing %d rules", reference.toString(), mockedRules.size());
                }

                @Override
                public boolean equals(Object object) {
                    if (object == this) {
                        return true;
                    }
                    if (!(object instanceof SecurityRuleEntry)) {
                        return false;
                    }
                    SecurityRuleEntry other = (SecurityRuleEntry) object;
                    return compareReferenceNullSafe(other.getReference(), reference) && other.getRules().size() == mockedRules.size();
                }
            };
        }
    });
    return testDefinition;
}
Also used : TestWiki(org.xwiki.security.authorization.testwikis.TestWiki) TestUserDocument(org.xwiki.security.authorization.testwikis.TestUserDocument) AbstractSecurityRuleEntry(org.xwiki.security.authorization.internal.AbstractSecurityRuleEntry) GroupSecurityReference(org.xwiki.security.GroupSecurityReference) TestDocument(org.xwiki.security.authorization.testwikis.TestDocument) UserSecurityReference(org.xwiki.security.UserSecurityReference) SecureTestEntity(org.xwiki.security.authorization.testwikis.SecureTestEntity) TestEntity(org.xwiki.security.authorization.testwikis.TestEntity) SecureTestEntity(org.xwiki.security.authorization.testwikis.SecureTestEntity) AbstractSecurityRuleEntry(org.xwiki.security.authorization.internal.AbstractSecurityRuleEntry) TestGroup(org.xwiki.security.authorization.testwikis.TestGroup) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestAccessRule(org.xwiki.security.authorization.testwikis.TestAccessRule) Collection(java.util.Collection) SecurityReference(org.xwiki.security.SecurityReference) GroupSecurityReference(org.xwiki.security.GroupSecurityReference) UserSecurityReference(org.xwiki.security.UserSecurityReference) WikiReference(org.xwiki.model.reference.WikiReference)

Aggregations

TestEntity (org.xwiki.security.authorization.testwikis.TestEntity)5 TestDocument (org.xwiki.security.authorization.testwikis.TestDocument)3 EntityReference (org.xwiki.model.reference.EntityReference)2 WikiReference (org.xwiki.model.reference.WikiReference)2 TestWiki (org.xwiki.security.authorization.testwikis.TestWiki)2 Collection (java.util.Collection)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 SAXException (org.xml.sax.SAXException)1 GroupSecurityReference (org.xwiki.security.GroupSecurityReference)1 SecurityReference (org.xwiki.security.SecurityReference)1 UserSecurityReference (org.xwiki.security.UserSecurityReference)1 AbstractSecurityRuleEntry (org.xwiki.security.authorization.internal.AbstractSecurityRuleEntry)1 SecureTestEntity (org.xwiki.security.authorization.testwikis.SecureTestEntity)1 TestAccessRule (org.xwiki.security.authorization.testwikis.TestAccessRule)1 TestGroup (org.xwiki.security.authorization.testwikis.TestGroup)1 TestSpace (org.xwiki.security.authorization.testwikis.TestSpace)1 TestUserDocument (org.xwiki.security.authorization.testwikis.TestUserDocument)1 DefaultTestGroup (org.xwiki.security.authorization.testwikis.internal.entities.DefaultTestGroup)1