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.provisioning.java;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.Set;
24  import org.apache.commons.jexl3.JexlContext;
25  import org.apache.commons.jexl3.MapContext;
26  import org.apache.syncope.core.persistence.api.entity.Any;
27  import org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory;
28  import org.apache.syncope.core.persistence.api.entity.DerSchema;
29  import org.apache.syncope.core.persistence.api.entity.GroupableRelatable;
30  import org.apache.syncope.core.persistence.api.entity.Membership;
31  import org.apache.syncope.core.provisioning.api.DerAttrHandler;
32  import org.apache.syncope.core.provisioning.api.jexl.JexlUtils;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  import org.springframework.transaction.annotation.Transactional;
36  
37  @Transactional(readOnly = true)
38  public class DefaultDerAttrHandler implements DerAttrHandler {
39  
40      protected static final Logger LOG = LoggerFactory.getLogger(DerAttrHandler.class);
41  
42      protected static Map<DerSchema, String> getValues(final Any<?> any, final Set<DerSchema> schemas) {
43          Map<DerSchema, String> result = new HashMap<>(schemas.size());
44  
45          schemas.forEach(schema -> {
46              JexlContext jexlContext = new MapContext();
47              JexlUtils.addPlainAttrsToContext(any.getPlainAttrs(), jexlContext);
48              JexlUtils.addFieldsToContext(any, jexlContext);
49  
50              result.put(schema, JexlUtils.evaluate(schema.getExpression(), jexlContext).toString());
51          });
52  
53          return result;
54      }
55  
56      protected final AnyUtilsFactory anyUtilsFactory;
57  
58      public DefaultDerAttrHandler(final AnyUtilsFactory anyUtilsFactory) {
59          this.anyUtilsFactory = anyUtilsFactory;
60      }
61  
62      @Override
63      public String getValue(final Any<?> any, final DerSchema schema) {
64          if (!anyUtilsFactory.getInstance(any).dao().
65                  findAllowedSchemas(any, DerSchema.class).forSelfContains(schema)) {
66  
67              LOG.debug("{} not allowed for {}", schema, any);
68              return null;
69          }
70  
71          return getValues(any, Set.of(schema)).get(schema);
72      }
73  
74      @Override
75      public String getValue(final Any<?> any, final Membership<?> membership, final DerSchema schema) {
76          if (!anyUtilsFactory.getInstance(any).dao().
77                  findAllowedSchemas(any, DerSchema.class).getForMembership(membership.getRightEnd()).contains(schema)) {
78  
79              LOG.debug("{} not allowed for {}", schema, any);
80              return null;
81          }
82  
83          return getValues(any, Set.of(schema)).get(schema);
84      }
85  
86      @Override
87      public Map<DerSchema, String> getValues(final Any<?> any) {
88          return getValues(
89                  any,
90                  anyUtilsFactory.getInstance(any).dao().findAllowedSchemas(any, DerSchema.class).getForSelf());
91      }
92  
93      protected static Map<DerSchema, String> getValues(
94              final GroupableRelatable<?, ?, ?, ?, ?> any, final Membership<?> membership, final Set<DerSchema> schemas) {
95  
96          Map<DerSchema, String> result = new HashMap<>(schemas.size());
97  
98          schemas.forEach(schema -> {
99              JexlContext jexlContext = new MapContext();
100             JexlUtils.addPlainAttrsToContext(any.getPlainAttrs(membership), jexlContext);
101             JexlUtils.addFieldsToContext(any, jexlContext);
102 
103             result.put(schema, JexlUtils.evaluate(schema.getExpression(), jexlContext).toString());
104         });
105 
106         return result;
107     }
108 
109     @Override
110     public Map<DerSchema, String> getValues(
111             final GroupableRelatable<?, ?, ?, ?, ?> any, final Membership<?> membership) {
112 
113         return getValues(
114                 any,
115                 membership,
116                 anyUtilsFactory.getInstance(any).dao().findAllowedSchemas(any, DerSchema.class).
117                         getForMembership(membership.getRightEnd()));
118     }
119 
120 }