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.Optional;
24  import java.util.stream.Collectors;
25  import org.apache.commons.lang3.ArrayUtils;
26  import org.apache.syncope.common.lib.to.SAML2IdPEntityTO;
27  import org.apache.syncope.common.lib.types.AMEntitlement;
28  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
29  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
30  import org.apache.syncope.core.persistence.api.dao.SAML2IdPEntityDAO;
31  import org.apache.syncope.core.persistence.api.entity.am.SAML2IdPEntity;
32  import org.apache.syncope.core.provisioning.api.data.SAML2IdPEntityDataBinder;
33  import org.springframework.security.access.prepost.PreAuthorize;
34  import org.springframework.transaction.annotation.Transactional;
35  
36  public class SAML2IdPEntityLogic extends AbstractTransactionalLogic<SAML2IdPEntityTO> {
37  
38      protected final SAML2IdPEntityDataBinder binder;
39  
40      protected final SAML2IdPEntityDAO entityDAO;
41  
42      public SAML2IdPEntityLogic(final SAML2IdPEntityDataBinder binder, final SAML2IdPEntityDAO entityDAO) {
43          this.binder = binder;
44          this.entityDAO = entityDAO;
45      }
46  
47      @PreAuthorize("hasRole('" + AMEntitlement.SAML2_IDP_ENTITY_LIST + "')")
48      @Transactional(readOnly = true)
49      public List<SAML2IdPEntityTO> list() {
50          return entityDAO.findAll().stream().
51                  map(binder::getSAML2IdPEntityTO).
52                  collect(Collectors.toList());
53      }
54  
55      @PreAuthorize("hasRole('" + AMEntitlement.SAML2_IDP_ENTITY_GET + "') "
56              + "or hasRole('" + IdRepoEntitlement.ANONYMOUS + "')")
57      @Transactional(readOnly = true)
58      public SAML2IdPEntityTO get(final String key) {
59          return Optional.ofNullable(entityDAO.find(key)).
60                  map(binder::getSAML2IdPEntityTO).
61                  orElseThrow(() -> new NotFoundException(key + " not found"));
62      }
63  
64      @PreAuthorize("hasRole('" + AMEntitlement.SAML2_IDP_ENTITY_SET + "') "
65              + "or hasRole('" + IdRepoEntitlement.ANONYMOUS + "')")
66      public SAML2IdPEntityTO set(final SAML2IdPEntityTO entityTO) {
67          SAML2IdPEntity entity = Optional.ofNullable(entityDAO.find(entityTO.getKey())).
68                  map(metadata -> binder.update(metadata, entityTO)).
69                  orElseGet(() -> binder.create(entityTO));
70          return binder.getSAML2IdPEntityTO(entityDAO.save(entity));
71      }
72  
73      @Override
74      protected SAML2IdPEntityTO resolveReference(final Method method, final Object... args)
75              throws UnresolvedReferenceException {
76  
77          String key = null;
78          if (ArrayUtils.isNotEmpty(args)) {
79              for (int i = 0; key == null && i < args.length; i++) {
80                  if (args[i] instanceof String) {
81                      key = (String) args[i];
82                  } else if (args[i] instanceof SAML2IdPEntityTO) {
83                      key = ((SAML2IdPEntityTO) args[i]).getKey();
84                  }
85              }
86          }
87  
88          if (key != null) {
89              try {
90                  return binder.getSAML2IdPEntityTO(entityDAO.find(key));
91              } catch (Throwable ignore) {
92                  LOG.debug("Unresolved reference", ignore);
93                  throw new UnresolvedReferenceException(ignore);
94              }
95          }
96  
97          throw new UnresolvedReferenceException();
98      }
99  }