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.util.List;
22  import java.util.Optional;
23  import java.util.stream.Collectors;
24  import org.apache.commons.lang3.tuple.Pair;
25  import org.apache.syncope.common.lib.to.AuthProfileTO;
26  import org.apache.syncope.common.lib.types.AMEntitlement;
27  import org.apache.syncope.core.persistence.api.dao.AuthProfileDAO;
28  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
29  import org.apache.syncope.core.persistence.api.entity.am.AuthProfile;
30  import org.apache.syncope.core.provisioning.api.data.AuthProfileDataBinder;
31  import org.springframework.security.access.prepost.PreAuthorize;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  public class AuthProfileLogic extends AbstractAuthProfileLogic {
35  
36      public AuthProfileLogic(final AuthProfileDAO authProfileDAO, final AuthProfileDataBinder binder) {
37          super(authProfileDAO, binder);
38      }
39  
40      @PreAuthorize("hasRole('" + AMEntitlement.AUTH_PROFILE_DELETE + "') ")
41      public void delete(final String key) {
42          authProfileDAO.delete(key);
43      }
44  
45      @PreAuthorize("hasRole('" + AMEntitlement.AUTH_PROFILE_READ + "') ")
46      @Transactional(readOnly = true)
47      public AuthProfileTO read(final String key) {
48          return Optional.ofNullable(authProfileDAO.find(key)).
49                  map(binder::getAuthProfileTO).
50                  orElseThrow(() -> new NotFoundException(key + " not found"));
51      }
52  
53      @PreAuthorize("hasRole('" + AMEntitlement.AUTH_PROFILE_CREATE + "') ")
54      public AuthProfileTO create(final AuthProfileTO authProfileTO) {
55          return binder.getAuthProfileTO(authProfileDAO.save(binder.create(authProfileTO)));
56      }
57  
58      @PreAuthorize("hasRole('" + AMEntitlement.AUTH_PROFILE_UPDATE + "') ")
59      public void update(final AuthProfileTO authProfileTO) {
60          AuthProfile authProfile = Optional.ofNullable(authProfileDAO.find(authProfileTO.getKey())).
61                  orElseThrow(() -> new NotFoundException(authProfileTO.getKey() + " not found"));
62          binder.update(authProfile, authProfileTO);
63          authProfileDAO.save(authProfile);
64      }
65  
66      @PreAuthorize("hasRole('" + AMEntitlement.AUTH_PROFILE_LIST + "')")
67      @Transactional(readOnly = true)
68      public Pair<Integer, List<AuthProfileTO>> list(final int page, final int size) {
69          int count = authProfileDAO.count();
70  
71          List<AuthProfileTO> result = authProfileDAO.findAll(page, size).
72                  stream().
73                  map(binder::getAuthProfileTO).
74                  collect(Collectors.toList());
75  
76          return Pair.of(count, result);
77      }
78  }