1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }