Search in sources :

Example 1 with GroupDatabase

use of org.apache.wiki.auth.authorize.GroupDatabase in project jspwiki by apache.

the class SecurityVerifier method verifyGroupDatabase.

/**
 * Verifies that the group datbase was initialized properly, and that
 * user add and delete operations work as they should.
 */
protected void verifyGroupDatabase() {
    GroupManager mgr = m_engine.getGroupManager();
    GroupDatabase db = null;
    try {
        db = m_engine.getGroupManager().getGroupDatabase();
    } catch (WikiSecurityException e) {
        m_session.addMessage(ERROR_GROUPS, "Could not retrieve GroupManager: " + e.getMessage());
    }
    // Check for obvious error conditions
    if (mgr == null || db == null) {
        if (mgr == null) {
            m_session.addMessage(ERROR_GROUPS, "GroupManager is null; JSPWiki could not " + "initialize it. Check the error logs.");
        }
        if (db == null) {
            m_session.addMessage(ERROR_GROUPS, "GroupDatabase is null; JSPWiki could not " + "initialize it. Check the error logs.");
        }
        return;
    }
    // Everything initialized OK...
    // Tell user what class of database this is.
    m_session.addMessage(INFO_GROUPS, "GroupDatabase is of type '" + db.getClass().getName() + "'. It appears to be initialized properly.");
    // Now, see how many groups we have.
    int oldGroupCount = 0;
    try {
        Group[] groups = db.groups();
        oldGroupCount = groups.length;
        m_session.addMessage(INFO_GROUPS, "The group database contains " + oldGroupCount + " groups.");
    } catch (WikiSecurityException e) {
        m_session.addMessage(ERROR_GROUPS, "Could not obtain a list of current groups: " + e.getMessage());
        return;
    }
    // Try adding a bogus group with random name
    String name = "TestGroup" + System.currentTimeMillis();
    Group group = null;
    try {
        // Create dummy test group
        group = mgr.parseGroup(name, "", true);
        Principal user = new WikiPrincipal("TestUser");
        group.add(user);
        db.save(group, new WikiPrincipal("SecurityVerifier"));
        // Make sure the group saved successfully
        if (db.groups().length == oldGroupCount) {
            m_session.addMessage(ERROR_GROUPS, "Could not add a test group to the database.");
            return;
        }
        m_session.addMessage(INFO_GROUPS, "The group database allows new groups to be created, as it should.");
    } catch (WikiSecurityException e) {
        m_session.addMessage(ERROR_GROUPS, "Could not add a group to the database: " + e.getMessage());
        return;
    }
    // Now delete the group; should be back to old count
    try {
        db.delete(group);
        if (db.groups().length != oldGroupCount) {
            m_session.addMessage(ERROR_GROUPS, "Could not delete a test group from the database.");
            return;
        }
        m_session.addMessage(INFO_GROUPS, "The group database allows groups to be deleted, as it should.");
    } catch (WikiSecurityException e) {
        m_session.addMessage(ERROR_GROUPS, "Could not delete a test group from the database: " + e.getMessage());
        return;
    }
    m_session.addMessage(INFO_GROUPS, "The group database configuration looks fine.");
}
Also used : Group(org.apache.wiki.auth.authorize.Group) GroupDatabase(org.apache.wiki.auth.authorize.GroupDatabase) GroupManager(org.apache.wiki.auth.authorize.GroupManager) Principal(java.security.Principal)

Aggregations

Principal (java.security.Principal)1 Group (org.apache.wiki.auth.authorize.Group)1 GroupDatabase (org.apache.wiki.auth.authorize.GroupDatabase)1 GroupManager (org.apache.wiki.auth.authorize.GroupManager)1