mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-22 12:20:34 +00:00

* update: Add type * fix: update import statement for NextApiRequest type * fix: update imports to use type for LexicalEditor and EditorState * Refactor imports to use 'import type' for type-only imports across multiple files - Updated imports in various components and API files to use 'import type' for better clarity and to optimize TypeScript's type checking. - Ensured consistent usage of type imports in files related to chat, dataset, workflow, and user management. - Improved code readability and maintainability by distinguishing between value and type imports. * refactor: remove old ESLint configuration and add new rules - Deleted the old ESLint configuration file from the app project. - Added a new ESLint configuration file with updated rules and settings. - Changed imports to use type-only imports in various files for better clarity and performance. - Updated TypeScript configuration to remove unnecessary options. - Added an ESLint ignore file to exclude build and dependency directories from linting. * fix: update imports to use 'import type' for type-only imports in schema files
87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
import { type PermissionListType, type PermissionValueType } from './type';
|
|
import { PermissionList, NullPermission, OwnerPermissionVal } from './constant';
|
|
|
|
export type PerConstructPros = {
|
|
per?: PermissionValueType;
|
|
isOwner?: boolean;
|
|
permissionList?: PermissionListType;
|
|
childUpdatePermissionCallback?: () => void;
|
|
};
|
|
|
|
// the Permission helper class
|
|
export class Permission {
|
|
value: PermissionValueType;
|
|
isOwner: boolean = false;
|
|
hasManagePer: boolean = false;
|
|
hasWritePer: boolean = false;
|
|
hasReadPer: boolean = false;
|
|
_permissionList: PermissionListType;
|
|
|
|
constructor(props?: PerConstructPros) {
|
|
const { per = NullPermission, isOwner = false, permissionList = PermissionList } = props || {};
|
|
if (isOwner) {
|
|
this.value = OwnerPermissionVal;
|
|
} else {
|
|
this.value = per;
|
|
}
|
|
|
|
this._permissionList = permissionList;
|
|
this.updatePermissions();
|
|
}
|
|
|
|
// add permission(s)
|
|
// it can be chaining called.
|
|
// @example
|
|
// const perm = new Permission(permission)
|
|
// perm.add(PermissionList['read'])
|
|
// perm.add(PermissionList['read'], PermissionList['write'])
|
|
// perm.add(PermissionList['read']).add(PermissionList['write'])
|
|
addPer(...perList: PermissionValueType[]) {
|
|
if (this.isOwner) {
|
|
return this;
|
|
}
|
|
for (const per of perList) {
|
|
this.value = this.value | per;
|
|
}
|
|
this.updatePermissions();
|
|
return this;
|
|
}
|
|
|
|
removePer(...perList: PermissionValueType[]) {
|
|
if (this.isOwner) {
|
|
return this.value;
|
|
}
|
|
for (const per of perList) {
|
|
this.value = this.value & ~per;
|
|
}
|
|
this.updatePermissions();
|
|
return this;
|
|
}
|
|
|
|
checkPer(perm: PermissionValueType): boolean {
|
|
// if the permission is owner permission, only owner has this permission.
|
|
if (perm === OwnerPermissionVal) {
|
|
return this.value === OwnerPermissionVal;
|
|
}
|
|
return (this.value & perm) === perm;
|
|
}
|
|
|
|
private updatePermissionCallback?: () => void;
|
|
setUpdatePermissionCallback(callback: () => void) {
|
|
callback();
|
|
this.updatePermissionCallback = callback;
|
|
}
|
|
|
|
private updatePermissions() {
|
|
this.isOwner = this.value === OwnerPermissionVal;
|
|
this.hasManagePer = this.checkPer(this._permissionList['manage'].value);
|
|
this.hasWritePer = this.checkPer(this._permissionList['write'].value);
|
|
this.hasReadPer = this.checkPer(this._permissionList['read'].value);
|
|
this.updatePermissionCallback?.();
|
|
}
|
|
|
|
toBinary() {
|
|
return this.value.toString(2);
|
|
}
|
|
}
|