Project

General

Profile

« Previous | Next » 

Revision 61441

merged branch aai_roles_new to trunk

View differences:

RepositoryController.java
3 3
import eu.dnetlib.domain.data.Repository;
4 4
import eu.dnetlib.domain.data.RepositoryInterface;
5 5
import eu.dnetlib.repo.manager.domain.*;
6
import eu.dnetlib.repo.manager.domain.dto.User;
6 7
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
7
import eu.dnetlib.repo.manager.service.RepositoryServiceImpl;
8
import eu.dnetlib.repo.manager.service.RepositoryService;
9
import eu.dnetlib.repo.manager.service.security.AuthorizationService;
10
import eu.dnetlib.repo.manager.utils.JsonUtils;
8 11
import io.swagger.annotations.Api;
9 12
import org.apache.log4j.Logger;
10 13
import org.json.JSONException;
11 14
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
12 15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.http.HttpStatus;
13 17
import org.springframework.http.MediaType;
18
import org.springframework.http.ResponseEntity;
14 19
import org.springframework.security.access.prepost.PostAuthorize;
15 20
import org.springframework.security.access.prepost.PreAuthorize;
16 21
import org.springframework.security.core.Authentication;
17 22
import org.springframework.security.core.context.SecurityContextHolder;
18 23
import org.springframework.web.bind.annotation.*;
19 24

  
25
import javax.ws.rs.core.Response;
20 26
import java.io.IOException;
21 27
import java.util.List;
22 28
import java.util.Map;
23 29

  
24 30
@RestController
25
@RequestMapping(value = "/repository")
26
@Api(description = "Repository API",  tags = {"repository"})
31
@RequestMapping(value = "/repositories")
32
@Api(description = "Repository API", tags = {"repositories"})
27 33
public class RepositoryController {
28 34

  
29
    private static Logger logger = Logger.getLogger(RepositoryController.class);
35
    private static final Logger logger = Logger.getLogger(RepositoryController.class);
30 36

  
37
    private final RepositoryService repositoryService;
38
    private final AuthorizationService authorizationService;
39

  
31 40
    @Autowired
32
    private RepositoryServiceImpl repositoryService;
41
    RepositoryController(RepositoryService repositoryService,
42
                         AuthorizationService authorizationService) {
43
        this.repositoryService = repositoryService;
44
        this.authorizationService = authorizationService;
45
    }
33 46

  
34
    @RequestMapping(value = "/getCountries", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
47
    @RequestMapping(value = "/countries", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
35 48
    @ResponseBody
36 49
    public Country[] getCountries() {
37 50
        return repositoryService.getCountries();
......
42 55
    @ResponseBody
43 56
    public List<RepositorySnippet> getRepositoriesByCountry(@PathVariable("country") String country,
44 57
                                                            @PathVariable("mode") String mode,
45
                                                            @RequestParam(value = "managed",required=false) Boolean managed) throws JSONException, IOException {
58
                                                            @RequestParam(value = "managed", required = false) Boolean managed) throws JSONException, IOException {
46 59
        return repositoryService.getRepositoriesByCountry(country, mode, managed);
47 60
    }
48 61

  
49
    @RequestMapping(value = "/getRepositoriesOfUser/{page}/{size}",method = RequestMethod.GET,
62
    @RequestMapping(value = "/snippets/user", method = RequestMethod.GET,
50 63
            produces = MediaType.APPLICATION_JSON_VALUE)
51 64
    @ResponseBody
52
    @PreAuthorize("hasRole('ROLE_USER')")
53
    public List<RepositorySnippet> getRepositoriesOfUser(
54
                                                  @PathVariable("page") String page,
55
                                                  @PathVariable("size") String size) throws JSONException, IOException {
56
       return repositoryService.getRepositoriesSnippetOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
65
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
66
    public List<RepositorySnippet> getRepositoriesSnippetsOfUser() throws Exception {
67
        return repositoryService.getRepositoriesSnippetsOfUser("0", "100");
57 68
    }
58 69

  
59
    @RequestMapping(value = "/searchRegisteredRepositories/{page}/{size}",method = RequestMethod.GET,
70
    @RequestMapping(value = "/searchRegisteredRepositories/{page}/{size}", method = RequestMethod.GET,
60 71
            produces = MediaType.APPLICATION_JSON_VALUE)
61 72
    @ResponseBody
62
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')")
63
    public List<RepositorySnippet> searchRegisteredRepositories(@RequestParam(name="country", required=false) String country,
64
                                                                @RequestParam(name="typology", required=false) String typology,
65
                                                                @RequestParam(name="englishName", required=false) String englishName,
66
                                                                @RequestParam(name="officialName", required=false) String officialName,
73
    @PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')")
74
    public List<RepositorySnippet> searchRegisteredRepositories(@RequestParam(name = "country", required = false) String country,
75
                                                                @RequestParam(name = "typology", required = false) String typology,
76
                                                                @RequestParam(name = "englishName", required = false) String englishName,
77
                                                                @RequestParam(name = "officialName", required = false) String officialName,
67 78
                                                                @RequestParam("requestSortBy") String requestSortBy,
68 79
                                                                @RequestParam("order") String order,
69 80
                                                                @PathVariable("page") int page,
......
75 86
    @RequestMapping(value = "/getRepositoryById/{id}", method = RequestMethod.GET,
76 87
            produces = MediaType.APPLICATION_JSON_VALUE)
77 88
    @ResponseBody
78
    @PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((returnObject.registeredBy=='null' or returnObject.registeredBy==authentication.userInfo.email) and hasRole('ROLE_USER'))")
79
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException,ResourceNotFoundException {
89
    @PostAuthorize("hasAnyRole('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id) or (returnObject.registeredBy=='null' and hasAuthority('REGISTERED_USER'))")
90
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException, ResourceNotFoundException {
80 91
        Repository repo = repositoryService.getRepositoryById(id);
81 92

  
82
	if (repo != null)
83
        	logger.info("Returning repository " + repo.getId() + " registered by " + repo.getRegisteredBy());
84
	else
85
		logger.info("Requested repository " + id  + " not found");
93
        if (repo != null)
94
            logger.info("Returning repository " + repo.getId() + " registered by " + repo.getRegisteredBy());
95
        else
96
            logger.info("Requested repository " + id + " not found");
86 97
        return repo;
87 98
    }
88 99

  
......
90 101
            produces = MediaType.APPLICATION_JSON_VALUE)
91 102
    @ResponseBody
92 103
    public List<AggregationDetails> getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
93
        return repositoryService.getRepositoryAggregations(id,0,20);
104
        return repositoryService.getRepositoryAggregations(id, 0, 20);
94 105
    }
95 106

  
96 107
    @RequestMapping(value = "/getRepositoryAggregationsByYear/{id}", method = RequestMethod.GET,
......
112 123
    @RequestMapping(value = "/getRepositoryInterface/{id}", method = RequestMethod.GET,
113 124
            produces = MediaType.APPLICATION_JSON_VALUE)
114 125
    @ResponseBody
115
    @PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((@repositoryService.getRepositoryById(#id).registeredBy==authentication.userInfo.email or @repositoryService.getRepositoryById(#id).registeredBy=='null' ) and hasRole('ROLE_USER'))")
126
    @PostAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id) or (@repositoryService.getRepositoryById(#id).registeredBy=='null' and hasAuthority('REGISTERED_USER'))")
116 127
    public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException {
117 128
        return repositoryService.getRepositoryInterface(id);
118 129
    }
......
120 131
    @RequestMapping(value = "/addRepository", method = RequestMethod.POST,
121 132
            consumes = MediaType.APPLICATION_JSON_VALUE)
122 133
    @ResponseBody
123
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((#repository.registeredBy==authentication.userInfo.email or returnObject.registeredBy=='null') and hasRole('ROLE_USER'))")
134
//    @PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or (hasAuthority(@authorizationService.convertRepoIdToRoleId(#repository.id)) or hasAuthority(@authorizationService.convertRepoIdToRoleId(returnObject.id)))")
135
    @PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or hasAuthority('REGISTERED_USER')")
124 136
    public Repository addRepository(@RequestParam("datatype") String datatype,
125 137
                                    @RequestBody Repository repository) throws Exception {
126 138

  
......
130 142
    @RequestMapping(value = "/getDnetCountries", method = RequestMethod.GET,
131 143
            produces = MediaType.APPLICATION_JSON_VALUE)
132 144
    @ResponseBody
133
    public List<String> getDnetCountries(){
145
    public List<String> getDnetCountries() {
134 146
        return repositoryService.getDnetCountries();
135 147
    }
136 148

  
137 149
    @RequestMapping(value = "/getTypologies", method = RequestMethod.GET,
138 150
            produces = MediaType.APPLICATION_JSON_VALUE)
139 151
    @ResponseBody
140
    public List<String> getTypologies(){
152
    public List<String> getTypologies() {
141 153
        return repositoryService.getTypologies();
142 154
    }
143 155

  
144 156
    @RequestMapping(value = "/getTimezones", method = RequestMethod.GET,
145 157
            produces = MediaType.APPLICATION_JSON_VALUE)
146 158
    @ResponseBody
147
    public List<Timezone> getTimezones(){
159
    public List<Timezone> getTimezones() {
148 160
        return repositoryService.getTimezones();
149 161
    }
150 162

  
151 163
    @RequestMapping(value = "/updateRepository", method = RequestMethod.POST,
152 164
            consumes = MediaType.APPLICATION_JSON_VALUE)
153 165
    @ResponseBody
154
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((#repository.registeredBy==authentication.userInfo.email or #repository.registeredBy=='null') and hasRole('ROLE_USER'))")
155
    public Repository updateRepository(@RequestBody Repository repository,Authentication authentication) throws Exception {
166
    @PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#repository.id)")
167
    public Repository updateRepository(@RequestBody Repository repository, Authentication authentication) throws Exception {
156 168
        return repositoryService.updateRepository(repository, authentication);
157 169
    }
158 170

  
159 171
    @RequestMapping(value = "/deleteInterface/", method = RequestMethod.DELETE)
160
    @PreAuthorize("hasRole('ROLE_USER') and #registeredBy == authentication.userInfo.email")
161
    public void deleteRepositoryInterface(@RequestParam("id") String id ,
162
                                          @RequestParam("registeredBy") String registeredBy){
172
    @PreAuthorize("@authorizationService.isMemberOf(#id)")
173
    public void deleteRepositoryInterface(@RequestParam("id") String id,
174
                                          @RequestParam("registeredBy") String registeredBy) {
163 175
        repositoryService.deleteRepositoryInterface(id, registeredBy);
164 176
    }
165 177

  
166 178
    @RequestMapping(value = "/addInterface", method = RequestMethod.POST,
167 179
            consumes = MediaType.APPLICATION_JSON_VALUE)
168 180
    @ResponseBody
169
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((@repositoryService.getRepositoryById(#repoId).registeredBy==authentication.userInfo.email or @repositoryService.getRepositoryById(#repoId).registeredBy=='null') and hasRole('ROLE_USER'))")
181
    @PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id)")
170 182
    public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype,
171
                                                      @RequestParam("repoId") String repoId,
183
                                                      @RequestParam("id") String id,
172 184
                                                      @RequestParam("registeredBy") String registeredBy,
173 185
                                                      @RequestParam(value = "comment", required = false) String comment,
174 186
                                                      @RequestBody RepositoryInterface repositoryInterface) throws Exception {
175
        return repositoryService.addRepositoryInterface(datatype, repoId, registeredBy, comment, repositoryInterface);
187
        return repositoryService.addRepositoryInterface(datatype, id, registeredBy, comment, repositoryInterface);
176 188
    }
177 189

  
178
    @RequestMapping(value = "/getUrlsOfUserRepos/{page}/{size}/",method = RequestMethod.GET,
190
    @RequestMapping(value = "/getUrlsOfUserRepos/{page}/{size}/", method = RequestMethod.GET,
179 191
            produces = MediaType.APPLICATION_JSON_VALUE)
180 192
    @ResponseBody
181
    @PreAuthorize("hasRole('ROLE_USER')")
182
    public List<String> getUrlsOfUserRepos(
183
                                           @PathVariable("page") String page,
184
                                           @PathVariable("size") String size) throws JSONException {
193
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
194
    public List<String> getUrlsOfUserRepos(@PathVariable("page") String page, @PathVariable("size") String size) {
185 195
        return repositoryService.getUrlsOfUserRepos(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
186 196
    }
187 197

  
188
    @RequestMapping(value = "/getDatasourceVocabularies/{mode}",method = RequestMethod.GET,
198
    @RequestMapping(value = "/getDatasourceVocabularies/{mode}", method = RequestMethod.GET,
189 199
            produces = MediaType.APPLICATION_JSON_VALUE)
190 200
    @ResponseBody
191 201
    public List<String> getDatasourceVocabularies(@PathVariable("mode") String mode) {
192
       return repositoryService.getDatasourceVocabularies(mode);
202
        return repositoryService.getDatasourceVocabularies(mode);
193 203
    }
194 204

  
195
    @RequestMapping(value = "/getCompatibilityClasses/{mode}",method = RequestMethod.GET,
205
    @RequestMapping(value = "/getCompatibilityClasses/{mode}", method = RequestMethod.GET,
196 206
            produces = MediaType.APPLICATION_JSON_VALUE)
197 207
    @ResponseBody
198
    public Map<String, String> getCompatibilityClasses(@PathVariable("mode") String mode)  {
208
    public Map<String, String> getCompatibilityClasses(@PathVariable("mode") String mode) {
199 209

  
200 210
        return repositoryService.getCompatibilityClasses(mode);
201 211
    }
202 212

  
203
    @RequestMapping(value = "/getDatasourceClasses/{mode}",method = RequestMethod.GET,
213
    @RequestMapping(value = "/getDatasourceClasses/{mode}", method = RequestMethod.GET,
204 214
            produces = MediaType.APPLICATION_JSON_VALUE)
205 215
    @ResponseBody
206
    public Map<String, String> getDatasourceClasses(@PathVariable("mode") String mode)  {
207
       return repositoryService.getDatasourceClasses(mode);
216
    public Map<String, String> getDatasourceClasses(@PathVariable("mode") String mode) {
217
        return repositoryService.getDatasourceClasses(mode);
208 218
    }
209 219

  
210
    @RequestMapping(value = "/getMetricsInfoForRepository/{repoId}",method = RequestMethod.GET,
220
    @RequestMapping(value = "/getMetricsInfoForRepository/{id}", method = RequestMethod.GET,
211 221
            produces = MediaType.APPLICATION_JSON_VALUE)
212 222
    @ResponseBody
213
    public MetricsInfo getMetricsInfoForRepository(@PathVariable("repoId")  String repoId) throws RepositoryServiceException {
214
        return repositoryService.getMetricsInfoForRepository(repoId);
223
    public MetricsInfo getMetricsInfoForRepository(@PathVariable("id") String id) throws RepositoryServiceException {
224
        return repositoryService.getMetricsInfoForRepository(id);
215 225
    }
216 226

  
217
    @RequestMapping(value = "/getListLatestUpdate/{mode}",method = RequestMethod.GET,
227
    @RequestMapping(value = "/getListLatestUpdate/{mode}", method = RequestMethod.GET,
218 228
            produces = MediaType.APPLICATION_JSON_VALUE)
219 229
    @ResponseBody
220
    public Map<String, String> getListLatestUpdate(@PathVariable("mode")  String mode) throws JSONException {
230
    public Map<String, String> getListLatestUpdate(@PathVariable("mode") String mode) throws JSONException {
221 231
        return repositoryService.getListLatestUpdate(mode);
222 232
    }
223 233

  
224 234
    @RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST,
225 235
            consumes = MediaType.APPLICATION_JSON_VALUE)
226 236
    @ResponseBody
227
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((@repositoryService.getRepositoryById(#repoId).registeredBy==authentication.userInfo.email or @repositoryService.getRepositoryById(#repoId).registeredBy=='null') and hasRole('ROLE_USER'))")
228
    public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId,
237
    @PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id)")
238
    public RepositoryInterface updateRepositoryInterface(@RequestParam("id") String id,
229 239
                                                         @RequestParam("registeredBy") String registeredBy,
230 240
                                                         @RequestParam(value = "comment", required = false) String comment,
231 241
                                                         @RequestBody RepositoryInterface repositoryInterface) throws Exception {
232
        return repositoryService.updateRepositoryInterface(repoId, registeredBy, comment, repositoryInterface);
242
        return repositoryService.updateRepositoryInterface(id, registeredBy, comment, repositoryInterface);
233 243
    }
244

  
245

  
246
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
247
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
248

  
249
    /**
250
     * Get all the admins of the repository
251
     */
252
    @RequestMapping(method = RequestMethod.GET, path = "{id}/admins")
253
    @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id)")
254
    public ResponseEntity<List<User>> getAdminsOfARepo(@PathVariable("id") String id) {
255
        return new ResponseEntity<>(authorizationService.getAdminsOfRepo(id), HttpStatus.OK);
256
    }
257

  
258
    /**
259
     * Subscribe to repo by email
260
     */
261
    @RequestMapping(method = RequestMethod.POST, path = "{id}/admins")
262
    @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id)")
263
    public Response subscribeByEmail(@PathVariable("id") String id, @RequestBody String email) throws ResourceNotFoundException {
264
        authorizationService.addAdmin(id, email);
265
        return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(javax.ws.rs.core.MediaType.APPLICATION_JSON).build();
266
    }
267

  
268
    /**
269
     * Unsubscribe from repo by email
270
     */
271
    @RequestMapping(method = RequestMethod.DELETE, path = "{id}/admins/{email:.+}")
272
    @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id)")
273
    public ResponseEntity<Void> unsubscribeByEmail(@PathVariable("id") String id, @PathVariable("email") String email) throws ResourceNotFoundException {
274
        authorizationService.removeAdmin(id, email);
275
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
276
    }
234 277
}

Also available in: Unified diff