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.stream.Collectors;
24 import org.apache.commons.lang3.ArrayUtils;
25 import org.apache.syncope.common.lib.SyncopeConstants;
26 import org.apache.syncope.common.lib.to.FIQLQueryTO;
27 import org.apache.syncope.common.lib.types.AnyTypeKind;
28 import org.apache.syncope.core.persistence.api.dao.FIQLQueryDAO;
29 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
30 import org.apache.syncope.core.persistence.api.dao.UserDAO;
31 import org.apache.syncope.core.persistence.api.entity.FIQLQuery;
32 import org.apache.syncope.core.provisioning.api.data.FIQLQueryDataBinder;
33 import org.apache.syncope.core.spring.security.AuthContextUtils;
34 import org.apache.syncope.core.spring.security.DelegatedAdministrationException;
35 import org.springframework.security.access.prepost.PreAuthorize;
36 import org.springframework.transaction.annotation.Transactional;
37
38 public class FIQLQueryLogic extends AbstractTransactionalLogic<FIQLQueryTO> {
39
40 protected final FIQLQueryDataBinder binder;
41
42 protected final FIQLQueryDAO fiqlQueryDAO;
43
44 protected final UserDAO userDAO;
45
46 public FIQLQueryLogic(
47 final FIQLQueryDataBinder binder,
48 final FIQLQueryDAO fiqlQueryDAO,
49 final UserDAO userDAO) {
50
51 this.binder = binder;
52 this.fiqlQueryDAO = fiqlQueryDAO;
53 this.userDAO = userDAO;
54 }
55
56 protected void securityChecks(final String owner) {
57 if (!AuthContextUtils.getUsername().equals(owner)) {
58 throw new DelegatedAdministrationException(SyncopeConstants.ROOT_REALM, AnyTypeKind.USER.name(), owner);
59 }
60 }
61
62 @PreAuthorize("isAuthenticated()")
63 @Transactional(readOnly = true)
64 public FIQLQueryTO read(final String key) {
65 FIQLQuery fiqlQuery = fiqlQueryDAO.find(key);
66 if (fiqlQuery == null) {
67 LOG.error("Could not find fiqlQuery '" + key + "'");
68 throw new NotFoundException(key);
69 }
70
71 securityChecks(fiqlQuery.getOwner().getUsername());
72
73 return binder.getFIQLQueryTO(fiqlQuery);
74 }
75
76 @PreAuthorize("isAuthenticated()")
77 @Transactional(readOnly = true)
78 public List<FIQLQueryTO> list(final String target) {
79 return fiqlQueryDAO.findByOwner(userDAO.findByUsername(AuthContextUtils.getUsername()), target).stream().
80 map(binder::getFIQLQueryTO).collect(Collectors.toList());
81 }
82
83 @PreAuthorize("isAuthenticated()")
84 public FIQLQueryTO create(final FIQLQueryTO fiqlQueryTO) {
85 return binder.getFIQLQueryTO(fiqlQueryDAO.save(binder.create(fiqlQueryTO)));
86 }
87
88 @PreAuthorize("isAuthenticated()")
89 public FIQLQueryTO update(final FIQLQueryTO fiqlQueryTO) {
90 FIQLQuery fiqlQuery = fiqlQueryDAO.find(fiqlQueryTO.getKey());
91 if (fiqlQuery == null) {
92 LOG.error("Could not find fiqlQuery '" + fiqlQueryTO.getKey() + "'");
93 throw new NotFoundException(fiqlQueryTO.getKey());
94 }
95
96 securityChecks(fiqlQuery.getOwner().getUsername());
97
98 return binder.getFIQLQueryTO(fiqlQueryDAO.save(binder.update(fiqlQuery, fiqlQueryTO)));
99 }
100
101 @PreAuthorize("isAuthenticated()")
102 public FIQLQueryTO delete(final String key) {
103 FIQLQuery fiqlQuery = fiqlQueryDAO.find(key);
104 if (fiqlQuery == null) {
105 LOG.error("Could not find fiqlQuery '" + key + "'");
106 throw new NotFoundException(key);
107 }
108
109 securityChecks(fiqlQuery.getOwner().getUsername());
110
111 FIQLQueryTO deleted = binder.getFIQLQueryTO(fiqlQuery);
112 fiqlQueryDAO.delete(key);
113 return deleted;
114 }
115
116 @Override
117 protected FIQLQueryTO resolveReference(final Method method, final Object... args)
118 throws UnresolvedReferenceException {
119
120 String key = null;
121
122 if (ArrayUtils.isNotEmpty(args)) {
123 for (int i = 0; key == null && i < args.length; i++) {
124 if (args[i] instanceof String) {
125 key = (String) args[i];
126 } else if (args[i] instanceof FIQLQueryTO) {
127 key = ((FIQLQueryTO) args[i]).getKey();
128 }
129 }
130 }
131
132 if (key != null) {
133 try {
134 return binder.getFIQLQueryTO(fiqlQueryDAO.find(key));
135 } catch (Throwable ignore) {
136 LOG.debug("Unresolved reference", ignore);
137 throw new UnresolvedReferenceException(ignore);
138 }
139 }
140
141 throw new UnresolvedReferenceException();
142 }
143 }