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.persistence.jpa.dao;
20  
21  import java.util.List;
22  import java.util.Set;
23  import java.util.stream.Collectors;
24  import javax.persistence.TypedQuery;
25  import org.apache.syncope.common.lib.types.IdMEntitlement;
26  import org.apache.syncope.core.persistence.api.dao.ConnInstanceDAO;
27  import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
28  import org.apache.syncope.core.persistence.api.entity.ConnInstance;
29  import org.apache.syncope.core.persistence.api.entity.ExternalResource;
30  import org.apache.syncope.core.persistence.jpa.entity.JPAConnInstance;
31  import org.apache.syncope.core.spring.security.AuthContextUtils;
32  import org.apache.syncope.core.spring.security.DelegatedAdministrationException;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  public class JPAConnInstanceDAO extends AbstractDAO<ConnInstance> implements ConnInstanceDAO {
36  
37      protected final ExternalResourceDAO resourceDAO;
38  
39      public JPAConnInstanceDAO(final ExternalResourceDAO resourceDAO) {
40          this.resourceDAO = resourceDAO;
41      }
42  
43      @Transactional(readOnly = true)
44      @Override
45      public ConnInstance find(final String key) {
46          return entityManager().find(JPAConnInstance.class, key);
47      }
48  
49      @Transactional(readOnly = true)
50      @Override
51      public ConnInstance authFind(final String key) {
52          ConnInstance connInstance = find(key);
53          if (connInstance == null) {
54              return null;
55          }
56  
57          Set<String> authRealms = AuthContextUtils.getAuthorizations().get(IdMEntitlement.CONNECTOR_READ);
58          if (authRealms == null || authRealms.isEmpty()
59                  || !authRealms.stream().anyMatch(
60                          realm -> connInstance.getAdminRealm().getFullPath().startsWith(realm))) {
61  
62              throw new DelegatedAdministrationException(
63                      connInstance.getAdminRealm().getFullPath(),
64                      ConnInstance.class.getSimpleName(),
65                      connInstance.getKey());
66          }
67  
68          return connInstance;
69      }
70  
71      @Override
72      public List<ConnInstance> findAll() {
73          final Set<String> authRealms = AuthContextUtils.getAuthorizations().get(IdMEntitlement.CONNECTOR_LIST);
74          if (authRealms == null || authRealms.isEmpty()) {
75              return List.of();
76          }
77  
78          TypedQuery<ConnInstance> query = entityManager().createQuery(
79                  "SELECT e FROM " + JPAConnInstance.class.getSimpleName() + " e", ConnInstance.class);
80  
81          return query.getResultList().stream().filter(connInstance -> authRealms.stream().
82                  anyMatch(realm -> connInstance.getAdminRealm().getFullPath().startsWith(realm))).
83                  collect(Collectors.toList());
84      }
85  
86      @Override
87      public ConnInstance save(final ConnInstance connector) {
88          ((JPAConnInstance) connector).list2json();
89          return entityManager().merge(connector);
90      }
91  
92      @Override
93      public void delete(final String key) {
94          ConnInstance connInstance = find(key);
95          if (connInstance == null) {
96              return;
97          }
98  
99          connInstance.getResources().stream().map(ExternalResource::getKey).collect(Collectors.toList()).
100                 forEach(resourceDAO::delete);
101 
102         entityManager().remove(connInstance);
103     }
104 }