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.time.OffsetDateTime;
22  import java.util.List;
23  import java.util.Optional;
24  import java.util.stream.Collectors;
25  import javax.persistence.TypedQuery;
26  import org.apache.syncope.core.persistence.api.dao.DelegationDAO;
27  import org.apache.syncope.core.persistence.api.entity.Delegation;
28  import org.apache.syncope.core.persistence.api.entity.Role;
29  import org.apache.syncope.core.persistence.api.entity.user.User;
30  import org.apache.syncope.core.persistence.jpa.entity.JPADelegation;
31  
32  public class JPADelegationDAO extends AbstractDAO<Delegation> implements DelegationDAO {
33  
34      @Override
35      public Delegation find(final String key) {
36          return entityManager().find(JPADelegation.class, key);
37      }
38  
39      @Override
40      public Optional<String> findValidFor(final String delegating, final String delegated) {
41          TypedQuery<Delegation> query = entityManager().createQuery(
42                  "SELECT e FROM " + JPADelegation.class.getSimpleName() + " e "
43                  + "WHERE e.delegating.id=:delegating AND e.delegated.id=:delegated "
44                  + "AND e.start <= :now AND (e.end IS NULL OR e.end >= :now)", Delegation.class);
45          query.setParameter("delegating", delegating);
46          query.setParameter("delegated", delegated);
47          query.setParameter("now", OffsetDateTime.now());
48          query.setMaxResults(1);
49  
50          List<Delegation> raw = query.getResultList();
51          return raw.isEmpty() ? Optional.empty() : Optional.of(raw.get(0).getKey());
52      }
53  
54      @Override
55      public List<String> findValidDelegating(final String delegated) {
56          TypedQuery<Delegation> query = entityManager().createQuery(
57                  "SELECT e FROM " + JPADelegation.class.getSimpleName() + " e "
58                  + "WHERE e.delegated.id=:delegated "
59                  + "AND e.start <= :now AND (e.end IS NULL OR e.end >= :now)", Delegation.class);
60          query.setParameter("delegated", delegated);
61          query.setParameter("now", OffsetDateTime.now());
62  
63          return query.getResultList().stream().
64                  map(delegation -> delegation.getDelegating().getUsername()).
65                  collect(Collectors.toList());
66      }
67  
68      @Override
69      public List<Delegation> findByDelegating(final User user) {
70          TypedQuery<Delegation> query = entityManager().createQuery(
71                  "SELECT e FROM " + JPADelegation.class.getSimpleName() + " e "
72                  + "WHERE e.delegating=:user", Delegation.class);
73          query.setParameter("user", user);
74          return query.getResultList();
75      }
76  
77      @Override
78      public List<Delegation> findByDelegated(final User user) {
79          TypedQuery<Delegation> query = entityManager().createQuery(
80                  "SELECT e FROM " + JPADelegation.class.getSimpleName() + " e "
81                  + "WHERE e.delegated=:user", Delegation.class);
82          query.setParameter("user", user);
83          return query.getResultList();
84      }
85  
86      @Override
87      public List<Delegation> findByRole(final Role role) {
88          TypedQuery<Delegation> query = entityManager().createQuery(
89                  "SELECT e FROM " + JPADelegation.class.getSimpleName() + " e "
90                  + "WHERE :role MEMBER OF e.roles", Delegation.class);
91          query.setParameter("role", role);
92          return query.getResultList();
93      }
94  
95      @Override
96      public List<Delegation> findAll() {
97          TypedQuery<Delegation> query = entityManager().createQuery(
98                  "SELECT e FROM " + JPADelegation.class.getSimpleName() + " e ", Delegation.class);
99          return query.getResultList();
100     }
101 
102     @Override
103     public Delegation save(final Delegation delegation) {
104         return entityManager().merge(delegation);
105     }
106 
107     @Override
108     public void delete(final Delegation delegation) {
109         entityManager().remove(delegation);
110     }
111 
112     @Override
113     public void delete(final String key) {
114         Delegation delegation = find(key);
115         if (delegation == null) {
116             return;
117         }
118 
119         delete(delegation);
120     }
121 }