Search in sources :

Example 1 with WikiSecurityEvent

use of org.apache.wiki.event.WikiSecurityEvent in project jspwiki by apache.

the class GroupManagerTest method testGroupAddEvents.

@Test
public void testGroupAddEvents() throws Exception {
    // Flush any pre-existing groups (left over from previous Assert.failures, perhaps)
    try {
        m_groupMgr.removeGroup("Events");
    } catch (NoSuchPrincipalException e) {
    // It's not a problem if we get here...
    }
    m_trap.clearEvents();
    Group group = m_groupMgr.parseGroup("Events", "", true);
    m_groupMgr.setGroup(m_session, group);
    WikiSecurityEvent event;
    group = m_groupMgr.getGroup("Events");
    group.add(new WikiPrincipal("Alice"));
    group.add(new WikiPrincipal("Bob"));
    group.add(new WikiPrincipal("Charlie"));
    // We should see a GROUP_ADD event
    WikiSecurityEvent[] events = m_trap.events();
    Assert.assertEquals(1, events.length);
    event = events[0];
    Assert.assertEquals(m_groupMgr, event.getSrc());
    Assert.assertEquals(WikiSecurityEvent.GROUP_ADD, event.getType());
    Assert.assertEquals(group, event.getTarget());
    // Clean up
    m_groupMgr.removeGroup("Events");
}
Also used : Group(org.apache.wiki.auth.authorize.Group) WikiSecurityEvent(org.apache.wiki.event.WikiSecurityEvent) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 2 with WikiSecurityEvent

use of org.apache.wiki.event.WikiSecurityEvent in project jspwiki by apache.

the class PageManager method actionPerformed.

/**
 * Listens for {@link org.apache.wiki.event.WikiSecurityEvent#PROFILE_NAME_CHANGED}
 * events. If a user profile's name changes, each page ACL is inspected. If an entry contains
 * a name that has changed, it is replaced with the new one. No events are emitted
 * as a consequence of this method, because the page contents are still the same; it is
 * only the representations of the names within the ACL that are changing.
 *
 * @param event The event
 */
public void actionPerformed(WikiEvent event) {
    if (!(event instanceof WikiSecurityEvent)) {
        return;
    }
    WikiSecurityEvent se = (WikiSecurityEvent) event;
    if (se.getType() == WikiSecurityEvent.PROFILE_NAME_CHANGED) {
        UserProfile[] profiles = (UserProfile[]) se.getTarget();
        Principal[] oldPrincipals = new Principal[] { new WikiPrincipal(profiles[0].getLoginName()), new WikiPrincipal(profiles[0].getFullname()), new WikiPrincipal(profiles[0].getWikiName()) };
        Principal newPrincipal = new WikiPrincipal(profiles[1].getFullname());
        // Examine each page ACL
        try {
            int pagesChanged = 0;
            Collection pages = getAllPages();
            for (Iterator it = pages.iterator(); it.hasNext(); ) {
                WikiPage page = (WikiPage) it.next();
                boolean aclChanged = changeAcl(page, oldPrincipals, newPrincipal);
                if (aclChanged) {
                    // If the Acl needed changing, change it now
                    try {
                        m_engine.getAclManager().setPermissions(page, page.getAcl());
                    } catch (WikiSecurityException e) {
                        log.error("Could not change page ACL for page " + page.getName() + ": " + e.getMessage(), e);
                    }
                    pagesChanged++;
                }
            }
            log.info("Profile name change for '" + newPrincipal.toString() + "' caused " + pagesChanged + " page ACLs to change also.");
        } catch (ProviderException e) {
            // Oooo! This is really bad...
            log.error("Could not change user name in Page ACLs because of Provider error:" + e.getMessage(), e);
        }
    }
}
Also used : UserProfile(org.apache.wiki.auth.user.UserProfile) ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Iterator(java.util.Iterator) Collection(java.util.Collection) WikiSecurityEvent(org.apache.wiki.event.WikiSecurityEvent) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal)

Example 3 with WikiSecurityEvent

use of org.apache.wiki.event.WikiSecurityEvent in project jspwiki by apache.

the class GroupManager method actionPerformed.

/**
 * Listens for {@link org.apache.wiki.event.WikiSecurityEvent#PROFILE_NAME_CHANGED}
 * events. If a user profile's name changes, each group is inspected. If an entry contains
 * a name that has changed, it is replaced with the new one. No group events are emitted
 * as a consequence of this method, because the group memberships are still the same; it is
 * only the representations of the names within that are changing.
 * @param event the incoming event
 */
public void actionPerformed(WikiEvent event) {
    if (!(event instanceof WikiSecurityEvent)) {
        return;
    }
    WikiSecurityEvent se = (WikiSecurityEvent) event;
    if (se.getType() == WikiSecurityEvent.PROFILE_NAME_CHANGED) {
        WikiSession session = se.getSrc();
        UserProfile[] profiles = (UserProfile[]) se.getTarget();
        Principal[] oldPrincipals = new Principal[] { new WikiPrincipal(profiles[0].getLoginName()), new WikiPrincipal(profiles[0].getFullname()), new WikiPrincipal(profiles[0].getWikiName()) };
        Principal newPrincipal = new WikiPrincipal(profiles[1].getFullname());
        // Examine each group
        int groupsChanged = 0;
        try {
            for (Group group : m_groupDatabase.groups()) {
                boolean groupChanged = false;
                for (Principal oldPrincipal : oldPrincipals) {
                    if (group.isMember(oldPrincipal)) {
                        group.remove(oldPrincipal);
                        group.add(newPrincipal);
                        groupChanged = true;
                    }
                }
                if (groupChanged) {
                    setGroup(session, group);
                    groupsChanged++;
                }
            }
        } catch (WikiException e) {
            // Oooo! This is really bad...
            log.error("Could not change user name in Group lists because of GroupDatabase error:" + e.getMessage());
        }
        log.info("Profile name change for '" + newPrincipal.toString() + "' caused " + groupsChanged + " groups to change also.");
    }
}
Also used : WikiSession(org.apache.wiki.WikiSession) WikiException(org.apache.wiki.api.exceptions.WikiException) UserProfile(org.apache.wiki.auth.user.UserProfile) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) WikiSecurityEvent(org.apache.wiki.event.WikiSecurityEvent) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal)

Aggregations

WikiSecurityEvent (org.apache.wiki.event.WikiSecurityEvent)3 Principal (java.security.Principal)2 WikiPrincipal (org.apache.wiki.auth.WikiPrincipal)2 UserProfile (org.apache.wiki.auth.user.UserProfile)2 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 WikiSession (org.apache.wiki.WikiSession)1 WikiSessionTest (org.apache.wiki.WikiSessionTest)1 ProviderException (org.apache.wiki.api.exceptions.ProviderException)1 WikiException (org.apache.wiki.api.exceptions.WikiException)1 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)1 WikiSecurityException (org.apache.wiki.auth.WikiSecurityException)1 Group (org.apache.wiki.auth.authorize.Group)1 Test (org.junit.Test)1