From dcf901adb5f4bc43ef1b51f33e08fc4f8b15ca08 Mon Sep 17 00:00:00 2001 From: sava Date: Mon, 21 Aug 2023 10:32:35 +0300 Subject: [PATCH 1/3] GDB-8648 Added custom roles in user view and a way how to parse them --- src/js/angular/security/app.js | 1 + src/js/angular/security/controllers.js | 20 +++++++++++++++++--- src/js/angular/security/templates/user.html | 16 ++++++++++++++++ src/js/angular/security/templates/users.html | 6 ++++++ webpack.config.common.js | 3 ++- 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/js/angular/security/app.js b/src/js/angular/security/app.js index 95ba18c43..54e78d8a6 100644 --- a/src/js/angular/security/app.js +++ b/src/js/angular/security/app.js @@ -2,6 +2,7 @@ import 'angular/core/services'; import 'angular/core/directives'; import 'angular/security/controllers'; import 'angular/core/services/jwt-auth.service'; +import 'ng-tags-input/build/ng-tags-input.min'; const modules = [ 'toastr', diff --git a/src/js/angular/security/controllers.js b/src/js/angular/security/controllers.js index b5be6c98c..4cb149b1e 100644 --- a/src/js/angular/security/controllers.js +++ b/src/js/angular/security/controllers.js @@ -16,7 +16,8 @@ const modules = [ 'graphdb.framework.core.services.jwtauth', 'graphdb.framework.core.services.openIDService', 'graphdb.framework.rest.security.service', - 'toastr' + 'toastr', + 'ngTagsInput' ]; const createUniqueKey = function (repository) { @@ -62,6 +63,7 @@ const setGrantedAuthorities = function ($scope) { } } } + $scope.customRoles.forEach((role) => pushAuthority('CUSTOM_' + role)); }; const parseAuthorities = function (authorities) { @@ -71,6 +73,7 @@ const parseAuthorities = function (authorities) { [WRITE_REPO]: {} }; const repositories = {}; + const customRoles = []; for (let i = 0; i < authorities.length; i++) { const role = authorities[i]; if (role === UserRole.ROLE_ADMIN) { @@ -81,7 +84,7 @@ const parseAuthorities = function (authorities) { } } else if (role === UserRole.ROLE_USER) { userType = UserType.USER; - } else if (role.indexOf('ROLE_') !== 0) { + } else if (role.indexOf('READ_REPO_') === 0 || role.indexOf('WRITE_REPO_') === 0) { const index = role.indexOf('_', role.indexOf('_') + 1); const op = role.substr(0, index); const repo = role.substr(index + 1); @@ -92,6 +95,8 @@ const parseAuthorities = function (authorities) { } else if (op === WRITE_REPO) { repositories[repo].write = true; } + } else if (role.indexOf('CUSTOM_') === 0) { + customRoles.push(role.substr('CUSTOM_'.length)); } } @@ -99,7 +104,8 @@ const parseAuthorities = function (authorities) { userType: userType, userTypeDescription: UserUtils.getUserRoleName(userType), grantedAuthorities: grantedAuthorities, - repositories: repositories + repositories: repositories, + customRoles: customRoles }; }; @@ -187,6 +193,7 @@ securityCtrl.controller('UsersCtrl', ['$scope', '$uibModal', 'toastr', '$window' $scope.users[i].userType = pa.userType; $scope.users[i].userTypeDescription = pa.userTypeDescription; $scope.users[i].repositories = pa.repositories; + $scope.users[i].customRoles = pa.customRoles; } $scope.loader = false; }).error(function (data) { @@ -464,6 +471,11 @@ securityCtrl.controller('CommonUserCtrl', ['$rootScope', '$scope', '$http', 'toa return $scope.user && !$scope.user.appSettings.DEFAULT_INFERENCE; }; + + $scope.addCustomRole = function (role) { + role.text = role.text.toUpperCase(); + return role; + }; }]); securityCtrl.controller('AddUserCtrl', ['$scope', '$http', 'toastr', '$window', '$timeout', '$location', '$jwtAuth', '$controller', 'SecurityRestService', 'ModalService', '$translate', @@ -617,6 +629,7 @@ securityCtrl.controller('EditUserCtrl', ['$scope', '$http', 'toastr', '$window', const pa = parseAuthorities(data.grantedAuthorities); $scope.userType = pa.userType; $scope.grantedAuthorities = pa.grantedAuthorities; + $scope.customRoles = pa.customRoles; }).error(function (data) { const msg = getError(data); toastr.error(msg, $translate.instant('common.error')); @@ -788,6 +801,7 @@ securityCtrl.controller('ChangeUserPasswordSettingsCtrl', ['$scope', 'toastr', ' const pa = parseAuthorities(scope.userData.authorities); $scope.userType = pa.userType; $scope.grantedAuthorities = pa.grantedAuthorities; + $scope.customRoles = pa.customRoles; }; $scope.submit = function () { diff --git a/src/js/angular/security/templates/user.html b/src/js/angular/security/templates/user.html index e612701b7..102ca87d2 100644 --- a/src/js/angular/security/templates/user.html +++ b/src/js/angular/security/templates/user.html @@ -31,6 +31,8 @@
{{'security.admin.may' | translate}}
+ +

{{pageTitle}}

@@ -204,6 +206,20 @@

{{'security.user.role' | translate}}

+
+
+

Custom roles

+
+ +
+
+
+

{{'security.repo.rights' | translate}}

diff --git a/src/js/angular/security/templates/users.html b/src/js/angular/security/templates/users.html index 72e69f166..9d85d4c7d 100644 --- a/src/js/angular/security/templates/users.html +++ b/src/js/angular/security/templates/users.html @@ -62,6 +62,12 @@

{{repo}}

{{'security.unrestricted' | translate}}
+
+ + {{ role }} +   + +
{{user.dateCreated | date: 'yyyy-MM-dd HH:mm:ss'}} diff --git a/webpack.config.common.js b/webpack.config.common.js index 48d10cedd..8e69a2faa 100644 --- a/webpack.config.common.js +++ b/webpack.config.common.js @@ -151,7 +151,8 @@ module.exports = { }, { from: 'src/js/angular/security/templates', - to: 'js/angular/security/templates' + to: 'js/angular/security/templates', + transform: replaceVersion }, { from: 'src/js/angular/settings/modal', From 7427ca1501ed434b94b2555542b4fe75674f819d Mon Sep 17 00:00:00 2001 From: sava Date: Mon, 21 Aug 2023 11:18:12 +0300 Subject: [PATCH 2/3] GDB-8648 Added check for undefined of $scope.customRoles and also test that if value present is properly parsed --- src/js/angular/security/controllers.js | 4 +++- test/security/controllers.spec.js | 7 +++++-- test/security/services.spec.js | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/js/angular/security/controllers.js b/src/js/angular/security/controllers.js index 4cb149b1e..4b18be399 100644 --- a/src/js/angular/security/controllers.js +++ b/src/js/angular/security/controllers.js @@ -63,7 +63,9 @@ const setGrantedAuthorities = function ($scope) { } } } - $scope.customRoles.forEach((role) => pushAuthority('CUSTOM_' + role)); + if ($scope.customRoles) { + $scope.customRoles.forEach((role) => pushAuthority('CUSTOM_' + role)); + } }; const parseAuthorities = function (authorities) { diff --git a/test/security/controllers.spec.js b/test/security/controllers.spec.js index 9a02439d5..7a6061200 100644 --- a/test/security/controllers.spec.js +++ b/test/security/controllers.spec.js @@ -399,18 +399,21 @@ describe('==> Controllers tests', function () { $scope.userType = 'user'; $scope.grantedAuthorities = {'READ_REPO': {}, 'WRITE_REPO': {}}; + $scope.customRoles = ['ROLE1']; $scope.setGrantedAuthorities(); - expect($scope.user.grantedAuthorities).toEqual(['ROLE_USER']); + expect($scope.user.grantedAuthorities).toEqual(['ROLE_USER', 'CUSTOM_ROLE1']); expect($scope.repositoryCheckError).toEqual(true); $scope.userType = 'user'; $scope.grantedAuthorities = {'READ_REPO': {'myrepo': true}, 'WRITE_REPO': {}}; + $scope.customRoles = ['ROLE2']; $scope.setGrantedAuthorities(); - expect($scope.user.grantedAuthorities).toEqual(['ROLE_USER', 'READ_REPO_myrepo']); + expect($scope.user.grantedAuthorities).toEqual(['ROLE_USER', 'READ_REPO_myrepo', 'CUSTOM_ROLE2']); expect($scope.repositoryCheckError).toEqual(false); $scope.userType = 'user'; $scope.grantedAuthorities = {'READ_REPO': {}, 'WRITE_REPO': {'myrepo': true}}; + $scope.customRoles = null; $scope.setGrantedAuthorities(); expect($scope.user.grantedAuthorities).toEqual(['ROLE_USER', 'WRITE_REPO_myrepo', 'READ_REPO_myrepo']); expect($scope.repositoryCheckError).toEqual(false); diff --git a/test/security/services.spec.js b/test/security/services.spec.js index 63a5174dd..7f61770e4 100644 --- a/test/security/services.spec.js +++ b/test/security/services.spec.js @@ -1,5 +1,6 @@ import 'angular/core/interceptors/unauthorized.interceptor'; import 'angular/core/services/jwt-auth.service'; +import 'ng-tags-input/build/ng-tags-input.min'; beforeEach(angular.mock.module('graphdb.framework.core.interceptors.unauthorized', function($provide) { $provide.constant("productInfo", { From dfaf4d63ed397a56de69777ec6de4207a2cc4d68 Mon Sep 17 00:00:00 2001 From: sava Date: Mon, 21 Aug 2023 12:07:27 +0300 Subject: [PATCH 3/3] GDB-8648 Applied translation --- src/i18n/locale-en.json | 2 ++ src/i18n/locale-fr.json | 2 ++ src/js/angular/security/templates/user.html | 4 ++-- test-cypress/fixtures/locale-en.json | 2 ++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/i18n/locale-en.json b/src/i18n/locale-en.json index f24bd236c..bc9f672f5 100644 --- a/src/i18n/locale-en.json +++ b/src/i18n/locale-en.json @@ -428,6 +428,8 @@ "security.workbench.settings.theme.onto-original-theme": "Ontotext original theme", "security.workbench.settings.theme.validation.missing-fields": "Color theme validation error. Check browser log for more details!", "security.user.role": "User role", + "security.user.custom_role": "Custom roles", + "security.user.add.custom_role.msg": "Add custom role...", "security.user.label": "User", "security.repo.manager.label": "Repository manager", "security.admin.label": "Administrator", diff --git a/src/i18n/locale-fr.json b/src/i18n/locale-fr.json index 1502ce0c3..23505b941 100644 --- a/src/i18n/locale-fr.json +++ b/src/i18n/locale-fr.json @@ -430,6 +430,8 @@ "security.workbench.settings.theme.onto-original-theme": "Ontotext thème original", "security.workbench.settings.theme.validation.missing-fields": "Erreur de validation du thème de couleur. Consultez le journal du navigateur pour plus de détails!", "security.user.role": "Rôle de l'utilisateur", + "security.user.custom_role": "Rôles personnalisés", + "security.user.add.custom_role.msg": "Ajouter un rôle personnalisé...", "security.user.label": "Utilisateur", "security.repo.manager.label": "Gestionnaire du dépôt", "security.admin.label": "Administrateur", diff --git a/src/js/angular/security/templates/user.html b/src/js/angular/security/templates/user.html index 102ca87d2..ece1f808c 100644 --- a/src/js/angular/security/templates/user.html +++ b/src/js/angular/security/templates/user.html @@ -208,14 +208,14 @@

{{'security.user.role' | translate}}

-

Custom roles

+

{{'security.user.custom_role' | translate}}

+ placeholder="{{'security.user.add.custom_role.msg' | translate}}">
diff --git a/test-cypress/fixtures/locale-en.json b/test-cypress/fixtures/locale-en.json index 653d2a144..d4640ed83 100644 --- a/test-cypress/fixtures/locale-en.json +++ b/test-cypress/fixtures/locale-en.json @@ -427,6 +427,8 @@ "security.workbench.settings.theme.onto-original-theme": "Ontotext original theme", "security.workbench.settings.theme.validation.missing-fields": "Color theme validation error. Check browser log for more details!", "security.user.role": "User role", + "security.user.custom_role": "Custom roles", + "security.user.add.custom_role.msg": "Add custom role...", "security.user.label": "User", "security.repo.manager.label": "Repository manager", "security.admin.label": "Administrator",