View Javadoc
1   /*
2    * Copyright (C) 2025 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
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     * Check if the current user is authenticated, throws exception if not. As we do not have editing authorisation any
32     * known, authenticated user can edit.
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     * Get editable feature type, throws exception if not found or not editable. Will throw a
44     * {@link ResponseStatusException} if the layer does not have an editable featuretype.
45     *
46     * @param application the application that has the editable layer
47     * @param appTreeLayerNode the layer to edit
48     * @param service the service that has the layer
49     * @param layer the layer to edit
50     * @return the editable feature type
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  }