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 java.util.HashSet;
22  import java.util.Set;
23  import javax.validation.ConstraintValidatorContext;
24  import org.apache.syncope.common.lib.types.AnyTypeKind;
25  import org.apache.syncope.common.lib.types.EntityViolationType;
26  import org.apache.syncope.core.persistence.api.entity.AnyType;
27  import org.apache.syncope.core.persistence.api.entity.Entity;
28  import org.apache.syncope.core.persistence.api.entity.anyobject.ADynGroupMembership;
29  import org.apache.syncope.core.persistence.api.entity.group.Group;
30  
31  public class GroupValidator extends AbstractValidator<GroupCheck, Group> {
32  
33      @Override
34      public boolean isValid(final Group group, final ConstraintValidatorContext context) {
35          context.disableDefaultConstraintViolation();
36  
37          boolean isValid = true;
38  
39          if (group.getUserOwner() != null && group.getGroupOwner() != null) {
40              isValid = false;
41  
42              context.buildConstraintViolationWithTemplate(
43                      getTemplate(EntityViolationType.InvalidGroupOwner,
44                              "A group must either be owned by an user or a group, not both")).
45                      addPropertyNode("owner").addConstraintViolation();
46          }
47  
48          if (isValid && (group.getName() == null || !Entity.ID_PATTERN.matcher(group.getName()).matches())) {
49              isValid = false;
50  
51              context.buildConstraintViolationWithTemplate(
52                      getTemplate(EntityViolationType.InvalidName, group.getName())).
53                      addPropertyNode("name").addConstraintViolation();
54          }
55  
56          if (isValid) {
57              Set<AnyType> anyTypes = new HashSet<>();
58              for (ADynGroupMembership memb : group.getADynMemberships()) {
59                  anyTypes.add(memb.getAnyType());
60  
61                  if (memb.getAnyType().getKind() != AnyTypeKind.ANY_OBJECT) {
62                      isValid = false;
63  
64                      context.buildConstraintViolationWithTemplate(
65                              getTemplate(EntityViolationType.InvalidADynMemberships,
66                                      "No user or group dynamic membership condition are allowed here")).
67                              addPropertyNode("aDynMemberships").addConstraintViolation();
68                  }
69              }
70  
71              if (isValid && anyTypes.size() < group.getADynMemberships().size()) {
72                  context.buildConstraintViolationWithTemplate(
73                          getTemplate(EntityViolationType.InvalidADynMemberships,
74                                  "Each dynamic membership condition requires a different "
75                                  + AnyType.class.getSimpleName())).
76                          addPropertyNode("aDynMemberships").addConstraintViolation();
77                  return false;
78              }
79  
80          }
81  
82          return isValid;
83      }
84  }