Search in sources :

Example 36 with Group

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

the class GroupMBean method addRole.

// ------------------------------------------------------------- Operations
/**
 * Add a new {@link Role} to those this group belongs to.
 *
 * @param rolename Role name of the new role
 */
public void addRole(String rolename) {
    Group group = (Group) this.resource;
    if (group == null) {
        return;
    }
    Role role = group.getUserDatabase().findRole(rolename);
    if (role == null) {
        throw new IllegalArgumentException("Invalid role name '" + rolename + "'");
    }
    group.addRole(role);
}
Also used : Role(org.apache.catalina.Role) Group(org.apache.catalina.Group)

Example 37 with Group

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

the class MemoryUserCreationFactory method removeRole.

/**
 * Remove the specified {@link Role} from this user database.
 *
 * @param role The role to be removed
 */
@Override
public void removeRole(Role role) {
    readLock.lock();
    try {
        Iterator<Group> groups = getGroups();
        while (groups.hasNext()) {
            Group group = groups.next();
            group.removeRole(role);
        }
        Iterator<User> users = getUsers();
        while (users.hasNext()) {
            User user = users.next();
            user.removeRole(role);
        }
        roles.remove(role.getRolename());
    } finally {
        readLock.unlock();
    }
}
Also used : Group(org.apache.catalina.Group) User(org.apache.catalina.User)

Example 38 with Group

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

the class DataSourceUserDatabase method findGroupInternal.

public Group findGroupInternal(Connection dbConnection, String groupName) {
    Group group = null;
    try (PreparedStatement stmt = dbConnection.prepareStatement(preparedGroup)) {
        stmt.setString(1, groupName);
        try (ResultSet rs = stmt.executeQuery()) {
            if (rs.next()) {
                if (rs.getString(1) != null) {
                    String description = (roleAndGroupDescriptionCol != null) ? rs.getString(2) : null;
                    ArrayList<Role> groupRoles = new ArrayList<>();
                    if (groupName != null) {
                        groupName = groupName.trim();
                        try (PreparedStatement stmt2 = dbConnection.prepareStatement(preparedGroupRoles)) {
                            stmt2.setString(1, groupName);
                            try (ResultSet rs2 = stmt2.executeQuery()) {
                                while (rs2.next()) {
                                    String roleName = rs2.getString(1);
                                    if (roleName != null) {
                                        Role groupRole = findRoleInternal(dbConnection, roleName);
                                        if (groupRole != null) {
                                            groupRoles.add(groupRole);
                                        }
                                    }
                                }
                            }
                        } catch (SQLException e) {
                            log.error(sm.getString("dataSourceUserDatabase.exception"), e);
                        }
                    }
                    group = new GenericGroup<>(this, groupName, description, groupRoles);
                }
            }
        }
    } catch (SQLException e) {
        log.error(sm.getString("dataSourceUserDatabase.exception"), e);
    }
    return group;
}
Also used : Role(org.apache.catalina.Role) Group(org.apache.catalina.Group) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 39 with Group

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

the class DataSourceUserDatabase method findUserInternal.

public User findUserInternal(Connection dbConnection, String userName) {
    String dbCredentials = null;
    String fullName = null;
    try (PreparedStatement stmt = dbConnection.prepareStatement(preparedUser)) {
        stmt.setString(1, userName);
        try (ResultSet rs = stmt.executeQuery()) {
            if (rs.next()) {
                dbCredentials = rs.getString(1);
                if (userFullNameCol != null) {
                    fullName = rs.getString(2);
                }
            }
            dbCredentials = (dbCredentials != null) ? dbCredentials.trim() : null;
        }
    } catch (SQLException e) {
        log.error(sm.getString("dataSourceUserDatabase.exception"), e);
    }
    // Lookup groups
    ArrayList<Group> groups = new ArrayList<>();
    if (isGroupStoreDefined()) {
        try (PreparedStatement stmt = dbConnection.prepareStatement(preparedUserGroups)) {
            stmt.setString(1, userName);
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    String groupName = rs.getString(1);
                    if (groupName != null) {
                        Group group = findGroupInternal(dbConnection, groupName);
                        if (group != null) {
                            groups.add(group);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            log.error(sm.getString("dataSourceUserDatabase.exception"), e);
        }
    }
    ArrayList<Role> roles = new ArrayList<>();
    if (userRoleTable != null && roleNameCol != null) {
        try (PreparedStatement stmt = dbConnection.prepareStatement(preparedUserRoles)) {
            stmt.setString(1, userName);
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    String roleName = rs.getString(1);
                    if (roleName != null) {
                        Role role = findRoleInternal(dbConnection, roleName);
                        if (role != null) {
                            roles.add(role);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            log.error(sm.getString("dataSourceUserDatabase.exception"), e);
        }
    }
    User user = new GenericUser<>(this, userName, dbCredentials, fullName, groups, roles);
    return user;
}
Also used : Role(org.apache.catalina.Role) Group(org.apache.catalina.Group) User(org.apache.catalina.User) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 40 with Group

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

the class DataSourceUserDatabase method createGroup.

@Override
public Group createGroup(String groupname, String description) {
    dbReadLock.lock();
    try {
        groupsWriteLock.lock();
        try {
            Group group = new GenericGroup<>(this, groupname, description, null);
            createdGroups.put(groupname, group);
            modifiedGroups.remove(groupname);
            return group;
        } finally {
            groupsWriteLock.unlock();
        }
    } finally {
        dbReadLock.unlock();
    }
}
Also used : Group(org.apache.catalina.Group)

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