Commit 42a80efdbb641337f0b94911888168a312d59378

Authored by vzikratyi
1 parent bfc3e75f

Moved OAuth2Template endpoints to separate controller

  1 +/**
  2 + * Copyright © 2016-2020 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.controller;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.http.HttpStatus;
  20 +import org.springframework.security.access.prepost.PreAuthorize;
  21 +import org.springframework.web.bind.annotation.*;
  22 +import org.thingsboard.server.common.data.EntityType;
  23 +import org.thingsboard.server.common.data.audit.ActionType;
  24 +import org.thingsboard.server.common.data.exception.ThingsboardException;
  25 +import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId;
  26 +import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate;
  27 +import org.thingsboard.server.queue.util.TbCoreComponent;
  28 +import org.thingsboard.server.service.security.permission.Operation;
  29 +import org.thingsboard.server.service.security.permission.Resource;
  30 +
  31 +import java.util.List;
  32 +
  33 +@RestController
  34 +@TbCoreComponent
  35 +@RequestMapping("/api/oauth2/config/template")
  36 +@Slf4j
  37 +public class OAuth2ConfigTemplateController extends BaseController {
  38 + private static final String CLIENT_REGISTRATION_TEMPLATE_ID = "clientRegistrationTemplateId";
  39 +
  40 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')")
  41 + @RequestMapping(method = RequestMethod.POST)
  42 + @ResponseStatus(value = HttpStatus.OK)
  43 + public OAuth2ClientRegistrationTemplate saveClientRegistrationTemplate(@RequestBody OAuth2ClientRegistrationTemplate clientRegistrationTemplate) throws ThingsboardException {
  44 + try {
  45 + clientRegistrationTemplate.setTenantId(getCurrentUser().getTenantId());
  46 + checkEntity(clientRegistrationTemplate.getId(), clientRegistrationTemplate, Resource.OAUTH2_CONFIGURATION_TEMPLATE);
  47 + return oAuth2ConfigTemplateService.saveClientRegistrationTemplate(clientRegistrationTemplate);
  48 + } catch (Exception e) {
  49 + throw handleException(e);
  50 + }
  51 + }
  52 +
  53 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')")
  54 + @RequestMapping(value = "/{clientRegistrationTemplateId}", method = RequestMethod.DELETE)
  55 + @ResponseStatus(value = HttpStatus.OK)
  56 + public void deleteClientRegistrationTemplate(@PathVariable(CLIENT_REGISTRATION_TEMPLATE_ID) String strClientRegistrationTemplateId) throws ThingsboardException {
  57 + checkParameter(CLIENT_REGISTRATION_TEMPLATE_ID, strClientRegistrationTemplateId);
  58 + try {
  59 + OAuth2ClientRegistrationTemplateId clientRegistrationTemplateId = new OAuth2ClientRegistrationTemplateId(toUUID(strClientRegistrationTemplateId));
  60 + OAuth2ClientRegistrationTemplate clientRegistrationTemplate = checkOAuth2ClientRegistrationTemplateId(clientRegistrationTemplateId, Operation.DELETE);
  61 + oAuth2ConfigTemplateService.deleteClientRegistrationTemplateById(clientRegistrationTemplateId);
  62 +
  63 + logEntityAction(clientRegistrationTemplateId, clientRegistrationTemplate,
  64 + null,
  65 + ActionType.DELETED, null, strClientRegistrationTemplateId);
  66 +
  67 + } catch (Exception e) {
  68 +
  69 + logEntityAction(emptyId(EntityType.OAUTH2_CLIENT_REGISTRATION_TEMPLATE),
  70 + null,
  71 + null,
  72 + ActionType.DELETED, e, strClientRegistrationTemplateId);
  73 +
  74 + throw handleException(e);
  75 + }
  76 + }
  77 +
  78 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
  79 + @RequestMapping(method = RequestMethod.GET, produces = "application/json")
  80 + @ResponseBody
  81 + public List<OAuth2ClientRegistrationTemplate> getClientRegistrationTemplates() throws ThingsboardException {
  82 + try {
  83 + checkOAuth2ConfigTemplatePermissions(Operation.READ);
  84 + return oAuth2ConfigTemplateService.findAllClientRegistrationTemplates();
  85 + } catch (Exception e) {
  86 + throw handleException(e);
  87 + }
  88 + }
  89 +
  90 + private void checkOAuth2ConfigTemplatePermissions(Operation operation) throws ThingsboardException {
  91 + accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION_TEMPLATE, operation);
  92 + }
  93 +}
@@ -23,7 +23,6 @@ import org.thingsboard.server.common.data.EntityType; @@ -23,7 +23,6 @@ import org.thingsboard.server.common.data.EntityType;
23 import org.thingsboard.server.common.data.audit.ActionType; 23 import org.thingsboard.server.common.data.audit.ActionType;
24 import org.thingsboard.server.common.data.exception.ThingsboardException; 24 import org.thingsboard.server.common.data.exception.ThingsboardException;
25 import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId; 25 import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId;
26 -import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId;  
27 import org.thingsboard.server.common.data.id.TenantId; 26 import org.thingsboard.server.common.data.id.TenantId;
28 import org.thingsboard.server.common.data.oauth2.*; 27 import org.thingsboard.server.common.data.oauth2.*;
29 import org.thingsboard.server.common.data.security.Authority; 28 import org.thingsboard.server.common.data.security.Authority;
@@ -42,7 +41,6 @@ import java.util.stream.Collectors; @@ -42,7 +41,6 @@ import java.util.stream.Collectors;
42 public class OAuth2Controller extends BaseController { 41 public class OAuth2Controller extends BaseController {
43 private static final String CLIENT_REGISTRATION_ID = "clientRegistrationId"; 42 private static final String CLIENT_REGISTRATION_ID = "clientRegistrationId";
44 private static final String DOMAIN = "domain"; 43 private static final String DOMAIN = "domain";
45 - private static final String CLIENT_REGISTRATION_TEMPLATE_ID = "clientRegistrationTemplateId";  
46 44
47 @RequestMapping(value = "/noauth/oauth2Clients", method = RequestMethod.POST) 45 @RequestMapping(value = "/noauth/oauth2Clients", method = RequestMethod.POST)
48 @ResponseBody 46 @ResponseBody
@@ -99,19 +97,6 @@ public class OAuth2Controller extends BaseController { @@ -99,19 +97,6 @@ public class OAuth2Controller extends BaseController {
99 } 97 }
100 } 98 }
101 99
102 - @PreAuthorize("hasAnyAuthority('SYS_ADMIN')")  
103 - @RequestMapping(value = "/oauth2/config/template", method = RequestMethod.POST)  
104 - @ResponseStatus(value = HttpStatus.OK)  
105 - public OAuth2ClientRegistrationTemplate saveClientRegistrationTemplate(@RequestBody OAuth2ClientRegistrationTemplate clientRegistrationTemplate) throws ThingsboardException {  
106 - try {  
107 - clientRegistrationTemplate.setTenantId(getCurrentUser().getTenantId());  
108 - checkEntity(clientRegistrationTemplate.getId(), clientRegistrationTemplate, Resource.OAUTH2_CONFIGURATION_TEMPLATE);  
109 - return oAuth2ConfigTemplateService.saveClientRegistrationTemplate(clientRegistrationTemplate);  
110 - } catch (Exception e) {  
111 - throw handleException(e);  
112 - }  
113 - }  
114 -  
115 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") 100 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
116 @RequestMapping(value = "/oauth2/config/{clientRegistrationId}", method = RequestMethod.DELETE) 101 @RequestMapping(value = "/oauth2/config/{clientRegistrationId}", method = RequestMethod.DELETE)
117 @ResponseStatus(value = HttpStatus.OK) 102 @ResponseStatus(value = HttpStatus.OK)
@@ -160,31 +145,6 @@ public class OAuth2Controller extends BaseController { @@ -160,31 +145,6 @@ public class OAuth2Controller extends BaseController {
160 } 145 }
161 } 146 }
162 147
163 - @PreAuthorize("hasAnyAuthority('SYS_ADMIN')")  
164 - @RequestMapping(value = "/oauth2/config/template/{clientRegistrationTemplateId}", method = RequestMethod.DELETE)  
165 - @ResponseStatus(value = HttpStatus.OK)  
166 - public void deleteClientRegistrationTemplate(@PathVariable(CLIENT_REGISTRATION_TEMPLATE_ID) String strClientRegistrationTemplateId) throws ThingsboardException {  
167 - checkParameter(CLIENT_REGISTRATION_TEMPLATE_ID, strClientRegistrationTemplateId);  
168 - try {  
169 - OAuth2ClientRegistrationTemplateId clientRegistrationTemplateId = new OAuth2ClientRegistrationTemplateId(toUUID(strClientRegistrationTemplateId));  
170 - OAuth2ClientRegistrationTemplate clientRegistrationTemplate = checkOAuth2ClientRegistrationTemplateId(clientRegistrationTemplateId, Operation.DELETE);  
171 - oAuth2ConfigTemplateService.deleteClientRegistrationTemplateById(clientRegistrationTemplateId);  
172 -  
173 - logEntityAction(clientRegistrationTemplateId, clientRegistrationTemplate,  
174 - null,  
175 - ActionType.DELETED, null, strClientRegistrationTemplateId);  
176 -  
177 - } catch (Exception e) {  
178 -  
179 - logEntityAction(emptyId(EntityType.OAUTH2_CLIENT_REGISTRATION_TEMPLATE),  
180 - null,  
181 - null,  
182 - ActionType.DELETED, e, strClientRegistrationTemplateId);  
183 -  
184 - throw handleException(e);  
185 - }  
186 - }  
187 -  
188 @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") 148 @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
189 @RequestMapping(value = "/oauth2/config/isAllowed", method = RequestMethod.GET) 149 @RequestMapping(value = "/oauth2/config/isAllowed", method = RequestMethod.GET)
190 @ResponseBody 150 @ResponseBody
@@ -196,25 +156,7 @@ public class OAuth2Controller extends BaseController { @@ -196,25 +156,7 @@ public class OAuth2Controller extends BaseController {
196 } 156 }
197 } 157 }
198 158
199 -  
200 -  
201 - @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")  
202 - @RequestMapping(value = "/oauth2/config/template", method = RequestMethod.GET, produces = "application/json")  
203 - @ResponseBody  
204 - public List<OAuth2ClientRegistrationTemplate> getClientRegistrationTemplates() throws ThingsboardException {  
205 - try {  
206 - checkOAuth2ConfigTemplatePermissions(Operation.READ);  
207 - return oAuth2ConfigTemplateService.findAllClientRegistrationTemplates();  
208 - } catch (Exception e) {  
209 - throw handleException(e);  
210 - }  
211 - }  
212 -  
213 private void checkOAuth2ConfigPermissions(Operation operation) throws ThingsboardException { 159 private void checkOAuth2ConfigPermissions(Operation operation) throws ThingsboardException {
214 accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION, operation); 160 accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION, operation);
215 } 161 }
216 -  
217 - private void checkOAuth2ConfigTemplatePermissions(Operation operation) throws ThingsboardException {  
218 - accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION_TEMPLATE, operation);  
219 - }  
220 } 162 }