1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.logic;
20
21 import java.lang.reflect.Method;
22 import java.util.List;
23 import java.util.Objects;
24 import java.util.Optional;
25 import java.util.stream.Collectors;
26 import org.apache.syncope.common.keymaster.client.api.DomainWatcher;
27 import org.apache.syncope.common.keymaster.client.api.KeymasterException;
28 import org.apache.syncope.common.keymaster.client.api.model.Domain;
29 import org.apache.syncope.common.lib.SyncopeConstants;
30 import org.apache.syncope.common.lib.to.EntityTO;
31 import org.apache.syncope.common.lib.types.CipherAlgorithm;
32 import org.apache.syncope.core.persistence.api.dao.DomainDAO;
33 import org.apache.syncope.core.persistence.api.dao.DuplicateException;
34 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
35 import org.apache.syncope.core.persistence.api.entity.DomainEntity;
36 import org.apache.syncope.core.persistence.api.entity.SelfKeymasterEntityFactory;
37 import org.springframework.security.access.prepost.PreAuthorize;
38
39 public class DomainLogic extends AbstractTransactionalLogic<EntityTO> {
40
41 protected final DomainDAO domainDAO;
42
43 protected final SelfKeymasterEntityFactory entityFactory;
44
45 protected final DomainWatcher domainWatcher;
46
47 public DomainLogic(
48 final DomainDAO domainDAO,
49 final SelfKeymasterEntityFactory entityFactory,
50 final DomainWatcher domainWatcher) {
51
52 this.domainDAO = domainDAO;
53 this.entityFactory = entityFactory;
54 this.domainWatcher = domainWatcher;
55 }
56
57 @PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
58 public List<Domain> list() {
59 return domainDAO.findAll().stream().map(DomainEntity::get).collect(Collectors.toList());
60 }
61
62 @PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
63 public Domain read(final String key) {
64 DomainEntity domain = Optional.ofNullable(domainDAO.find(key)).
65 orElseThrow(() -> new NotFoundException("Domain " + key));
66
67 return domain.get();
68 }
69
70 @PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
71 public Domain create(final Domain domain) {
72 if (Objects.equals(domain.getKey(), SyncopeConstants.MASTER_DOMAIN)) {
73 throw new KeymasterException("Cannot create domain " + SyncopeConstants.MASTER_DOMAIN);
74 }
75
76 if (domainDAO.find(domain.getKey()) != null) {
77 throw new DuplicateException("Domain " + domain.getKey() + " already existing");
78 }
79
80 DomainEntity domainEntity = entityFactory.newDomainEntity();
81 domainEntity.setKey(domain.getKey());
82 domainEntity.set(domain);
83 domainEntity = domainDAO.save(domainEntity);
84
85 domainWatcher.added(domain);
86
87 return domainEntity.get();
88 }
89
90 @PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
91 public void changeAdminPassword(final String key, final String password, final CipherAlgorithm cipherAlgorithm) {
92 DomainEntity domain = Optional.ofNullable(domainDAO.find(key)).
93 orElseThrow(() -> new NotFoundException("Domain " + key));
94
95 Domain domainObj = domain.get();
96 domainObj.setAdminPassword(password);
97 domainObj.setAdminCipherAlgorithm(cipherAlgorithm);
98 domain.set(domainObj);
99 domainDAO.save(domain);
100 }
101
102 @PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
103 public void adjustPoolSize(final String key, final int poolMaxActive, final int poolMinIdle) {
104 DomainEntity domain = Optional.ofNullable(domainDAO.find(key)).
105 orElseThrow(() -> new NotFoundException("Domain " + key));
106
107 Domain domainObj = domain.get();
108 domainObj.setPoolMaxActive(poolMaxActive);
109 domainObj.setPoolMinIdle(poolMinIdle);
110 domain.set(domainObj);
111 domainDAO.save(domain);
112 }
113
114 @PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
115 public void delete(final String key) {
116 domainDAO.delete(key);
117
118 domainWatcher.removed(key);
119 }
120
121 @Override
122 protected EntityTO resolveReference(final Method method, final Object... args) throws UnresolvedReferenceException {
123 throw new UnsupportedOperationException();
124 }
125 }