in java/org/apache/catalina/users/DataSourceUserDatabase.java [1123:1561]
protected void saveInternal(Connection dbConnection) {
StringBuilder temp = null;
StringBuilder tempRelation;
StringBuilder tempRelationDelete = null;
if (isRoleStoreDefined()) {
// Removed roles
if (!removedRoles.isEmpty()) {
temp = new StringBuilder("DELETE FROM ");
temp.append(roleTable);
temp.append(" WHERE ").append(roleNameCol);
temp.append(" = ?");
if (groupRoleTable != null) {
tempRelationDelete = new StringBuilder("DELETE FROM ");
tempRelationDelete.append(groupRoleTable);
tempRelationDelete.append(" WHERE ");
tempRelationDelete.append(roleNameCol);
tempRelationDelete.append(" = ?");
}
StringBuilder tempRelationDelete2 = new StringBuilder("DELETE FROM ");
tempRelationDelete2.append(userRoleTable);
tempRelationDelete2.append(" WHERE ");
tempRelationDelete2.append(roleNameCol);
tempRelationDelete2.append(" = ?");
for (Role role : removedRoles.values()) {
if (tempRelationDelete != null) {
try (PreparedStatement stmt = dbConnection.prepareStatement(tempRelationDelete.toString())) {
stmt.setString(1, role.getRolename());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
try (PreparedStatement stmt = dbConnection.prepareStatement(tempRelationDelete2.toString())) {
stmt.setString(1, role.getRolename());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
try (PreparedStatement stmt = dbConnection.prepareStatement(temp.toString())) {
stmt.setString(1, role.getRolename());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
removedRoles.clear();
}
// Created roles
if (!createdRoles.isEmpty()) {
temp = new StringBuilder("INSERT INTO ");
temp.append(roleTable);
temp.append('(').append(roleNameCol);
if (roleAndGroupDescriptionCol != null) {
temp.append(',').append(roleAndGroupDescriptionCol);
}
temp.append(") VALUES (?");
if (roleAndGroupDescriptionCol != null) {
temp.append(", ?");
}
temp.append(')');
for (Role role : createdRoles.values()) {
try (PreparedStatement stmt = dbConnection.prepareStatement(temp.toString())) {
stmt.setString(1, role.getRolename());
if (roleAndGroupDescriptionCol != null) {
stmt.setString(2, role.getDescription());
}
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
createdRoles.clear();
}
// Modified roles
if (!modifiedRoles.isEmpty() && roleAndGroupDescriptionCol != null) {
temp = new StringBuilder("UPDATE ");
temp.append(roleTable);
temp.append(" SET ").append(roleAndGroupDescriptionCol);
temp.append(" = ? WHERE ").append(roleNameCol);
temp.append(" = ?");
for (Role role : modifiedRoles.values()) {
try (PreparedStatement stmt = dbConnection.prepareStatement(temp.toString())) {
stmt.setString(1, role.getDescription());
stmt.setString(2, role.getRolename());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
modifiedRoles.clear();
}
} else if (userRoleTable != null && roleNameCol != null) {
// Only remove role from users
tempRelationDelete = new StringBuilder("DELETE FROM ");
tempRelationDelete.append(userRoleTable);
tempRelationDelete.append(" WHERE ");
tempRelationDelete.append(roleNameCol);
tempRelationDelete.append(" = ?");
for (Role role : removedRoles.values()) {
try (PreparedStatement stmt = dbConnection.prepareStatement(tempRelationDelete.toString())) {
stmt.setString(1, role.getRolename());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
removedRoles.clear();
}
if (isGroupStoreDefined()) {
tempRelation = new StringBuilder("INSERT INTO ");
tempRelation.append(groupRoleTable);
tempRelation.append('(').append(groupNameCol).append(", ");
tempRelation.append(roleNameCol);
tempRelation.append(") VALUES (?, ?)");
String groupRoleRelation = tempRelation.toString();
// Always drop and recreate all group <-> role relations
tempRelationDelete = new StringBuilder("DELETE FROM ");
tempRelationDelete.append(groupRoleTable);
tempRelationDelete.append(" WHERE ");
tempRelationDelete.append(groupNameCol);
tempRelationDelete.append(" = ?");
String groupRoleRelationDelete = tempRelationDelete.toString();
// Removed groups
if (!removedGroups.isEmpty()) {
temp = new StringBuilder("DELETE FROM ");
temp.append(groupTable);
temp.append(" WHERE ").append(groupNameCol);
temp.append(" = ?");
StringBuilder tempRelationDelete2 = new StringBuilder("DELETE FROM ");
tempRelationDelete2.append(userGroupTable);
tempRelationDelete2.append(" WHERE ");
tempRelationDelete2.append(groupNameCol);
tempRelationDelete2.append(" = ?");
for (Group group : removedGroups.values()) {
try (PreparedStatement stmt = dbConnection.prepareStatement(groupRoleRelationDelete)) {
stmt.setString(1, group.getGroupname());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
try (PreparedStatement stmt = dbConnection.prepareStatement(tempRelationDelete2.toString())) {
stmt.setString(1, group.getGroupname());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
try (PreparedStatement stmt = dbConnection.prepareStatement(temp.toString())) {
stmt.setString(1, group.getGroupname());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
removedGroups.clear();
}
// Created groups
if (!createdGroups.isEmpty()) {
temp = new StringBuilder("INSERT INTO ");
temp.append(groupTable);
temp.append('(').append(groupNameCol);
if (roleAndGroupDescriptionCol != null) {
temp.append(',').append(roleAndGroupDescriptionCol);
}
temp.append(") VALUES (?");
if (roleAndGroupDescriptionCol != null) {
temp.append(", ?");
}
temp.append(')');
for (Group group : createdGroups.values()) {
try (PreparedStatement stmt = dbConnection.prepareStatement(temp.toString())) {
stmt.setString(1, group.getGroupname());
if (roleAndGroupDescriptionCol != null) {
stmt.setString(2, group.getDescription());
}
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
Iterator<Role> roles = group.getRoles();
while (roles.hasNext()) {
Role role = roles.next();
try (PreparedStatement stmt = dbConnection.prepareStatement(groupRoleRelation)) {
stmt.setString(1, group.getGroupname());
stmt.setString(2, role.getRolename());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
}
createdGroups.clear();
}
// Modified groups
if (!modifiedGroups.isEmpty()) {
if (roleAndGroupDescriptionCol != null) {
temp = new StringBuilder("UPDATE ");
temp.append(groupTable);
temp.append(" SET ").append(roleAndGroupDescriptionCol);
temp.append(" = ? WHERE ").append(groupNameCol);
temp.append(" = ?");
}
for (Group group : modifiedGroups.values()) {
if (temp != null) {
try (PreparedStatement stmt = dbConnection.prepareStatement(temp.toString())) {
stmt.setString(1, group.getDescription());
stmt.setString(2, group.getGroupname());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
try (PreparedStatement stmt = dbConnection.prepareStatement(groupRoleRelationDelete)) {
stmt.setString(1, group.getGroupname());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
Iterator<Role> roles = group.getRoles();
while (roles.hasNext()) {
Role role = roles.next();
try (PreparedStatement stmt = dbConnection.prepareStatement(groupRoleRelation)) {
stmt.setString(1, group.getGroupname());
stmt.setString(2, role.getRolename());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
}
modifiedGroups.clear();
}
}
String userRoleRelation = null;
String userRoleRelationDelete = null;
if (userRoleTable != null && roleNameCol != null) {
tempRelation = new StringBuilder("INSERT INTO ");
tempRelation.append(userRoleTable);
tempRelation.append('(').append(userNameCol).append(", ");
tempRelation.append(roleNameCol);
tempRelation.append(") VALUES (?, ?)");
userRoleRelation = tempRelation.toString();
// Always drop and recreate all user <-> role relations
tempRelationDelete = new StringBuilder("DELETE FROM ");
tempRelationDelete.append(userRoleTable);
tempRelationDelete.append(" WHERE ");
tempRelationDelete.append(userNameCol);
tempRelationDelete.append(" = ?");
userRoleRelationDelete = tempRelationDelete.toString();
}
String userGroupRelation = null;
String userGroupRelationDelete = null;
if (isGroupStoreDefined()) {
tempRelation = new StringBuilder("INSERT INTO ");
tempRelation.append(userGroupTable);
tempRelation.append('(').append(userNameCol).append(", ");
tempRelation.append(groupNameCol);
tempRelation.append(") VALUES (?, ?)");
userGroupRelation = tempRelation.toString();
// Always drop and recreate all user <-> group relations
tempRelationDelete = new StringBuilder("DELETE FROM ");
tempRelationDelete.append(userGroupTable);
tempRelationDelete.append(" WHERE ");
tempRelationDelete.append(userNameCol);
tempRelationDelete.append(" = ?");
userGroupRelationDelete = tempRelationDelete.toString();
}
// Removed users
if (!removedUsers.isEmpty()) {
temp = new StringBuilder("DELETE FROM ");
temp.append(userTable);
temp.append(" WHERE ").append(userNameCol);
temp.append(" = ?");
for (User user : removedUsers.values()) {
if (userRoleRelationDelete != null) {
try (PreparedStatement stmt = dbConnection.prepareStatement(userRoleRelationDelete)) {
stmt.setString(1, user.getUsername());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
if (userGroupRelationDelete != null) {
try (PreparedStatement stmt = dbConnection.prepareStatement(userGroupRelationDelete)) {
stmt.setString(1, user.getUsername());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
try (PreparedStatement stmt = dbConnection.prepareStatement(temp.toString())) {
stmt.setString(1, user.getUsername());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
removedUsers.clear();
}
// Created users
if (!createdUsers.isEmpty()) {
temp = new StringBuilder("INSERT INTO ");
temp.append(userTable);
temp.append('(').append(userNameCol);
temp.append(", ").append(userCredCol);
if (userFullNameCol != null) {
temp.append(',').append(userFullNameCol);
}
temp.append(") VALUES (?, ?");
if (userFullNameCol != null) {
temp.append(", ?");
}
temp.append(')');
for (User user : createdUsers.values()) {
try (PreparedStatement stmt = dbConnection.prepareStatement(temp.toString())) {
stmt.setString(1, user.getUsername());
stmt.setString(2, user.getPassword());
if (userFullNameCol != null) {
stmt.setString(3, user.getFullName());
}
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
if (userRoleRelation != null) {
Iterator<Role> roles = user.getRoles();
while (roles.hasNext()) {
Role role = roles.next();
try (PreparedStatement stmt = dbConnection.prepareStatement(userRoleRelation)) {
stmt.setString(1, user.getUsername());
stmt.setString(2, role.getRolename());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
}
if (userGroupRelation != null) {
Iterator<Group> groups = user.getGroups();
while (groups.hasNext()) {
Group group = groups.next();
try (PreparedStatement stmt = dbConnection.prepareStatement(userGroupRelation)) {
stmt.setString(1, user.getUsername());
stmt.setString(2, group.getGroupname());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
}
}
createdUsers.clear();
}
// Modified users
if (!modifiedUsers.isEmpty()) {
temp = new StringBuilder("UPDATE ");
temp.append(userTable);
temp.append(" SET ").append(userCredCol);
temp.append(" = ?");
if (userFullNameCol != null) {
temp.append(", ").append(userFullNameCol).append(" = ?");
}
temp.append(" WHERE ").append(userNameCol);
temp.append(" = ?");
for (User user : modifiedUsers.values()) {
try (PreparedStatement stmt = dbConnection.prepareStatement(temp.toString())) {
stmt.setString(1, user.getPassword());
if (userFullNameCol != null) {
stmt.setString(2, user.getFullName());
stmt.setString(3, user.getUsername());
} else {
stmt.setString(2, user.getUsername());
}
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
if (userRoleRelationDelete != null) {
try (PreparedStatement stmt = dbConnection.prepareStatement(userRoleRelationDelete)) {
stmt.setString(1, user.getUsername());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
if (userGroupRelationDelete != null) {
try (PreparedStatement stmt = dbConnection.prepareStatement(userGroupRelationDelete)) {
stmt.setString(1, user.getUsername());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
if (userRoleRelation != null) {
Iterator<Role> roles = user.getRoles();
while (roles.hasNext()) {
Role role = roles.next();
try (PreparedStatement stmt = dbConnection.prepareStatement(userRoleRelation)) {
stmt.setString(1, user.getUsername());
stmt.setString(2, role.getRolename());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
}
if (userGroupRelation != null) {
Iterator<Group> groups = user.getGroups();
while (groups.hasNext()) {
Group group = groups.next();
try (PreparedStatement stmt = dbConnection.prepareStatement(userGroupRelation)) {
stmt.setString(1, user.getUsername());
stmt.setString(2, group.getGroupname());
stmt.executeUpdate();
} catch (SQLException e) {
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
}
}
}
}
modifiedGroups.clear();
}
}