1
2
3
4
5
6 package org.tailormap.api.util;
7
8 import org.springframework.http.HttpStatus;
9 import org.springframework.security.authentication.AnonymousAuthenticationToken;
10 import org.springframework.security.core.Authentication;
11 import org.springframework.security.core.context.SecurityContextHolder;
12 import org.springframework.stereotype.Component;
13 import org.springframework.web.server.ResponseStatusException;
14 import org.tailormap.api.persistence.Application;
15 import org.tailormap.api.persistence.GeoService;
16 import org.tailormap.api.persistence.TMFeatureType;
17 import org.tailormap.api.persistence.json.AppLayerSettings;
18 import org.tailormap.api.persistence.json.AppTreeLayerNode;
19 import org.tailormap.api.persistence.json.GeoServiceLayer;
20 import org.tailormap.api.repository.FeatureSourceRepository;
21
22 @Component
23 public class EditUtil {
24 private final FeatureSourceRepository featureSourceRepository;
25
26 public EditUtil(FeatureSourceRepository featureSourceRepository) {
27 this.featureSourceRepository = featureSourceRepository;
28 }
29
30
31
32
33
34 public void checkEditAuthorisation() throws ResponseStatusException {
35 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
36 boolean isAuthenticated = authentication != null && !(authentication instanceof AnonymousAuthenticationToken);
37 if (!isAuthenticated) {
38 throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Not properly authenticated");
39 }
40 }
41
42
43
44
45
46
47
48
49
50
51
52 public TMFeatureType getEditableFeatureType(
53 Application application, AppTreeLayerNode appTreeLayerNode, GeoService service, GeoServiceLayer layer)
54 throws ResponseStatusException {
55
56 if (null == layer) {
57 throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Cannot find layer " + appTreeLayerNode);
58 }
59
60 AppLayerSettings appLayerSettings = application.getAppLayerSettings(appTreeLayerNode);
61 if (!Boolean.TRUE.equals(appLayerSettings.getEditable())) {
62 throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Layer is not editable");
63 }
64
65 TMFeatureType tmft = service.findFeatureTypeForLayer(layer, featureSourceRepository);
66 if (null == tmft) {
67 throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Layer does not have feature type");
68 }
69 if (!tmft.isWriteable()) {
70 throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Feature type is not writeable");
71 }
72
73 return tmft;
74 }
75 }