From 3a63f948a02da3b22eba06a582edea2ddc8473a4 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sun, 15 Oct 2023 14:25:37 +0530 Subject: [PATCH 1/2] Add admin role when adding shared scopes --- .../apimgt/extension/rest/api/util/ScopeUtils.java | 14 +++++++++++++- .../webapp/publisher/APIPublisherServiceImpl.java | 9 +++++---- .../src/main/resources/dbscripts/cdm/h2.sql | 2 +- .../src/main/resources/dbscripts/cdm/mysql.sql | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/util/ScopeUtils.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/util/ScopeUtils.java index e8f602f992..58cff08553 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/util/ScopeUtils.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/util/ScopeUtils.java @@ -18,6 +18,9 @@ package io.entgra.device.mgt.core.apimgt.extension.rest.api.util; +import java.util.HashSet; +import java.util.Set; + /** * This class represents the scope data. */ @@ -53,7 +56,7 @@ public class ScopeUtils { } public void setRoles(String roles) { - this.roles = roles; + this.roles = removeDuplicatesFromRoleString(roles); } public String getDescription() { @@ -75,4 +78,13 @@ public class ScopeUtils { "}"; return jsonString; } + + private static String removeDuplicatesFromRoleString(String roleString) { + String[] roles = roleString.split(","); + Set roleSet = new HashSet<>(); + for(String role : roles) { + roleSet.add(role.trim()); + } + return String.join(",", roleSet); + } } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java index e76631e0cb..be4def9da6 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java @@ -100,6 +100,7 @@ public class APIPublisherServiceImpl implements APIPublisherService { public static final String SUBSCRIPTION_TO_CURRENT_TENANT = "CURRENT_TENANT"; public static final String API_GLOBAL_VISIBILITY = "PUBLIC"; public static final String API_PRIVATE_VISIBILITY = "PRIVATE"; + private static final String ADMIN_ROLE_KEY = ",admin"; private static final Log log = LogFactory.getLog(APIPublisherServiceImpl.class); @@ -186,7 +187,7 @@ public class APIPublisherServiceImpl implements APIPublisherService { scope.setName(apiScope.getName()); scope.setDescription(apiScope.getDescription()); scope.setKey(apiScope.getKey()); - scope.setRoles(apiScope.getRoles()); + scope.setRoles(apiScope.getRoles() + ADMIN_ROLE_KEY); publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope); } } @@ -259,7 +260,7 @@ public class APIPublisherServiceImpl implements APIPublisherService { scope.setName(apiScope.getName()); scope.setDescription(apiScope.getDescription()); scope.setKey(apiScope.getKey()); - scope.setRoles(apiScope.getRoles()); + scope.setRoles(apiScope.getRoles() + ADMIN_ROLE_KEY); publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope); } @@ -280,7 +281,7 @@ public class APIPublisherServiceImpl implements APIPublisherService { scope.setName(apiScope.getName()); scope.setDescription(apiScope.getDescription()); scope.setKey(apiScope.getKey()); - scope.setRoles(apiScope.getRoles()); + scope.setRoles(apiScope.getRoles() + ADMIN_ROLE_KEY); publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope); } } @@ -460,7 +461,7 @@ public class APIPublisherServiceImpl implements APIPublisherService { scope.setName(scopeMapping.getName()); scope.setDescription(scopeMapping.getName()); scope.setKey(scopeMapping.getKey()); - scope.setRoles(scopeMapping.getDefaultRoles()); + scope.setRoles(scopeMapping.getDefaultRoles() + ADMIN_ROLE_KEY); publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope); } } catch (BadRequestException | UnexpectedResponseException | APIServicesException e) { diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 4d87dbdcca..4f9f9fa5dc 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -594,7 +594,7 @@ CREATE TABLE IF NOT EXISTS DM_METADATA ( METADATA_ID INT AUTO_INCREMENT NOT NULL, DATA_TYPE VARCHAR(16) NOT NULL, METADATA_KEY VARCHAR(128) NOT NULL, - METADATA_VALUE VARCHAR(20000) NOT NULL, + METADATA_VALUE VARCHAR(65535) NOT NULL, TENANT_ID INTEGER NOT NULL, PRIMARY KEY (METADATA_ID), CONSTRAINT METADATA_KEY_TENANT_ID UNIQUE (METADATA_KEY, TENANT_ID) diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 1ea355fb81..2f4bd489f0 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -656,7 +656,7 @@ CREATE TABLE IF NOT EXISTS DM_METADATA ( METADATA_ID INTEGER NOT NULL AUTO_INCREMENT, DATA_TYPE VARCHAR(16) NOT NULL, METADATA_KEY VARCHAR(128) NOT NULL, - METADATA_VALUE VARCHAR(8000) NOT NULL, + METADATA_VALUE VARCHAR(65535) NOT NULL, TENANT_ID INTEGER NOT NULL, PRIMARY KEY (METADATA_ID), UNIQUE KEY METADATA_KEY_TENANT_ID (METADATA_KEY,TENANT_ID) From 4418caa631793c9a7a20035e0f4f090d61576505 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sun, 15 Oct 2023 16:03:06 +0530 Subject: [PATCH 2/2] Update db scripts --- .../src/main/resources/dbscripts/cdm/oracle.sql | 3 ++- .../src/main/resources/dbscripts/cdm/postgresql.sql | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index f19645cdb7..f85e4349e5 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -998,7 +998,8 @@ CREATE TABLE DM_METADATA ( METADATA_ID NUMBER(10) NOT NULL, DATA_TYPE VARCHAR2(16) NOT NULL, METADATA_KEY VARCHAR2(128) NOT NULL, - METADATA_VALUE VARCHAR2(8000) NOT NULL, + -- Can be upgrade to 32767 bytes if the MAX_STRING_SIZE initialization parameter is set to EXTENDED -- + METADATA_VALUE VARCHAR2(4000) NOT NULL, TENANT_ID INTEGER NOT NULL, CONSTRAINT PK_DM_METADATA PRIMARY KEY (METADATA_ID), CONSTRAINT METADATA_KEY_TENANT_ID UNIQUE (METADATA_KEY, TENANT_ID) diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 51361a90d3..2d72a7b423 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -660,7 +660,7 @@ CREATE TABLE IF NOT EXISTS DM_METADATA ( METADATA_ID BIGSERIAL PRIMARY KEY, DATA_TYPE VARCHAR(16) NOT NULL, METADATA_KEY VARCHAR(128) NOT NULL, - METADATA_VALUE VARCHAR(8000) NOT NULL, + METADATA_VALUE VARCHAR(65535) NOT NULL, TENANT_ID INTEGER NOT NULL, CONSTRAINT METADATA_KEY_TENANT_ID UNIQUE(METADATA_KEY, TENANT_ID) );