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.data;
20  
21  import org.apache.syncope.common.lib.SyncopeClientCompositeException;
22  import org.apache.syncope.common.lib.SyncopeClientException;
23  import org.apache.syncope.common.lib.to.AuthModuleTO;
24  import org.apache.syncope.common.lib.to.Item;
25  import org.apache.syncope.common.lib.types.ClientExceptionType;
26  import org.apache.syncope.common.lib.types.MappingPurpose;
27  import org.apache.syncope.core.persistence.api.entity.EntityFactory;
28  import org.apache.syncope.core.persistence.api.entity.am.AuthModule;
29  import org.apache.syncope.core.provisioning.api.data.AuthModuleDataBinder;
30  import org.apache.syncope.core.provisioning.api.jexl.JexlUtils;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  public class AuthModuleDataBinderImpl implements AuthModuleDataBinder {
35  
36      protected static final Logger LOG = LoggerFactory.getLogger(AuthModuleDataBinder.class);
37  
38      protected final EntityFactory entityFactory;
39  
40      public AuthModuleDataBinderImpl(final EntityFactory entityFactory) {
41          this.entityFactory = entityFactory;
42      }
43  
44      protected void populateItems(final AuthModuleTO authModuleTO, final AuthModule authModule) {
45          SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
46          SyncopeClientException invalidMapping =
47                  SyncopeClientException.build(ClientExceptionType.InvalidMapping);
48          SyncopeClientException requiredValuesMissing =
49                  SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
50  
51          authModuleTO.getItems().forEach(itemTO -> {
52              if (itemTO == null) {
53                  LOG.error("Null {}", Item.class.getSimpleName());
54                  invalidMapping.getElements().add("Null " + Item.class.getSimpleName());
55              } else if (itemTO.getIntAttrName() == null) {
56                  requiredValuesMissing.getElements().add("intAttrName");
57                  scce.addException(requiredValuesMissing);
58              } else {
59                  // no mandatory condition implies mandatory condition false
60                  if (!JexlUtils.isExpressionValid(itemTO.getMandatoryCondition() == null
61                          ? "false" : itemTO.getMandatoryCondition())) {
62  
63                      SyncopeClientException invalidMandatoryCondition =
64                              SyncopeClientException.build(ClientExceptionType.InvalidValues);
65                      invalidMandatoryCondition.getElements().add(itemTO.getMandatoryCondition());
66                      scce.addException(invalidMandatoryCondition);
67                  }
68  
69                  Item item = new Item();
70                  item.setIntAttrName(itemTO.getIntAttrName());
71                  item.setExtAttrName(itemTO.getExtAttrName());
72                  item.setMandatoryCondition(itemTO.getMandatoryCondition());
73                  item.setConnObjectKey(itemTO.isConnObjectKey());
74                  item.setPassword(itemTO.isPassword());
75                  item.setPropagationJEXLTransformer(itemTO.getPropagationJEXLTransformer());
76                  item.setPullJEXLTransformer(itemTO.getPullJEXLTransformer());
77                  authModule.getItems().add(item);
78              }
79          });
80  
81          if (!invalidMapping.getElements().isEmpty()) {
82              scce.addException(invalidMapping);
83          }
84          if (scce.hasExceptions()) {
85              throw scce;
86          }
87      }
88  
89      @Override
90      public AuthModule create(final AuthModuleTO authModuleTO) {
91          AuthModule authModule = entityFactory.newEntity(AuthModule.class);
92          authModule.setKey(authModuleTO.getKey());
93          return update(authModule, authModuleTO);
94      }
95  
96      @Override
97      public AuthModule update(final AuthModule authModule, final AuthModuleTO authModuleTO) {
98          authModule.setDescription(authModuleTO.getDescription());
99          authModule.setState(authModuleTO.getState());
100         authModule.setOrder(authModuleTO.getOrder());
101         authModule.setConf(authModuleTO.getConf());
102 
103         authModule.getItems().clear();
104         populateItems(authModuleTO, authModule);
105 
106         return authModule;
107     }
108 
109     protected void populateItems(final AuthModule authModule, final AuthModuleTO authModuleTO) {
110         authModule.getItems().forEach(item -> {
111             Item itemTO = new Item();
112             itemTO.setIntAttrName(item.getIntAttrName());
113             itemTO.setExtAttrName(item.getExtAttrName());
114             itemTO.setMandatoryCondition(item.getMandatoryCondition());
115             itemTO.setConnObjectKey(item.isConnObjectKey());
116             itemTO.setPassword(item.isPassword());
117             itemTO.setPropagationJEXLTransformer(item.getPropagationJEXLTransformer());
118             itemTO.setPullJEXLTransformer(item.getPullJEXLTransformer());
119             itemTO.setPurpose(MappingPurpose.NONE);
120 
121             authModuleTO.getItems().add(itemTO);
122         });
123     }
124 
125     @Override
126     public AuthModuleTO getAuthModuleTO(final AuthModule authModule) {
127         AuthModuleTO authModuleTO = new AuthModuleTO();
128 
129         authModuleTO.setKey(authModule.getKey());
130         authModuleTO.setDescription(authModule.getDescription());
131         authModuleTO.setState(authModule.getState());
132         authModuleTO.setOrder(authModule.getOrder());
133         authModuleTO.setConf(authModule.getConf());
134 
135         populateItems(authModule, authModuleTO);
136 
137         return authModuleTO;
138     }
139 }