return response;
}
public ValidationResponse validateRole(SecurityValidationContext ctx, CRole role, boolean update) {
ValidationResponse response = new ValidationResponse();
if (ctx != null) {
response.setContext(ctx);
}
SecurityValidationContext context = (SecurityValidationContext) response.getContext();
List<String> existingIds = context.getExistingRoleIds();
if (existingIds == null) {
context.addExistingRoleIds();
existingIds = context.getExistingRoleIds();
}
if (!update && existingIds.contains(role.getId())) {
ValidationMessage message = new ValidationMessage("id", "Role ID must be unique.");
response.addValidationError(message);
}
if (update && !existingIds.contains(role.getId())) {
ValidationMessage message = new ValidationMessage("id", "Role ID cannot be changed.");
response.addValidationError(message);
}
if (!update && (StringUtils.isEmpty(role.getId()) || "0".equals(role.getId()))) {
String newId = idGenerator.generateId();
response.addValidationWarning("Fixed wrong role ID from '" + role.getId() + "' to '" + newId + "'");
role.setId(newId);
response.setModified(true);
}
Map<String, String> existingRoleNameMap = context.getExistingRoleNameMap();
if (StringUtils.isEmpty(role.getName())) {
ValidationMessage message =
new ValidationMessage("name", "Role ID '" + role.getId() + "' requires a name.", "Name is required.");
response.addValidationError(message);
}
else if (isRoleNameAlreadyInUse(existingRoleNameMap, role)) {
ValidationMessage message =
new ValidationMessage("name", "Role ID '" + role.getId() + "' can't use the name '" + role.getName()
+ "'.", "Name is already in use.");
response.addValidationError(message);
}
else {
existingRoleNameMap.put(role.getId(), role.getName());
}
if (context.getExistingPrivilegeIds() != null) {
List<String> privIds = role.getPrivileges();
for (String privId : privIds) {
if (!context.getExistingPrivilegeIds().contains(privId)) {
ValidationMessage message =
new ValidationMessage("privileges", "Role ID '" + role.getId() + "' Invalid privilege id '"
+ privId + "' found.", "Role cannot contain invalid privilege ID '" + privId + "'.");
response.addValidationWarning(message);
}
}
}
List<String> roleIds = role.getRoles();
List<String> containedRoles = context.getRoleContainmentMap().get(role.getId());
if (containedRoles == null) {
containedRoles = new ArrayList<String>();
context.getRoleContainmentMap().put(role.getId(), containedRoles);
}
for (String roleId : roleIds) {
if (roleId.equals(role.getId())) {
ValidationMessage message =
new ValidationMessage("roles", "Role ID '" + role.getId() + "' cannot contain itself.",
"Role cannot contain itself.");
response.addValidationError(message);
}
else if (context.getRoleContainmentMap() != null) {
containedRoles.add(roleId);
}
}
// It is expected that a full context is built upon update
if (update) {
response.append(isRecursive(role.getId(), role.getId(), context));
}
existingIds.add(role.getId());
return response;