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.util.List;
23  import java.util.Optional;
24  import java.util.stream.Collectors;
25  import org.apache.commons.lang3.ArrayUtils;
26  import org.apache.syncope.common.lib.SyncopeClientException;
27  import org.apache.syncope.common.lib.policy.PolicyTO;
28  import org.apache.syncope.common.lib.types.ClientExceptionType;
29  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
30  import org.apache.syncope.common.lib.types.PolicyType;
31  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
32  import org.apache.syncope.core.persistence.api.dao.PolicyDAO;
33  import org.apache.syncope.core.persistence.api.entity.policy.Policy;
34  import org.apache.syncope.core.persistence.api.entity.policy.PolicyUtils;
35  import org.apache.syncope.core.persistence.api.entity.policy.PolicyUtilsFactory;
36  import org.apache.syncope.core.provisioning.api.data.PolicyDataBinder;
37  import org.springframework.security.access.prepost.PreAuthorize;
38  import org.springframework.transaction.annotation.Transactional;
39  
40  public class PolicyLogic extends AbstractTransactionalLogic<PolicyTO> {
41  
42      protected final PolicyDAO policyDAO;
43  
44      protected final PolicyDataBinder binder;
45  
46      protected final PolicyUtilsFactory policyUtilsFactory;
47  
48      public PolicyLogic(
49              final PolicyDAO policyDAO,
50              final PolicyDataBinder binder,
51              final PolicyUtilsFactory policyUtilsFactory) {
52  
53          this.policyDAO = policyDAO;
54          this.binder = binder;
55          this.policyUtilsFactory = policyUtilsFactory;
56      }
57  
58      @PreAuthorize("hasRole('" + IdRepoEntitlement.POLICY_CREATE + "')")
59      public <T extends PolicyTO> T create(final PolicyType type, final T policyTO) {
60          PolicyUtils policyUtils = policyUtilsFactory.getInstance(policyTO);
61          if (policyUtils.getType() != type) {
62              SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
63              sce.getElements().add("Found " + type + ", expected " + policyUtils.getType());
64              throw sce;
65          }
66  
67          return binder.getPolicyTO(policyDAO.save(binder.create(policyTO)));
68      }
69  
70      @PreAuthorize("hasRole('" + IdRepoEntitlement.POLICY_UPDATE + "')")
71      public PolicyTO update(final PolicyType type, final PolicyTO policyTO) {
72          Policy policy = Optional.ofNullable(policyDAO.<Policy>find(policyTO.getKey())).
73                  orElseThrow(() -> new NotFoundException("Policy " + policyTO.getKey() + " not found"));
74  
75          PolicyUtils policyUtils = policyUtilsFactory.getInstance(policy);
76          if (policyUtils.getType() != type) {
77              SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
78              sce.getElements().add("Found " + type + ", expected " + policyUtils.getType());
79              throw sce;
80          }
81  
82          return binder.getPolicyTO(policyDAO.save(binder.update(policy, policyTO)));
83      }
84  
85      @PreAuthorize("hasRole('" + IdRepoEntitlement.POLICY_LIST + "')")
86      @Transactional(readOnly = true)
87      public <T extends PolicyTO> List<T> list(final PolicyType type) {
88          PolicyUtils policyUtils = policyUtilsFactory.getInstance(type);
89  
90          return policyDAO.find(policyUtils.policyClass()).stream().
91                  <T>map(binder::getPolicyTO).collect(Collectors.toList());
92      }
93  
94      @PreAuthorize("hasRole('" + IdRepoEntitlement.POLICY_READ + "')")
95      @Transactional(readOnly = true)
96      public <T extends PolicyTO> T read(final PolicyType type, final String key) {
97          Policy policy = Optional.ofNullable(policyDAO.<Policy>find(key)).
98                  orElseThrow(() -> new NotFoundException("Policy " + key + " not found"));
99  
100         PolicyUtils policyUtils = policyUtilsFactory.getInstance(policy);
101         if (type != null && policyUtils.getType() != type) {
102             SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
103             sce.getElements().add("Found " + type + ", expected " + policyUtils.getType());
104             throw sce;
105         }
106 
107         return binder.getPolicyTO(policy);
108     }
109 
110     @PreAuthorize("hasRole('" + IdRepoEntitlement.POLICY_DELETE + "')")
111     public <T extends PolicyTO> T delete(final PolicyType type, final String key) {
112         Policy policy = Optional.ofNullable(policyDAO.<Policy>find(key)).
113                 orElseThrow(() -> new NotFoundException("Policy " + key + " not found"));
114 
115         PolicyUtils policyUtils = policyUtilsFactory.getInstance(policy);
116         if (type != null && policyUtils.getType() != type) {
117             SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
118             sce.getElements().add("Found " + type + ", expected " + policyUtils.getType());
119             throw sce;
120         }
121 
122         T deleted = binder.getPolicyTO(policy);
123         policyDAO.delete(policy);
124 
125         return deleted;
126     }
127 
128     @Override
129     protected PolicyTO resolveReference(final Method method, final Object... args)
130             throws UnresolvedReferenceException {
131 
132         if (ArrayUtils.isEmpty(args) || args.length != 2) {
133             throw new UnresolvedReferenceException();
134         }
135 
136         try {
137             final String key;
138             final PolicyType type;
139 
140             if (args[0] instanceof PolicyType) {
141                 type = (PolicyType) args[0];
142             } else {
143                 throw new RuntimeException("Invalid Policy type");
144             }
145 
146             if (args[1] instanceof String) {
147                 key = (String) args[1];
148             } else if (args[1] instanceof PolicyTO) {
149                 key = ((PolicyTO) args[1]).getKey();
150             } else {
151                 throw new RuntimeException("Invalid ClientApp key");
152             }
153 
154             return read(type, key);
155         } catch (Throwable t) {
156             throw new UnresolvedReferenceException();
157         }
158     }
159 }