Search in sources :

Example 41 with Role

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

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

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

the class DataSourceUserDatabaseMBean method getUserRoles.

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

Example 44 with Role

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

the class DataSourceUserDatabaseMBean method createRole.

/**
 * Create a new Role and return the corresponding name.
 *
 * @param rolename Group name of the new group
 * @param description Description of the new group
 * @return the new role name
 */
public String createRole(String rolename, String description) {
    UserDatabase database = (UserDatabase) this.resource;
    Role role = database.createRole(rolename, description);
    return role.getRolename();
}
Also used : Role(org.apache.catalina.Role) UserDatabase(org.apache.catalina.UserDatabase)

Example 45 with Role

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

the class DataSourceUserDatabaseMBean method addUserRole.

/**
 * Add specified role to the user.
 * @param username The user name
 * @param rolename The role name
 */
public void addUserRole(String username, String rolename) {
    UserDatabase database = (UserDatabase) this.resource;
    User user = database.findUser(username);
    Role role = database.findRole(rolename);
    if (user != null && role != null) {
        user.addRole(role);
    }
}
Also used : Role(org.apache.catalina.Role) User(org.apache.catalina.User) UserDatabase(org.apache.catalina.UserDatabase)

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