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.logic;
20  
21  import java.lang.reflect.Method;
22  import java.time.OffsetDateTime;
23  import java.util.List;
24  import java.util.Optional;
25  import java.util.stream.Collectors;
26  import org.apache.commons.lang3.ArrayUtils;
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.commons.lang3.tuple.Pair;
29  import org.apache.syncope.common.lib.request.AnyCR;
30  import org.apache.syncope.common.lib.request.AnyObjectCR;
31  import org.apache.syncope.common.lib.request.AnyObjectUR;
32  import org.apache.syncope.common.lib.request.AnyUR;
33  import org.apache.syncope.common.lib.request.GroupCR;
34  import org.apache.syncope.common.lib.request.GroupUR;
35  import org.apache.syncope.common.lib.request.UserCR;
36  import org.apache.syncope.common.lib.request.UserUR;
37  import org.apache.syncope.common.lib.to.ProvisioningResult;
38  import org.apache.syncope.common.lib.to.RemediationTO;
39  import org.apache.syncope.common.lib.types.IdMEntitlement;
40  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
41  import org.apache.syncope.core.persistence.api.dao.RemediationDAO;
42  import org.apache.syncope.core.persistence.api.dao.search.OrderByClause;
43  import org.apache.syncope.core.persistence.api.entity.Remediation;
44  import org.apache.syncope.core.provisioning.api.data.RemediationDataBinder;
45  import org.springframework.security.access.prepost.PreAuthorize;
46  import org.springframework.transaction.annotation.Transactional;
47  
48  public class RemediationLogic extends AbstractLogic<RemediationTO> {
49  
50      protected final UserLogic userLogic;
51  
52      protected final GroupLogic groupLogic;
53  
54      protected final AnyObjectLogic anyObjectLogic;
55  
56      protected final RemediationDataBinder binder;
57  
58      protected final RemediationDAO remediationDAO;
59  
60      public RemediationLogic(
61              final UserLogic userLogic,
62              final GroupLogic groupLogic,
63              final AnyObjectLogic anyObjectLogic,
64              final RemediationDataBinder binder,
65              final RemediationDAO remediationDAO) {
66  
67          this.userLogic = userLogic;
68          this.groupLogic = groupLogic;
69          this.anyObjectLogic = anyObjectLogic;
70          this.binder = binder;
71          this.remediationDAO = remediationDAO;
72      }
73  
74      @PreAuthorize("hasRole('" + IdMEntitlement.REMEDIATION_LIST + "')")
75      @Transactional(readOnly = true)
76      public Pair<Integer, List<RemediationTO>> list(
77              final OffsetDateTime before,
78              final OffsetDateTime after,
79              final int page,
80              final int size,
81              final List<OrderByClause> orderByClauses) {
82  
83          int count = remediationDAO.count(before, after);
84  
85          List<RemediationTO> result = remediationDAO.findAll(before, after, page, size, orderByClauses).stream().
86                  map(binder::getRemediationTO).collect(Collectors.toList());
87  
88          return Pair.of(count, result);
89      }
90  
91      @PreAuthorize("hasRole('" + IdMEntitlement.REMEDIATION_READ + "')")
92      @Transactional(readOnly = true)
93      public RemediationTO read(final String key) {
94          Remediation remediation = Optional.ofNullable(remediationDAO.find(key)).
95                  orElseThrow(() -> new NotFoundException(key));
96  
97          return binder.getRemediationTO(remediation);
98      }
99  
100     @PreAuthorize("hasRole('" + IdMEntitlement.REMEDIATION_DELETE + "')")
101     @Transactional
102     public void delete(final String key) {
103         Optional.ofNullable(remediationDAO.find(key)).
104                 orElseThrow(() -> new NotFoundException(key));
105 
106         remediationDAO.delete(key);
107     }
108 
109     @PreAuthorize("hasRole('" + IdMEntitlement.REMEDIATION_REMEDY + "')")
110     public ProvisioningResult<?> remedy(final String key, final AnyCR anyCR, final boolean nullPriorityAsync) {
111         ProvisioningResult<?> result;
112         switch (read(key).getAnyType()) {
113             case "USER":
114                 result = userLogic.create((UserCR) anyCR, nullPriorityAsync);
115                 break;
116 
117             case "GROUP":
118                 result = groupLogic.create((GroupCR) anyCR, nullPriorityAsync);
119                 break;
120 
121             default:
122                 result = anyObjectLogic.create((AnyObjectCR) anyCR, nullPriorityAsync);
123         }
124 
125         remediationDAO.delete(key);
126 
127         return result;
128     }
129 
130     @PreAuthorize("hasRole('" + IdMEntitlement.REMEDIATION_REMEDY + "')")
131     public ProvisioningResult<?> remedy(final String key, final AnyUR anyUR, final boolean nullPriorityAsync) {
132         ProvisioningResult<?> result;
133         switch (read(key).getAnyType()) {
134             case "USER":
135                 result = userLogic.update((UserUR) anyUR, nullPriorityAsync);
136                 break;
137 
138             case "GROUP":
139                 result = groupLogic.update((GroupUR) anyUR, nullPriorityAsync);
140                 break;
141 
142             default:
143                 result = anyObjectLogic.update((AnyObjectUR) anyUR, nullPriorityAsync);
144         }
145 
146         remediationDAO.delete(key);
147 
148         return result;
149     }
150 
151     @PreAuthorize("hasRole('" + IdMEntitlement.REMEDIATION_REMEDY + "')")
152     public ProvisioningResult<?> remedy(final String key, final String anyKey, final boolean nullPriorityAsync) {
153         ProvisioningResult<?> result;
154         switch (read(key).getAnyType()) {
155             case "USER":
156                 result = userLogic.delete(anyKey, nullPriorityAsync);
157                 break;
158 
159             case "GROUP":
160                 result = groupLogic.delete(anyKey, nullPriorityAsync);
161                 break;
162 
163             default:
164                 result = anyObjectLogic.delete(anyKey, nullPriorityAsync);
165         }
166 
167         remediationDAO.delete(key);
168 
169         return result;
170     }
171 
172     @Override
173     protected RemediationTO resolveReference(final Method method, final Object... args)
174             throws UnresolvedReferenceException {
175 
176         String key = null;
177 
178         if (ArrayUtils.isNotEmpty(args)) {
179             for (int i = 0; key == null && i < args.length; i++) {
180                 if (args[i] instanceof String) {
181                     key = (String) args[i];
182                 } else if (args[i] instanceof RemediationTO) {
183                     key = ((RemediationTO) args[i]).getKey();
184                 }
185             }
186         }
187 
188         if (StringUtils.isNotBlank(key)) {
189             try {
190                 return binder.getRemediationTO(remediationDAO.find(key));
191             } catch (Throwable ignore) {
192                 LOG.debug("Unresolved reference", ignore);
193                 throw new UnresolvedReferenceException(ignore);
194             }
195         }
196 
197         throw new UnresolvedReferenceException();
198     }
199 }