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.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 }