}
}
@Override
public ValidationResponse validatePrivilege(CPrivilege privilege, SecurityValidationContext ctx, boolean update) {
ValidationResponse response = super.validatePrivilege(privilege, ctx, update);
if (!TYPE.equals(privilege.getType())) {
return response;
}
// validate method
// method is of form ('*' | 'read' | 'create' | 'update' | 'delete' [, method]* )
// so, 'read' method is correct, but so is also 'create,update,delete'
// '*' means ALL POSSIBLE value for this "field"
String method = null;
String repositoryId = null;
String repositoryTargetId = null;
String repositoryGroupId = null;
for (CProperty property : (List<CProperty>) privilege.getProperties()) {
if (property.getKey().equals(ApplicationPrivilegeMethodPropertyDescriptor.ID)) {
method = property.getValue();
}
else if (property.getKey().equals(TargetPrivilegeRepositoryPropertyDescriptor.ID)) {
repositoryId = property.getValue();
}
else if (property.getKey().equals(TargetPrivilegeRepositoryTargetPropertyDescriptor.ID)) {
repositoryTargetId = property.getValue();
}
else if (property.getKey().equals(TargetPrivilegeGroupPropertyDescriptor.ID)) {
repositoryGroupId = property.getValue();
}
}
if (StringUtils.isEmpty(repositoryTargetId)) {
ValidationMessage message = new ValidationMessage("repositoryTargetId", "Privilege ID '"
+ privilege.getId() + "' requires a repositoryTargetId.", "Repository Target is required.");
response.addValidationError(message);
}
if (!StringUtils.isEmpty(repositoryId) && !StringUtils.isEmpty(repositoryGroupId)) {
ValidationMessage message = new ValidationMessage(
"repositoryId",
"Privilege ID '"
+ privilege.getId()
+ "' cannot be assigned to both a group and repository."
+ " Either assign a group, a repository or neither (which assigns to ALL repositories).",
"Cannot select both a Repository and Repository Group.");
response.addValidationError(message);
}
if (StringUtils.isEmpty(method)) {
response.addValidationError("Method cannot be empty on a privilege!");
}
else {
String[] methods = null;
if (method.contains(",")) {
// it is a list of methods
methods = method.split(",");
}
else {
// it is a single method
methods = new String[]{method};
}
boolean valid = true;
for (String singlemethod : methods) {
if (!"create".equals(singlemethod) && !"delete".equals(singlemethod)
&& !"read".equals(singlemethod) && !"update".equals(singlemethod)
&& !"*".equals(singlemethod)) {
valid = false;
break;
}
}
if (!valid) {
ValidationMessage message = new ValidationMessage(
"method",
"Privilege ID '" + privilege.getId()
+ "' Method is wrong! (Allowed methods are: create, delete, read and update)",
"Invalid method selected.");
response.addValidationError(message);
}
}
return response;