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.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.concurrent.ConcurrentHashMap;
27 import org.apache.commons.lang3.ArrayUtils;
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.syncope.common.lib.Attr;
30 import org.apache.syncope.common.lib.SyncopeClientException;
31 import org.apache.syncope.common.lib.to.AttributableTO;
32 import org.apache.syncope.common.lib.to.DerSchemaTO;
33 import org.apache.syncope.common.lib.to.PlainSchemaTO;
34 import org.apache.syncope.common.lib.to.SchemaTO;
35 import org.apache.syncope.common.lib.types.AnyTypeKind;
36 import org.apache.syncope.common.lib.types.AttrSchemaType;
37 import org.apache.syncope.common.lib.types.ClientExceptionType;
38 import org.apache.syncope.common.lib.types.IdRepoEntitlement;
39 import org.apache.syncope.common.lib.types.SchemaType;
40 import org.apache.syncope.core.persistence.api.attrvalue.DropdownValueProvider;
41 import org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO;
42 import org.apache.syncope.core.persistence.api.dao.DerSchemaDAO;
43 import org.apache.syncope.core.persistence.api.dao.DuplicateException;
44 import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
45 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
46 import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
47 import org.apache.syncope.core.persistence.api.entity.AnyTypeClass;
48 import org.apache.syncope.core.persistence.api.entity.DerSchema;
49 import org.apache.syncope.core.persistence.api.entity.PlainSchema;
50 import org.apache.syncope.core.persistence.api.entity.Schema;
51 import org.apache.syncope.core.provisioning.api.data.SchemaDataBinder;
52 import org.apache.syncope.core.spring.implementation.ImplementationManager;
53 import org.apache.syncope.core.spring.security.AuthContextUtils;
54 import org.springframework.security.access.prepost.PreAuthorize;
55 import org.springframework.transaction.annotation.Transactional;
56
57 public class SchemaLogic extends AbstractTransactionalLogic<SchemaTO> {
58
59 protected final PlainSchemaDAO plainSchemaDAO;
60
61 protected final DerSchemaDAO derSchemaDAO;
62
63 protected final AnyTypeClassDAO anyTypeClassDAO;
64
65 protected final ImplementationDAO implementationDAO;
66
67 protected final SchemaDataBinder binder;
68
69 protected final Map<String, DropdownValueProvider> perContextDropdownValueProviders = new ConcurrentHashMap<>();
70
71 public SchemaLogic(
72 final PlainSchemaDAO plainSchemaDAO,
73 final DerSchemaDAO derSchemaDAO,
74 final AnyTypeClassDAO anyTypeClassDAO,
75 final ImplementationDAO implementationDAO,
76 final SchemaDataBinder binder) {
77
78 this.plainSchemaDAO = plainSchemaDAO;
79 this.derSchemaDAO = derSchemaDAO;
80 this.anyTypeClassDAO = anyTypeClassDAO;
81 this.implementationDAO = implementationDAO;
82 this.binder = binder;
83 }
84
85 @SuppressWarnings("unchecked")
86 protected <S extends Schema> Optional<S> findById(final SchemaType schemaType, final String name) {
87 Optional<S> result = Optional.empty();
88
89 switch (schemaType) {
90 case DERIVED:
91 result = (Optional<S>) derSchemaDAO.findById(name);
92 break;
93
94 case PLAIN:
95 result = (Optional<S>) plainSchemaDAO.findById(name);
96 break;
97
98 default:
99 }
100
101 return result;
102 }
103
104 @PreAuthorize("hasRole('" + IdRepoEntitlement.SCHEMA_CREATE + "')")
105 @SuppressWarnings("unchecked")
106 public <T extends SchemaTO> T create(final SchemaType schemaType, final T schemaTO) {
107 if (StringUtils.isBlank(schemaTO.getKey())) {
108 SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
109 sce.getElements().add("Schema key");
110 throw sce;
111 }
112
113 if (findById(schemaType, schemaTO.getKey()).isPresent()) {
114 throw new DuplicateException(schemaType + "/" + schemaTO.getKey());
115 }
116
117 T created;
118 switch (schemaType) {
119 case DERIVED:
120 created = (T) binder.getDerSchemaTO(binder.create((DerSchemaTO) schemaTO).getKey());
121 break;
122
123 case PLAIN:
124 default:
125 created = (T) binder.getPlainSchemaTO(binder.create((PlainSchemaTO) schemaTO).getKey());
126 }
127 return created;
128 }
129
130 @PreAuthorize("hasRole('" + IdRepoEntitlement.SCHEMA_DELETE + "')")
131 public void delete(final SchemaType schemaType, final String schemaKey) {
132 findById(schemaType, schemaKey).
133 orElseThrow(() -> new NotFoundException(schemaType + ": " + schemaKey));
134
135 switch (schemaType) {
136 case DERIVED:
137 derSchemaDAO.deleteById(schemaKey);
138 break;
139
140 case PLAIN:
141 default:
142 plainSchemaDAO.deleteById(schemaKey);
143 }
144 }
145
146 @PreAuthorize("isAuthenticated()")
147 @Transactional(readOnly = true)
148 @SuppressWarnings("unchecked")
149 public <T extends SchemaTO> List<T> search(
150 final SchemaType schemaType, final List<String> anyTypeClasses, final String keyword) {
151
152 List<AnyTypeClass> classes = new ArrayList<>(anyTypeClasses.size());
153 anyTypeClasses.remove(AnyTypeKind.USER.name());
154 anyTypeClasses.remove(AnyTypeKind.GROUP.name());
155 anyTypeClasses.forEach(anyTypeClass -> anyTypeClassDAO.findById(anyTypeClass).
156 ifPresentOrElse(
157 classes::add,
158 () -> LOG.warn("Ignoring invalid {}: {}",
159 AnyTypeClass.class.getSimpleName(), anyTypeClass)));
160
161 List<T> result;
162 switch (schemaType) {
163 case DERIVED:
164 List<? extends DerSchema> derSchemas = classes.isEmpty()
165 ? keyword == null
166 ? derSchemaDAO.findAll()
167 : derSchemaDAO.findByIdLike(keyword)
168 : derSchemaDAO.findByAnyTypeClasses(classes);
169 result = derSchemas.stream().map(schema -> (T) binder.getDerSchemaTO(schema.getKey())).
170 toList();
171 break;
172
173 case PLAIN:
174 default:
175 List<? extends PlainSchema> plainSchemas = classes.isEmpty()
176 ? keyword == null
177 ? plainSchemaDAO.findAll()
178 : plainSchemaDAO.findByIdLike(keyword)
179 : plainSchemaDAO.findByAnyTypeClasses(classes);
180 result = plainSchemas.stream().map(schema -> (T) binder.getPlainSchemaTO(schema.getKey())).
181 toList();
182 }
183
184 return result;
185 }
186
187 @PreAuthorize("isAuthenticated()")
188 @SuppressWarnings("unchecked")
189 public <T extends SchemaTO> T read(final SchemaType schemaType, final String schemaKey) {
190 T read;
191 switch (schemaType) {
192 case DERIVED:
193 read = (T) binder.getDerSchemaTO(schemaKey);
194 break;
195
196 case PLAIN:
197 default:
198 read = (T) binder.getPlainSchemaTO(schemaKey);
199 }
200
201 return read;
202 }
203
204 @PreAuthorize("hasRole('" + IdRepoEntitlement.SCHEMA_UPDATE + "')")
205 public <T extends SchemaTO> void update(final SchemaType schemaType, final T schemaTO) {
206 Schema schema = findById(schemaType, schemaTO.getKey()).
207 orElseThrow(() -> new NotFoundException(schemaType + ": " + schemaTO.getKey()));
208
209 switch (schemaType) {
210 case DERIVED ->
211 binder.update((DerSchemaTO) schemaTO, (DerSchema) schema);
212
213 case PLAIN ->
214 binder.update((PlainSchemaTO) schemaTO, (PlainSchema) schema);
215
216 default -> {
217 }
218 }
219 }
220
221 @PreAuthorize("isAuthenticated()")
222 public Attr getDropdownValues(final String key, final AttributableTO attributableTO) {
223 PlainSchema schema = plainSchemaDAO.findById(key).
224 filter(s -> s.getType() == AttrSchemaType.Dropdown).
225 orElseThrow(() -> new NotFoundException(AttrSchemaType.Dropdown.name() + " PlainSchema " + key));
226
227 try {
228 DropdownValueProvider provider = ImplementationManager.build(
229 AuthContextUtils.getDomain(),
230 schema.getDropdownValueProvider(),
231 () -> perContextDropdownValueProviders.get(schema.getDropdownValueProvider().getKey()),
232 instance -> perContextDropdownValueProviders.put(
233 schema.getDropdownValueProvider().getKey(), instance));
234 return new Attr.Builder(schema.getKey()).values(provider.getChoices(attributableTO)).build();
235 } catch (Exception e) {
236 LOG.error("While getting dropdown values for {}", key, e);
237
238 SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidImplementation);
239 sce.getElements().add(e.getMessage());
240 throw sce;
241 }
242 }
243
244 @Override
245 protected SchemaTO resolveReference(final Method method, final Object... args)
246 throws UnresolvedReferenceException {
247
248 String key = null;
249 if (ArrayUtils.isNotEmpty(args)) {
250 for (int i = 0; key == null && i < args.length; i++) {
251 if (args[i] instanceof String string) {
252 key = string;
253 } else if (args[i] instanceof SchemaTO schemaTO) {
254 key = schemaTO.getKey();
255 }
256 }
257 }
258
259 if (key != null) {
260 try {
261 SchemaTO result = null;
262
263 Optional<? extends Schema> schema = plainSchemaDAO.findById(key);
264 if (schema.isEmpty()) {
265 schema = derSchemaDAO.findById(key);
266 if (schema.isPresent()) {
267 result = binder.getDerSchemaTO(key);
268 }
269 } else {
270 result = binder.getPlainSchemaTO(key);
271 }
272
273 return result;
274 } catch (Throwable ignore) {
275 LOG.debug("Unresolved reference", ignore);
276 throw new UnresolvedReferenceException(ignore);
277 }
278 }
279
280 throw new UnresolvedReferenceException();
281 }
282 }