Skip to content

Commit

Permalink
Merge pull request #1002 from Ontotext-AD/GDB-8648-Custom-Roles-User-…
Browse files Browse the repository at this point in the history
…View

GDB-8648 Added custom roles in user view and a way how to parse them
  • Loading branch information
sava-savov-ontotext authored Aug 21, 2023
2 parents 043e934 + dfaf4d6 commit ca98a32
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/i18n/locale-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/locale-fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/js/angular/security/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
22 changes: 19 additions & 3 deletions src/js/angular/security/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -62,6 +63,9 @@ const setGrantedAuthorities = function ($scope) {
}
}
}
if ($scope.customRoles) {
$scope.customRoles.forEach((role) => pushAuthority('CUSTOM_' + role));
}
};

const parseAuthorities = function (authorities) {
Expand All @@ -71,6 +75,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) {
Expand All @@ -81,7 +86,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);
Expand All @@ -92,14 +97,17 @@ 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));
}
}

return {
userType: userType,
userTypeDescription: UserUtils.getUserRoleName(userType),
grantedAuthorities: grantedAuthorities,
repositories: repositories
repositories: repositories,
customRoles: customRoles
};
};

Expand Down Expand Up @@ -187,6 +195,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) {
Expand Down Expand Up @@ -464,6 +473,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',
Expand Down Expand Up @@ -617,6 +631,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'));
Expand Down Expand Up @@ -788,6 +803,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 () {
Expand Down
16 changes: 16 additions & 0 deletions src/js/angular/security/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ <h5>{{'security.admin.may' | translate}}</h5>
</ul>
</script>

<link href="css/lib/ng-tags-input/ng-tags-input.min.css?v=[AIV]{version}[/AIV]" rel="stylesheet"/>

<h1>{{pageTitle}}</h1>

<div id="wb-user" ng-hide="loader">
Expand Down Expand Up @@ -204,6 +206,20 @@ <h3>{{'security.user.role' | translate}}</h3>
</div>
</div>

<div class="card user-custom-roles" id="user-custom-roles">
<div class="card-block">
<h3>{{'security.user.custom_role' | translate}}</h3>
<div class="input-group">
<tags-input class="wb-tags-input" ng-model="customRoles" min-length="2" ng-disabled="hasEditRestrictions()"
use-strings="true"
add-on-space="true"
add-on-comma="true"
on-tag-added="addCustomRole($tag)"
placeholder="{{'security.user.add.custom_role.msg' | translate}}"></tags-input>
</div>
</div>
</div>

<div id="user-repos" class="card user-repositories">
<div class="card-block">
<h3>{{'security.repo.rights' | translate}}</h3>
Expand Down
6 changes: 6 additions & 0 deletions src/js/angular/security/templates/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ <h1>
{{repo}}
</div>
<div ng-show="user.userType !== 'user'">{{'security.unrestricted' | translate}}</div>
<div>
<span ng-repeat="role in user.customRoles">
<span class="tag tag-primary" >{{ role }}</span>
&nbsp;
</span>
</div>
</td>
<td class="date-created"><span>{{user.dateCreated | date: 'yyyy-MM-dd HH:mm:ss'}}</span></td>
<td class="actions-bar">
Expand Down
2 changes: 2 additions & 0 deletions test-cypress/fixtures/locale-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions test/security/controllers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/security/services.spec.js
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down
3 changes: 2 additions & 1 deletion webpack.config.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit ca98a32

Please sign in to comment.