dev.kanngard.net make sure you visit my new blog at: johankanngard.net

Short tip: Add roles to an ACL with Java

by Johan Känngård / [Java] / 2002-05-09 / #44


This is a short snippet of code that is used to add roles to a database.

/**
 * Adds the roles in the specified Vector to the
 * specified Database.
 *
 * @author Johan Känngård, http://dev.kanngard.net
 * @param db the Database to add the roles to.
 * @param roles the roles in a Vector to add to
 * the Database.
 * @return a Vector of the roles that were added
 * and null if no roles were added.
*/
public Vector addRoles(Database db, Vector roles)
        throws NotesException {
    Vector addedRoles = new Vector();
    ACL acl = db.getACL();
    String role = null;

    for (int i = 0; i < roles.size(); i++) {
        role = "[" + (String)roles.elementAt(i) + "]";

        if (!(acl.getRoles().contains(role))) {
            acl.addRole(role);
            addedRoles.addElement(role);
        }
    }

    if (addedRoles.size() != 0) {
        acl.save();
    }
    return addedRoles;
}

It can be used by creating an agent with this code:

Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Database currentDb = agentContext.getCurrentDatabase();
Vector roles = new Vector();
roles.addElement("Administrator");
roles.addElement("User");
roles.addElement("Infomaster");
roles.addElement("Hacker");
System.out.println("Added " + addRoles(currentDb, roles)
    + " to " + currentDb.getFilePath());