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.AuthModuleTO;
26  import org.apache.syncope.common.lib.types.AMEntitlement;
27  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
28  import org.apache.syncope.core.persistence.api.dao.AuthModuleDAO;
29  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
30  import org.apache.syncope.core.persistence.api.entity.am.AuthModule;
31  import org.apache.syncope.core.provisioning.api.data.AuthModuleDataBinder;
32  import org.springframework.security.access.prepost.PreAuthorize;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  public class AuthModuleLogic extends AbstractTransactionalLogic<AuthModuleTO> {
36  
37      protected final AuthModuleDataBinder binder;
38  
39      protected final AuthModuleDAO authModuleDAO;
40  
41      public AuthModuleLogic(final AuthModuleDataBinder binder, final AuthModuleDAO authModuleDAO) {
42          this.binder = binder;
43          this.authModuleDAO = authModuleDAO;
44      }
45  
46      @PreAuthorize("hasRole('" + AMEntitlement.AUTH_MODULE_CREATE + "')")
47      public AuthModuleTO create(final AuthModuleTO authModuleTO) {
48          return binder.getAuthModuleTO(authModuleDAO.save(binder.create(authModuleTO)));
49      }
50  
51      @PreAuthorize("hasRole('" + AMEntitlement.AUTH_MODULE_UPDATE + "')")
52      public AuthModuleTO update(final AuthModuleTO authModuleTO) {
53          AuthModule authModule = authModuleDAO.find(authModuleTO.getKey());
54          if (authModule == null) {
55              throw new NotFoundException("AuthModule " + authModuleTO.getKey() + " not found");
56          }
57  
58          return binder.getAuthModuleTO(authModuleDAO.save(binder.update(authModule, authModuleTO)));
59      }
60  
61      @PreAuthorize("hasRole('" + AMEntitlement.AUTH_MODULE_LIST + "') or hasRole('" + IdRepoEntitlement.ANONYMOUS + "')")
62      @Transactional(readOnly = true)
63      public List<AuthModuleTO> list() {
64          return authModuleDAO.findAll().stream().map(binder::getAuthModuleTO).collect(Collectors.toList());
65      }
66  
67      @PreAuthorize("hasRole('" + AMEntitlement.AUTH_MODULE_READ + "')")
68      @Transactional(readOnly = true)
69      public AuthModuleTO read(final String key) {
70          AuthModule authModule = authModuleDAO.find(key);
71          if (authModule == null) {
72              throw new NotFoundException("AuthModule " + key + " not found");
73          }
74  
75          return binder.getAuthModuleTO(authModule);
76      }
77  
78      @PreAuthorize("hasRole('" + AMEntitlement.AUTH_MODULE_DELETE + "')")
79      public AuthModuleTO delete(final String key) {
80          AuthModule authModule = authModuleDAO.find(key);
81          if (authModule == null) {
82              throw new NotFoundException("AuthModule " + key + " not found");
83          }
84  
85          AuthModuleTO deleted = binder.getAuthModuleTO(authModule);
86          authModuleDAO.delete(authModule);
87  
88          return deleted;
89      }
90  
91      @Override
92      protected AuthModuleTO resolveReference(final Method method, final Object... args)
93              throws UnresolvedReferenceException {
94  
95          if (ArrayUtils.isEmpty(args)) {
96              throw new UnresolvedReferenceException();
97          }
98  
99          final String key;
100 
101         if (args[0] instanceof String) {
102             key = (String) args[0];
103         } else if (args[0] instanceof AuthModuleTO) {
104             key = ((AuthModuleTO) args[0]).getKey();
105         } else {
106             throw new UnresolvedReferenceException();
107         }
108 
109         try {
110             return binder.getAuthModuleTO(authModuleDAO.find(key));
111         } catch (Throwable ignore) {
112             LOG.debug("Unresolved reference", ignore);
113             throw new UnresolvedReferenceException(ignore);
114         }
115     }
116 }