From 1d84d8c7441f06a862bd91ce3ef9eac5bf180d28 Mon Sep 17 00:00:00 2001 From: Vadim Basko Date: Sun, 24 Nov 2019 04:04:17 +0400 Subject: [PATCH] Provide roles types of current user #78 (#79) --- .../controllers/PermissionsController.java | 6 +++++ .../service/PermissionsControllerManager.java | 22 +++++++++++++++ .../api/service/filter/data/RoleInfo.java | 26 ++++++++++++++++++ .../api/service/filter/data/RolesInfo.java | 27 +++++++++++++++++++ .../addon/restapi/rest-dispatcher-spring.xml | 1 + 5 files changed, 82 insertions(+) create mode 100644 modules/rest-api/src/com/haulmont/addon/restapi/api/service/filter/data/RoleInfo.java create mode 100644 modules/rest-api/src/com/haulmont/addon/restapi/api/service/filter/data/RolesInfo.java diff --git a/modules/rest-api/src/com/haulmont/addon/restapi/api/controllers/PermissionsController.java b/modules/rest-api/src/com/haulmont/addon/restapi/api/controllers/PermissionsController.java index 4f53be2..9a17aef 100644 --- a/modules/rest-api/src/com/haulmont/addon/restapi/api/controllers/PermissionsController.java +++ b/modules/rest-api/src/com/haulmont/addon/restapi/api/controllers/PermissionsController.java @@ -18,6 +18,7 @@ import com.haulmont.addon.restapi.api.service.filter.data.PermissionInfo; import com.haulmont.addon.restapi.api.service.PermissionsControllerManager; +import com.haulmont.addon.restapi.api.service.filter.data.RolesInfo; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -40,4 +41,9 @@ public class PermissionsController { public Collection getPermissions() { return permissionsControllerManager.getPermissionInfos(); } + + @GetMapping("/v2/roles") + public RolesInfo getRoles() { + return permissionsControllerManager.getRolesInfo(); + } } \ No newline at end of file diff --git a/modules/rest-api/src/com/haulmont/addon/restapi/api/service/PermissionsControllerManager.java b/modules/rest-api/src/com/haulmont/addon/restapi/api/service/PermissionsControllerManager.java index beb241c..a5483f4 100644 --- a/modules/rest-api/src/com/haulmont/addon/restapi/api/service/PermissionsControllerManager.java +++ b/modules/rest-api/src/com/haulmont/addon/restapi/api/service/PermissionsControllerManager.java @@ -19,8 +19,11 @@ import com.haulmont.addon.restapi.api.controllers.PermissionsController; import com.haulmont.addon.restapi.api.exception.RestAPIException; import com.haulmont.addon.restapi.api.service.filter.data.PermissionInfo; +import com.haulmont.addon.restapi.api.service.filter.data.RoleInfo; +import com.haulmont.addon.restapi.api.service.filter.data.RolesInfo; import com.haulmont.cuba.core.global.UserSessionSource; import com.haulmont.cuba.security.entity.PermissionType; +import com.haulmont.cuba.security.entity.User; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; @@ -83,4 +86,23 @@ protected String getPermissionValueStr(PermissionType permissionType, int value) } throw new RestAPIException("Cannot evaluate permission value", "", HttpStatus.INTERNAL_SERVER_ERROR); } + + public RolesInfo getRolesInfo() { + + RolesInfo rolesInfo = new RolesInfo(); + rolesInfo.roles = new ArrayList<>(); + User user = userSessionSource.getUserSession().getCurrentOrSubstitutedUser(); + + if (user == null || user.getUserRoles() == null) return rolesInfo; + + rolesInfo.permissions = getPermissionInfos(); + + user.getUserRoles().forEach(userRole -> { + RoleInfo roleInfo = new RoleInfo(); + roleInfo.roleType = userRole == null || userRole.getRole() == null ? null : userRole.getRole().getType(); + rolesInfo.roles.add(roleInfo); + }); + + return rolesInfo; + } } diff --git a/modules/rest-api/src/com/haulmont/addon/restapi/api/service/filter/data/RoleInfo.java b/modules/rest-api/src/com/haulmont/addon/restapi/api/service/filter/data/RoleInfo.java new file mode 100644 index 0000000..c276462 --- /dev/null +++ b/modules/rest-api/src/com/haulmont/addon/restapi/api/service/filter/data/RoleInfo.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2008-2019 Haulmont. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.haulmont.addon.restapi.api.service.filter.data; + +import com.haulmont.cuba.security.entity.RoleType; + +public class RoleInfo { + + public RoleType roleType; + +} diff --git a/modules/rest-api/src/com/haulmont/addon/restapi/api/service/filter/data/RolesInfo.java b/modules/rest-api/src/com/haulmont/addon/restapi/api/service/filter/data/RolesInfo.java new file mode 100644 index 0000000..9298f70 --- /dev/null +++ b/modules/rest-api/src/com/haulmont/addon/restapi/api/service/filter/data/RolesInfo.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2008-2019 Haulmont. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.haulmont.addon.restapi.api.service.filter.data; + +import java.util.Collection; + +public class RolesInfo { + + public Collection permissions; + + public Collection roles; +} diff --git a/modules/rest-api/src/com/haulmont/addon/restapi/rest-dispatcher-spring.xml b/modules/rest-api/src/com/haulmont/addon/restapi/rest-dispatcher-spring.xml index aafa215..9bbfe3d 100644 --- a/modules/rest-api/src/com/haulmont/addon/restapi/rest-dispatcher-spring.xml +++ b/modules/rest-api/src/com/haulmont/addon/restapi/rest-dispatcher-spring.xml @@ -155,6 +155,7 @@ +