Search in sources :

Example 36 with Role

use of org.apache.catalina.Role 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 Role

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

the class DataSourceUserDatabase method findRole.

@Override
public Role findRole(String rolename) {
    dbReadLock.lock();
    try {
        rolesReadLock.lock();
        try {
            // Check local changes first
            Role role = createdRoles.get(rolename);
            if (role != null) {
                return role;
            }
            role = modifiedRoles.get(rolename);
            if (role != null) {
                return role;
            }
            role = removedRoles.get(rolename);
            if (role != null) {
                return null;
            }
            if (userRoleTable != null && roleNameCol != null) {
                Connection dbConnection = openConnection();
                if (dbConnection == null) {
                    return null;
                }
                try {
                    return findRoleInternal(dbConnection, rolename);
                } finally {
                    closeConnection(dbConnection);
                }
            } else {
                return null;
            }
        } finally {
            rolesReadLock.unlock();
        }
    } finally {
        dbReadLock.unlock();
    }
}
Also used : Role(org.apache.catalina.Role) Connection(java.sql.Connection)

Example 38 with Role

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

the class DataSourceUserDatabase method getRoles.

@Override
public Iterator<Role> getRoles() {
    dbReadLock.lock();
    try {
        rolesReadLock.lock();
        try {
            HashMap<String, Role> roles = new HashMap<>();
            roles.putAll(createdRoles);
            roles.putAll(modifiedRoles);
            Connection dbConnection = openConnection();
            if (dbConnection != null && preparedAllRoles != null) {
                try (PreparedStatement stmt = dbConnection.prepareStatement(preparedAllRoles)) {
                    try (ResultSet rs = stmt.executeQuery()) {
                        while (rs.next()) {
                            String roleName = rs.getString(1);
                            if (roleName != null) {
                                if (!roles.containsKey(roleName) && !removedRoles.containsKey(roleName)) {
                                    Role role = findRoleInternal(dbConnection, roleName);
                                    if (role != null) {
                                        roles.put(roleName, role);
                                    }
                                }
                            }
                        }
                    }
                } catch (SQLException e) {
                    log.error(sm.getString("dataSourceUserDatabase.exception"), e);
                } finally {
                    closeConnection(dbConnection);
                }
            }
            return roles.values().iterator();
        } finally {
            rolesReadLock.unlock();
        }
    } finally {
        dbReadLock.unlock();
    }
}
Also used : Role(org.apache.catalina.Role) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 39 with Role

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

the class DataSourceUserDatabase method createRole.

@Override
public Role createRole(String rolename, String description) {
    dbReadLock.lock();
    try {
        rolesWriteLock.lock();
        try {
            Role role = new GenericRole<>(this, rolename, description);
            createdRoles.put(rolename, role);
            modifiedRoles.remove(rolename);
            return role;
        } finally {
            rolesWriteLock.unlock();
        }
    } finally {
        dbReadLock.unlock();
    }
}
Also used : Role(org.apache.catalina.Role)

Example 40 with Role

use of org.apache.catalina.Role 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)

Aggregations

Role (org.apache.catalina.Role)50 UserDatabase (org.apache.catalina.UserDatabase)21 Group (org.apache.catalina.Group)20 User (org.apache.catalina.User)18 ArrayList (java.util.ArrayList)13 MalformedObjectNameException (javax.management.MalformedObjectNameException)10 ObjectName (javax.management.ObjectName)7 PreparedStatement (java.sql.PreparedStatement)5 SQLException (java.sql.SQLException)5 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 MBeanException (javax.management.MBeanException)4 RuntimeOperationsException (javax.management.RuntimeOperationsException)4 Statement (java.sql.Statement)2 NamingException (javax.naming.NamingException)2 OperationNotSupportedException (javax.naming.OperationNotSupportedException)2 LoggingBaseTest (org.apache.catalina.startup.LoggingBaseTest)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1