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;
}
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;
}
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;
}
}
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();
}
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);
}
}
Aggregations