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