Search in sources :

Example 41 with Group

use of org.apache.catalina.Group in project tomcat by apache.

the class MemoryUserCreationFactory method createObject.

@Override
public Object createObject(Attributes attributes) {
    String groupname = attributes.getValue("groupname");
    if (groupname == null) {
        groupname = attributes.getValue("name");
    }
    String description = attributes.getValue("description");
    String roles = attributes.getValue("roles");
    Group group = database.findGroup(groupname);
    if (group == null) {
        group = database.createGroup(groupname, description);
    } else {
        if (group.getDescription() == null) {
            group.setDescription(description);
        }
    }
    if (roles != null) {
        while (roles.length() > 0) {
            String rolename = null;
            int comma = roles.indexOf(',');
            if (comma >= 0) {
                rolename = roles.substring(0, comma).trim();
                roles = roles.substring(comma + 1);
            } else {
                rolename = roles.trim();
                roles = "";
            }
            if (rolename.length() > 0) {
                Role role = database.findRole(rolename);
                if (role == null) {
                    role = database.createRole(rolename, null);
                }
                group.addRole(role);
            }
        }
    }
    return group;
}
Also used : Role(org.apache.catalina.Role) Group(org.apache.catalina.Group)

Example 42 with Group

use of org.apache.catalina.Group in project tomcat by apache.

the class DataSourceUserDatabaseMBean method removeGroup.

/**
 * Remove an existing group.
 *
 * @param groupname Group name to remove
 */
public void removeGroup(String groupname) {
    UserDatabase database = (UserDatabase) this.resource;
    Group group = database.findGroup(groupname);
    if (group == null) {
        return;
    }
    database.removeGroup(group);
}
Also used : Group(org.apache.catalina.Group) UserDatabase(org.apache.catalina.UserDatabase)

Example 43 with Group

use of org.apache.catalina.Group in project tomcat by apache.

the class DataSourceUserDatabaseMBean method getUserGroups.

/**
 * Get groups for a user.
 * @param username The user name
 * @return Array of group names
 */
public String[] getUserGroups(String username) {
    UserDatabase database = (UserDatabase) this.resource;
    User user = database.findUser(username);
    if (user != null) {
        List<String> results = new ArrayList<>();
        Iterator<Group> groups = user.getGroups();
        while (groups.hasNext()) {
            Group group = groups.next();
            results.add(group.getGroupname());
        }
        return results.toArray(new String[0]);
    } else {
        return null;
    }
}
Also used : Group(org.apache.catalina.Group) User(org.apache.catalina.User) UserDatabase(org.apache.catalina.UserDatabase) ArrayList(java.util.ArrayList)

Example 44 with Group

use of org.apache.catalina.Group in project tomcat by apache.

the class DataSourceUserDatabaseMBean method getGroups.

// ------------------------------------------------------------- Attributes
/**
 * @return the names of all groups defined in this database.
 */
public String[] getGroups() {
    UserDatabase database = (UserDatabase) this.resource;
    List<String> results = new ArrayList<>();
    Iterator<Group> groups = database.getGroups();
    while (groups.hasNext()) {
        Group group = groups.next();
        results.add(group.getGroupname());
    }
    return results.toArray(new String[0]);
}
Also used : Group(org.apache.catalina.Group) UserDatabase(org.apache.catalina.UserDatabase) ArrayList(java.util.ArrayList)

Example 45 with Group

use of org.apache.catalina.Group in project tomcat by apache.

the class DataSourceUserDatabaseMBean method removeUserGroup.

/**
 * Remove group from user.
 * @param username The user name
 * @param groupname The group name
 */
public void removeUserGroup(String username, String groupname) {
    UserDatabase database = (UserDatabase) this.resource;
    User user = database.findUser(username);
    Group group = database.findGroup(groupname);
    if (user != null && group != null) {
        user.removeGroup(group);
    }
}
Also used : Group(org.apache.catalina.Group) User(org.apache.catalina.User) UserDatabase(org.apache.catalina.UserDatabase)

Aggregations

Group (org.apache.catalina.Group)51 User (org.apache.catalina.User)21 UserDatabase (org.apache.catalina.UserDatabase)21 Role (org.apache.catalina.Role)20 ArrayList (java.util.ArrayList)15 MalformedObjectNameException (javax.management.MalformedObjectNameException)12 ObjectName (javax.management.ObjectName)9 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 MBeanException (javax.management.MBeanException)4 RuntimeOperationsException (javax.management.RuntimeOperationsException)4 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 NamingException (javax.naming.NamingException)2 OperationNotSupportedException (javax.naming.OperationNotSupportedException)2 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 LoggingBaseTest (org.apache.catalina.startup.LoggingBaseTest)1