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.stream.Collectors;
24  import org.apache.commons.lang3.ArrayUtils;
25  import org.apache.syncope.common.lib.to.DynRealmTO;
26  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
27  import org.apache.syncope.core.persistence.api.dao.DynRealmDAO;
28  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
29  import org.apache.syncope.core.persistence.api.entity.DynRealm;
30  import org.apache.syncope.core.provisioning.api.data.DynRealmDataBinder;
31  import org.springframework.security.access.prepost.PreAuthorize;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  public class DynRealmLogic extends AbstractTransactionalLogic<DynRealmTO> {
35  
36      protected final DynRealmDataBinder binder;
37  
38      protected final DynRealmDAO dynRealmDAO;
39  
40      public DynRealmLogic(final DynRealmDataBinder binder, final DynRealmDAO dynRealmDAO) {
41          this.binder = binder;
42          this.dynRealmDAO = dynRealmDAO;
43      }
44  
45      @PreAuthorize("hasRole('" + IdRepoEntitlement.DYNREALM_READ + "')")
46      @Transactional(readOnly = true)
47      public DynRealmTO read(final String key) {
48          DynRealm dynRealm = dynRealmDAO.find(key);
49          if (dynRealm == null) {
50              LOG.error("Could not find dynamic realm '" + key + '\'');
51  
52              throw new NotFoundException(key);
53          }
54  
55          return binder.getDynRealmTO(dynRealm);
56      }
57  
58      @Transactional(readOnly = true)
59      public List<DynRealmTO> list() {
60          return dynRealmDAO.findAll().stream().map(binder::getDynRealmTO).collect(Collectors.toList());
61      }
62  
63      @PreAuthorize("hasRole('" + IdRepoEntitlement.DYNREALM_CREATE + "')")
64      public DynRealmTO create(final DynRealmTO dynRealmTO) {
65          return binder.getDynRealmTO(binder.create(dynRealmTO));
66      }
67  
68      @PreAuthorize("hasRole('" + IdRepoEntitlement.DYNREALM_UPDATE + "')")
69      public DynRealmTO update(final DynRealmTO dynRealmTO) {
70          DynRealm dynRealm = dynRealmDAO.find(dynRealmTO.getKey());
71          if (dynRealm == null) {
72              LOG.error("Could not find dynamic realm '" + dynRealmTO.getKey() + '\'');
73              throw new NotFoundException(dynRealmTO.getKey());
74          }
75  
76          return binder.getDynRealmTO(binder.update(dynRealm, dynRealmTO));
77      }
78  
79      @PreAuthorize("hasRole('" + IdRepoEntitlement.DYNREALM_DELETE + "')")
80      public DynRealmTO delete(final String key) {
81          DynRealm dynRealm = dynRealmDAO.find(key);
82          if (dynRealm == null) {
83              LOG.error("Could not find dynamic realm '" + key + '\'');
84  
85              throw new NotFoundException(key);
86          }
87  
88          DynRealmTO deleted = binder.getDynRealmTO(dynRealm);
89          dynRealmDAO.delete(key);
90          return deleted;
91      }
92  
93      @Override
94      protected DynRealmTO resolveReference(final Method method, final Object... args)
95              throws UnresolvedReferenceException {
96  
97          String key = null;
98  
99          if (ArrayUtils.isNotEmpty(args)) {
100             for (int i = 0; key == null && i < args.length; i++) {
101                 if (args[i] instanceof String) {
102                     key = (String) args[i];
103                 } else if (args[i] instanceof DynRealmTO) {
104                     key = ((DynRealmTO) args[i]).getKey();
105                 }
106             }
107         }
108 
109         if (key != null) {
110             try {
111                 return binder.getDynRealmTO(dynRealmDAO.find(key));
112             } catch (Throwable ignore) {
113                 LOG.debug("Unresolved reference", ignore);
114                 throw new UnresolvedReferenceException(ignore);
115             }
116         }
117 
118         throw new UnresolvedReferenceException();
119     }
120 
121 }