1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.persistence.jpa.validation.entity;
20
21 import javax.validation.ConstraintValidatorContext;
22 import org.apache.syncope.common.lib.types.EntityViolationType;
23 import org.apache.syncope.core.persistence.api.dao.AllowedSchemas;
24 import org.apache.syncope.core.persistence.api.entity.Any;
25 import org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory;
26 import org.apache.syncope.core.persistence.api.entity.GroupableRelatable;
27 import org.apache.syncope.core.persistence.api.entity.Membership;
28 import org.apache.syncope.core.persistence.api.entity.PlainAttr;
29 import org.apache.syncope.core.persistence.api.entity.PlainSchema;
30 import org.apache.syncope.core.persistence.api.entity.group.Group;
31 import org.apache.syncope.core.spring.ApplicationContextProvider;
32
33 @SuppressWarnings("rawtypes")
34 public class AnyValidator extends AbstractValidator<AnyCheck, Any> {
35
36 private static boolean raiseNotAllowedViolation(
37 final ConstraintValidatorContext context,
38 final String schema,
39 final Group group) {
40
41 if (group == null) {
42 context.buildConstraintViolationWithTemplate(
43 getTemplate(EntityViolationType.InvalidPlainAttr,
44 schema + " not allowed for this instance")).
45 addPropertyNode("plainAttrs").addConstraintViolation();
46 } else {
47 context.buildConstraintViolationWithTemplate(
48 getTemplate(EntityViolationType.InvalidPlainAttr,
49 schema + " not allowed for membership of group " + group.getName())).
50 addPropertyNode("plainAttrs").addConstraintViolation();
51 }
52 return false;
53 }
54
55 @Override
56 public boolean isValid(final Any any, final ConstraintValidatorContext context) {
57 context.disableDefaultConstraintViolation();
58
59 AllowedSchemas<PlainSchema> allowedPlainSchemas =
60 ApplicationContextProvider.getApplicationContext().getBean(AnyUtilsFactory.class).
61 getInstance(any.getType().getKind()).dao().findAllowedSchemas(any, PlainSchema.class);
62
63 for (PlainAttr<?> attr : ((Any<?>) any).getPlainAttrs()) {
64 if (attr != null && !allowedPlainSchemas.forSelfContains(attr.getSchema().getKey())) {
65 return raiseNotAllowedViolation(context, attr.getSchema().getKey(), null);
66 }
67 }
68 if (any instanceof GroupableRelatable) {
69 for (Membership<?> membership : ((GroupableRelatable<?, ?, ?, ?, ?>) any).getMemberships()) {
70 for (PlainAttr<?> attr : ((GroupableRelatable<?, ?, ?, ?, ?>) any).getPlainAttrs(membership)) {
71 if (attr != null && !allowedPlainSchemas.forMembershipsContains(
72 membership.getRightEnd(), attr.getSchema().getKey())) {
73
74 return raiseNotAllowedViolation(
75 context, attr.getSchema().getKey(), membership.getRightEnd());
76 }
77 }
78 }
79 }
80
81 return true;
82 }
83 }