View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }