Skip to content

Commit

Permalink
[ADD] 添加角色授权、角色授权主体、角色授权范围三个管理相关功能及单元测试
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Yeh <[email protected]>
  • Loading branch information
alan-yeh committed Dec 19, 2024
1 parent d7191d8 commit df98647
Show file tree
Hide file tree
Showing 11 changed files with 973 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
package central.studio.dashboard.controller.authority.controller;

import central.bean.Page;
import central.data.authority.Role;
import central.data.authority.*;
import central.starter.web.param.IdsParams;
import central.starter.web.query.IdQuery;
import central.studio.dashboard.controller.authority.param.RoleParams;
import central.studio.dashboard.controller.authority.param.RolePermissionParams;
import central.studio.dashboard.controller.authority.param.RolePrincipalParams;
import central.studio.dashboard.controller.authority.param.RoleRangeParams;
import central.studio.dashboard.controller.authority.query.RolePageQuery;
import central.studio.dashboard.logic.authority.MenuLogic;
import central.studio.dashboard.logic.authority.RoleLogic;
import central.validation.group.Insert;
import central.validation.group.Update;
Expand All @@ -41,6 +45,8 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* System Role Controller
* <p>
Expand All @@ -64,6 +70,7 @@ public interface Permissions {
String ENABLE = "${application}:authority:system:role:enable";
String DISABLE = "${application}:authority:system:role:disable";
}

@Setter(onMethod_ = @Autowired)
private RoleLogic logic;

Expand Down Expand Up @@ -129,4 +136,122 @@ public Role update(@RequestBody @Validated({Update.class, Default.class}) RolePa
public long delete(@Validated IdsParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.deleteByIds(params.getIds(), accountId, tenant);
}

@Setter(onMethod_ = @Autowired)
private MenuLogic menuLogic;
/**
* 获取已授权的权限
* <p>
* 以菜单树的形式,方便前端展示
*
* @param query 角色查询
* @param tenant 租户标识
* @return 授权菜单树
*/
@GetMapping("/permissions")
public List<Menu> getPermissions(@Validated IdQuery<Role> query, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
var relations = this.logic.findPermissions(query.getId(), tenant);
return this.menuLogic.getMenuTreeByPermissionIds(relations.stream().map(RolePermission::getPermissionId).toList(), tenant);
}

/**
* 为角色授权权限
*
* @param params 授权参数
* @param tenant 租户标识
* @return 已添加的授权
*/
@PostMapping("/permissions")
public List<RolePermission> addPermissions(@Validated @RequestBody RolePermissionParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.insertPermissions(params.toInputs(), accountId, tenant);
}

/**
* 移除角色的权限
*
* @param params 待移除的权限主键
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 受影响数据行数
*/
@DeleteMapping("/permissions")
public long deletePermissions(@Validated IdsParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.deletePermissions(params.getIds(), accountId, tenant);
}

/**
* 获取已授权的主体
*
* @param query 角色查询
* @param tenant 租户标识
* @return 授权主体列表
*/
@GetMapping("/principals")
public List<RolePrincipal> getPrincipals(@Validated IdQuery<Role> query, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.findPrincipals(query.getId(), tenant);
}

/**
* 添加授权主体
*
* @param params 授权主体参数
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 已添加的授权主体
*/
@PostMapping("/principals")
public List<RolePrincipal> addPrincipals(@Validated @RequestBody RolePrincipalParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.insertPrincipals(params.toInputs(), accountId, tenant);
}

/**
* 删除授权主体
*
* @param params 待移除的授权主体主键
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 受影响数据行数
*/
@DeleteMapping("/principals")
public long deletePrincipals(@Validated IdsParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.deletePrincipals(params.getIds(), accountId, tenant);
}

/**
* 获取已授权的范围
*
* @param query 角色查询
* @param tenant 租户标识
* @return 授权范围列表
*/
@GetMapping("/ranges")
public List<RoleRange> getRanges(@Validated IdQuery<Role> query, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.findRanges(query.getId(), tenant);
}

/**
* 添加授权范围
*
* @param params 授权范围参数
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 已添加的授权范围
*/
@PostMapping("/ranges")
public List<RoleRange> addRanges(@Validated @RequestBody RoleRangeParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.insertRanges(params.toInputs(), accountId, tenant);
}

/**
* 删除数据范围
*
* @param params 待删除的数据范围主键
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 受影响数据行数
*/
@DeleteMapping("/ranges")
public long deleteRanges(@Validated IdsParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.deleteRanges(params.getIds(), accountId, tenant);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* MIT License
*
* Copyright (c) 2022-present Alan Yeh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package central.studio.dashboard.controller.authority.param;

import central.data.authority.RolePermissionInput;
import central.util.Listx;
import central.validation.Label;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* Role Permission Relation Params
* <p>
* 角色与权限关联关系入参
*
* @author Alan Yeh
* @since 2024/12/16
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RolePermissionParams {

@Label("应用主键")
@NotBlank
@Size(min = 1, max = 32)
private String applicationId;

@Label("角色主键")
@NotBlank
@Size(min = 1, max = 32)
private String roleId;

@Label("权限主键")
@NotEmpty
private List<String> permissionIds;

public List<RolePermissionInput> toInputs() {
return Listx.asStream(this.getPermissionIds())
.map(permissionId -> RolePermissionInput.builder()
.applicationId(this.getApplicationId())
.roleId(this.getRoleId())
.permissionId(permissionId)
.build())
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* MIT License
*
* Copyright (c) 2022-present Alan Yeh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package central.studio.dashboard.controller.authority.param;

import central.data.authority.RolePrincipalInput;
import central.data.authority.option.PrincipalType;
import central.util.Listx;
import central.validation.Enums;
import central.validation.Label;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* Role Principal Relation Params
* <p>
* 角色与主体关联关系入参
*
* @author Alan Yeh
* @since 2024/12/16
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RolePrincipalParams {

@Label("应用主键")
@NotBlank
@Size(min = 1, max = 32)
private String applicationId;

@Label("角色")
@NotBlank
@Size(min = 1, max = 32)
private String roleId;

@Label("授权主体主键")
@NotEmpty
private List<String> principalIds;

@Label("主体类型")
@NotBlank
@Enums(PrincipalType.class)
private String type;

public List<RolePrincipalInput> toInputs() {
return Listx.asStream(this.getPrincipalIds())
.map(principalId -> RolePrincipalInput.builder()
.applicationId(this.getApplicationId())
.roleId(this.getRoleId())
.type(this.getType())
.principalId(principalId)
.build())
.toList();
}
}
Loading

0 comments on commit df98647

Please sign in to comment.