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.SyncopeConstants;
23  import org.apache.syncope.common.lib.types.AnyTypeKind;
24  import org.apache.syncope.common.lib.types.EntityViolationType;
25  import org.apache.syncope.core.persistence.api.entity.AnyType;
26  
27  public class AnyTypeValidator extends AbstractValidator<AnyTypeCheck, AnyType> {
28  
29      @Override
30      public boolean isValid(final AnyType anyType, final ConstraintValidatorContext context) {
31          context.disableDefaultConstraintViolation();
32  
33          boolean isValid = true;
34  
35          if (isHtml(anyType.getKey())) {
36              context.buildConstraintViolationWithTemplate(
37                      getTemplate(EntityViolationType.InvalidKey, anyType.getKey())).
38                      addPropertyNode("key").addConstraintViolation();
39  
40              isValid = false;
41          }
42  
43          boolean nameKindMatch;
44          switch (anyType.getKind()) {
45              case USER:
46                  nameKindMatch = AnyTypeKind.USER.name().equalsIgnoreCase(anyType.getKey());
47                  break;
48  
49              case GROUP:
50                  nameKindMatch = AnyTypeKind.GROUP.name().equalsIgnoreCase(anyType.getKey());
51                  break;
52  
53              case ANY_OBJECT:
54              default:
55                  nameKindMatch = !AnyTypeKind.USER.name().equalsIgnoreCase(anyType.getKey())
56                          && !AnyTypeKind.GROUP.name().equalsIgnoreCase(anyType.getKey())
57                          && !SyncopeConstants.REALM_ANYTYPE.equalsIgnoreCase(anyType.getKey());
58          }
59          if (!nameKindMatch) {
60              context.buildConstraintViolationWithTemplate(
61                      getTemplate(EntityViolationType.InvalidAnyType, "Name / kind mismatch")).
62                      addPropertyNode("name").addConstraintViolation();
63          }
64  
65          return isValid && nameKindMatch;
66      }
67  }