refactor event
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import type { Role } from "@/store/permissionSettingsStore";
|
||||
import type { ManagedUser } from "@/types/ManagedUser";
|
||||
|
||||
type ManagedUserRole = NonNullable<ManagedUser["roleList"]>[number];
|
||||
|
||||
function buildRoleCode(role: Role): string {
|
||||
return role.moduleCode && !role.code.includes(":") ? `${role.moduleCode}:${role.code}` : role.code;
|
||||
}
|
||||
|
||||
function getBareRoleCode(code: string): string {
|
||||
const codePartList = code.split(":");
|
||||
return codePartList[codePartList.length - 1] || code;
|
||||
}
|
||||
|
||||
function findRole(role: ManagedUserRole, roleList: Role[]): Role | undefined {
|
||||
if (typeof role === "string") {
|
||||
return roleList.find(roleItem => roleItem.code === role || buildRoleCode(roleItem) === role);
|
||||
}
|
||||
return roleList.find(roleItem => roleItem.id === role.id || roleItem.code === role.code || buildRoleCode(roleItem) === role.code);
|
||||
}
|
||||
|
||||
function appendCompatibleRoleCode(roleCodeSet: Set<string>, code: string): void {
|
||||
if (!code) {
|
||||
return;
|
||||
}
|
||||
|
||||
roleCodeSet.add(code);
|
||||
roleCodeSet.add(getBareRoleCode(code));
|
||||
}
|
||||
|
||||
export function getRoleDisplayName(role: ManagedUserRole | undefined, roleList: Role[]): string {
|
||||
if (!role) {
|
||||
return "未配置角色";
|
||||
}
|
||||
|
||||
const fullRole = findRole(role, roleList);
|
||||
if (fullRole) {
|
||||
return fullRole.name || fullRole.code || "未配置角色";
|
||||
}
|
||||
|
||||
return typeof role === "string" ? getBareRoleCode(role) : role.name || role.code || "未配置角色";
|
||||
}
|
||||
|
||||
export function buildEffectiveRoleCodeSet(user: ManagedUser, roleList: Role[]): Set<string> {
|
||||
const roleCodeSet = new Set<string>();
|
||||
for (const role of user.roleList || []) {
|
||||
const fullRole = findRole(role, roleList);
|
||||
if (fullRole) {
|
||||
appendRoleCode(roleCodeSet, fullRole, roleList, new Set<string>());
|
||||
continue;
|
||||
}
|
||||
appendCompatibleRoleCode(roleCodeSet, typeof role === "string" ? role : role.code);
|
||||
}
|
||||
return roleCodeSet;
|
||||
}
|
||||
|
||||
function appendRoleCode(roleCodeSet: Set<string>, role: Role, roleList: Role[], visitedRoleIds: Set<string>): void {
|
||||
if (!role.id || visitedRoleIds.has(role.id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
visitedRoleIds.add(role.id);
|
||||
appendCompatibleRoleCode(roleCodeSet, buildRoleCode(role));
|
||||
for (const childRoleId of role.childRoleIdList) {
|
||||
const childRole = roleList.find(item => item.id === childRoleId);
|
||||
if (childRole) {
|
||||
appendRoleCode(roleCodeSet, childRole, roleList, visitedRoleIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user