Skip to content

Commit

Permalink
Provide roles types of current user #78 (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimbasko authored Nov 24, 2019
1 parent 868b7ef commit 1d84d8c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,4 +41,9 @@ public class PermissionsController {
public Collection<PermissionInfo> getPermissions() {
return permissionsControllerManager.getPermissionInfos();
}

@GetMapping("/v2/roles")
public RolesInfo getRoles() {
return permissionsControllerManager.getRolesInfo();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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<PermissionInfo> permissions;

public Collection<RoleInfo> roles;
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
<intercept-url pattern="/rest/v2/queries/**" access="isAuthenticated()"/>
<intercept-url pattern="/rest/v2/services/**" access="isAuthenticated()"/>
<intercept-url pattern="/rest/v2/permissions" access="isAuthenticated()"/>
<intercept-url pattern="/rest/v2/roles" access="isAuthenticated()"/>
<intercept-url pattern="/rest/v2/metadata/**" access="isAuthenticated()"/>
<intercept-url pattern="/rest/v2/userInfo" access="isAuthenticated()"/>
<intercept-url pattern="/rest/v2/files/**" access="isAuthenticated()"/>
Expand Down

0 comments on commit 1d84d8c

Please sign in to comment.