Commit 14939c27e61a17480cb46688233cd695a55aa492
1 parent
edcaebe9
Added 'deleteByDomain' for OAuth2Service
Showing
7 changed files
with
60 additions
and
1 deletions
... | ... | @@ -47,6 +47,7 @@ import java.util.List; |
47 | 47 | @Slf4j |
48 | 48 | public class OAuth2Controller extends BaseController { |
49 | 49 | private static final String CLIENT_REGISTRATION_ID = "clientRegistrationId"; |
50 | + private static final String DOMAIN = "domain"; | |
50 | 51 | private static final String CLIENT_REGISTRATION_TEMPLATE_ID = "clientRegistrationTemplateId"; |
51 | 52 | |
52 | 53 | @RequestMapping(value = "/noauth/oauth2Clients", method = RequestMethod.POST) |
... | ... | @@ -131,6 +132,29 @@ public class OAuth2Controller extends BaseController { |
131 | 132 | } |
132 | 133 | } |
133 | 134 | |
135 | + | |
136 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") | |
137 | + @RequestMapping(value = "/oauth2/config/domain/{domain}", method = RequestMethod.DELETE) | |
138 | + @ResponseStatus(value = HttpStatus.OK) | |
139 | + public void deleteClientRegistrationForDomain(@PathVariable(DOMAIN) String domain) throws ThingsboardException { | |
140 | + checkParameter(DOMAIN, domain); | |
141 | + try { | |
142 | + oAuth2Service.deleteClientRegistrationsByDomain(getCurrentUser().getTenantId(), domain); | |
143 | + | |
144 | + logEntityAction(emptyId(EntityType.OAUTH2_CLIENT_REGISTRATION), null, | |
145 | + null, | |
146 | + ActionType.DELETED, null, domain); | |
147 | + | |
148 | + } catch (Exception e) { | |
149 | + logEntityAction(emptyId(EntityType.OAUTH2_CLIENT_REGISTRATION), | |
150 | + null, | |
151 | + null, | |
152 | + ActionType.DELETED, e, domain); | |
153 | + | |
154 | + throw handleException(e); | |
155 | + } | |
156 | + } | |
157 | + | |
134 | 158 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") |
135 | 159 | @RequestMapping(value = "/oauth2/config/template/{clientRegistrationTemplateId}", method = RequestMethod.DELETE) |
136 | 160 | @ResponseStatus(value = HttpStatus.OK) | ... | ... |
... | ... | @@ -38,5 +38,7 @@ public interface OAuth2Service { |
38 | 38 | |
39 | 39 | void deleteClientRegistrationById(TenantId tenantId, OAuth2ClientRegistrationId id); |
40 | 40 | |
41 | + void deleteClientRegistrationsByDomain(TenantId tenantId, String domain); | |
42 | + | |
41 | 43 | boolean isOAuth2ClientRegistrationAllowed(TenantId tenantId); |
42 | 44 | } | ... | ... |
... | ... | @@ -28,5 +28,7 @@ public interface OAuth2ClientRegistrationDao extends Dao<OAuth2ClientRegistratio |
28 | 28 | |
29 | 29 | List<OAuth2ClientRegistration> findByDomainName(String domainName); |
30 | 30 | |
31 | + int removeByTenantIdAndDomainName(UUID tenantId, String domainName); | |
32 | + | |
31 | 33 | int removeByTenantId(UUID tenantId); |
32 | 34 | } | ... | ... |
... | ... | @@ -100,12 +100,22 @@ public class OAuth2ServiceImpl extends AbstractEntityService implements OAuth2Se |
100 | 100 | |
101 | 101 | @Override |
102 | 102 | public void deleteClientRegistrationById(TenantId tenantId, OAuth2ClientRegistrationId id) { |
103 | - log.trace("Executing deleteClientRegistrationById [{}]", id); | |
103 | + log.trace("Executing deleteClientRegistrationById [{}], [{}]", tenantId, id); | |
104 | + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | |
104 | 105 | validateId(id, INCORRECT_CLIENT_REGISTRATION_ID + id); |
105 | 106 | clientRegistrationDao.removeById(tenantId, id.getId()); |
106 | 107 | } |
107 | 108 | |
108 | 109 | @Override |
110 | + @Transactional | |
111 | + public void deleteClientRegistrationsByDomain(TenantId tenantId, String domain) { | |
112 | + log.trace("Executing deleteClientRegistrationsByDomain [{}], [{}]", tenantId, domain); | |
113 | + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | |
114 | + validateString(domain, INCORRECT_DOMAIN_NAME + domain); | |
115 | + clientRegistrationDao.removeByTenantIdAndDomainName(tenantId.getId(), domain); | |
116 | + } | |
117 | + | |
118 | + @Override | |
109 | 119 | public boolean isOAuth2ClientRegistrationAllowed(TenantId tenantId) { |
110 | 120 | log.trace("Executing isOAuth2ClientRegistrationAllowed [{}]", tenantId); |
111 | 121 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | ... | ... |
... | ... | @@ -68,6 +68,11 @@ public class JpaOAuth2ClientRegistrationDao extends JpaAbstractDao<OAuth2ClientR |
68 | 68 | } |
69 | 69 | |
70 | 70 | @Override |
71 | + public int removeByTenantIdAndDomainName(UUID tenantId, String domainName) { | |
72 | + return repository.deleteByTenantIdAndDomainName(tenantId, domainName); | |
73 | + } | |
74 | + | |
75 | + @Override | |
71 | 76 | public int removeByTenantId(UUID tenantId) { |
72 | 77 | return repository.deleteByTenantId(tenantId); |
73 | 78 | } | ... | ... |
... | ... | @@ -26,5 +26,7 @@ public interface OAuth2ClientRegistrationRepository extends CrudRepository<OAuth |
26 | 26 | |
27 | 27 | List<OAuth2ClientRegistrationEntity> findAllByDomainName(String domainName); |
28 | 28 | |
29 | + int deleteByTenantIdAndDomainName(UUID tenantId, String domainName); | |
30 | + | |
29 | 31 | int deleteByTenantId(UUID tenantId); |
30 | 32 | } | ... | ... |
... | ... | @@ -215,6 +215,20 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
215 | 215 | Assert.assertEquals(0, oAuth2Service.findClientRegistrationsByTenantId(tenantId).size()); |
216 | 216 | } |
217 | 217 | |
218 | + @Test | |
219 | + public void testDeleteTenantDomainOAuth2ClientRegistrations() { | |
220 | + oAuth2Service.saveClientRegistration(validClientRegistration(tenantId, "domain1")); | |
221 | + oAuth2Service.saveClientRegistration(validClientRegistration(tenantId, "domain1")); | |
222 | + oAuth2Service.saveClientRegistration(validClientRegistration(tenantId, "domain2")); | |
223 | + oAuth2Service.saveClientRegistration(validClientRegistration(TenantId.SYS_TENANT_ID, "domain2")); | |
224 | + Assert.assertEquals(4, oAuth2Service.findAllClientRegistrations().size()); | |
225 | + Assert.assertEquals(3, oAuth2Service.findClientRegistrationsByTenantId(tenantId).size()); | |
226 | + | |
227 | + oAuth2Service.deleteClientRegistrationsByDomain(tenantId, "domain1"); | |
228 | + Assert.assertEquals(2, oAuth2Service.findAllClientRegistrations().size()); | |
229 | + Assert.assertEquals(1, oAuth2Service.findClientRegistrationsByTenantId(tenantId).size()); | |
230 | + } | |
231 | + | |
218 | 232 | private void updateTenantAllowOAuth2Setting(Boolean allowOAuth2) throws IOException { |
219 | 233 | Tenant tenant = tenantService.findTenantById(tenantId); |
220 | 234 | if (allowOAuth2 == null) { | ... | ... |