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.provisioning.java.data;
20  
21  import java.util.Iterator;
22  import java.util.Optional;
23  import java.util.stream.Collectors;
24  import org.apache.syncope.common.lib.SyncopeClientException;
25  import org.apache.syncope.common.lib.to.DelegationTO;
26  import org.apache.syncope.common.lib.to.RoleTO;
27  import org.apache.syncope.common.lib.types.ClientExceptionType;
28  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
29  import org.apache.syncope.core.persistence.api.dao.RoleDAO;
30  import org.apache.syncope.core.persistence.api.dao.UserDAO;
31  import org.apache.syncope.core.persistence.api.entity.Delegation;
32  import org.apache.syncope.core.persistence.api.entity.EntityFactory;
33  import org.apache.syncope.core.persistence.api.entity.Role;
34  import org.apache.syncope.core.persistence.api.entity.user.User;
35  import org.apache.syncope.core.provisioning.api.data.DelegationDataBinder;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  public class DelegationDataBinderImpl implements DelegationDataBinder {
40  
41      protected static final Logger LOG = LoggerFactory.getLogger(DelegationDataBinder.class);
42  
43      protected final UserDAO userDAO;
44  
45      protected final RoleDAO roleDAO;
46  
47      protected final EntityFactory entityFactory;
48  
49      public DelegationDataBinderImpl(
50              final UserDAO userDAO,
51              final RoleDAO roleDAO,
52              final EntityFactory entityFactory) {
53  
54          this.userDAO = userDAO;
55          this.roleDAO = roleDAO;
56          this.entityFactory = entityFactory;
57      }
58  
59      @Override
60      public Delegation create(final DelegationTO delegationTO) {
61          Delegation delegation = entityFactory.newEntity(Delegation.class);
62  
63          User delegating = Optional.ofNullable(userDAO.find(delegationTO.getDelegating())).
64                  orElseThrow(() -> new NotFoundException("Delegating User " + delegationTO.getDelegating()));
65          delegation.setDelegating(delegating);
66  
67          User delegated = Optional.ofNullable(userDAO.find(delegationTO.getDelegated())).
68                  orElseThrow(() -> new NotFoundException("Delegated User " + delegationTO.getDelegated()));
69          delegation.setDelegated(delegated);
70  
71          return update(delegation, delegationTO);
72      }
73  
74      @Override
75      public Delegation update(final Delegation delegation, final DelegationTO delegationTO) {
76          delegation.setStart(delegationTO.getStart());
77          delegation.setEnd(delegationTO.getEnd());
78  
79          // 1. add or update all (valid) roles from TO
80          delegationTO.getRoles().forEach(roleTO -> {
81              if (roleTO == null) {
82                  LOG.error("Null {}", RoleTO.class.getSimpleName());
83              } else {
84                  Role role = roleDAO.find(roleTO);
85                  if (role == null) {
86                      SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRole);
87                      sce.getElements().add("Role " + roleTO + " not found");
88                      throw sce;
89                  }
90  
91                  delegation.add(role);
92              }
93          });
94  
95          // 2. remove all roles not contained in the TO
96          for (Iterator<? extends Role> itor = delegation.getRoles().iterator(); itor.hasNext();) {
97              Role role = itor.next();
98              if (!delegationTO.getRoles().stream().anyMatch(roleKey -> roleKey.equals(role.getKey()))) {
99                  itor.remove();
100             }
101         }
102 
103         return delegation;
104     }
105 
106     @Override
107     public DelegationTO getDelegationTO(final Delegation delegation) {
108         DelegationTO delegationTO = new DelegationTO();
109 
110         delegationTO.setKey(delegation.getKey());
111         delegationTO.setDelegating(delegation.getDelegating().getKey());
112         delegationTO.setDelegated(delegation.getDelegated().getKey());
113         delegationTO.setStart(delegation.getStart());
114         delegationTO.setEnd(delegation.getEnd());
115         delegationTO.getRoles().addAll(delegation.getRoles().stream().map(Role::getKey).collect(Collectors.toSet()));
116 
117         return delegationTO;
118     }
119 }