refactor(core): Remove roleId indirection (no-changelog) (#8413)
This commit is contained in:
committed by
GitHub
parent
1affebd85e
commit
d6deceacde
@@ -1,39 +0,0 @@
|
||||
import { Column, Entity, OneToMany, PrimaryColumn, Unique } from 'typeorm';
|
||||
import { IsString, Length } from 'class-validator';
|
||||
|
||||
import type { User } from './User';
|
||||
import type { SharedWorkflow } from './SharedWorkflow';
|
||||
import type { SharedCredentials } from './SharedCredentials';
|
||||
import { WithTimestamps } from './AbstractEntity';
|
||||
import { idStringifier } from '../utils/transformers';
|
||||
|
||||
export type RoleNames = 'owner' | 'member' | 'user' | 'editor' | 'admin';
|
||||
export type RoleScopes = 'global' | 'workflow' | 'credential';
|
||||
|
||||
@Entity()
|
||||
@Unique(['scope', 'name'])
|
||||
export class Role extends WithTimestamps {
|
||||
@PrimaryColumn({ transformer: idStringifier })
|
||||
id: string;
|
||||
|
||||
@Column({ length: 32 })
|
||||
@IsString({ message: 'Role name must be of type string.' })
|
||||
@Length(1, 32, { message: 'Role name must be 1 to 32 characters long.' })
|
||||
name: RoleNames;
|
||||
|
||||
@Column()
|
||||
scope: RoleScopes;
|
||||
|
||||
@OneToMany('User', 'globalRole')
|
||||
globalForUsers: User[];
|
||||
|
||||
@OneToMany('SharedWorkflow', 'role')
|
||||
sharedWorkflows: SharedWorkflow[];
|
||||
|
||||
@OneToMany('SharedCredentials', 'role')
|
||||
sharedCredentials: SharedCredentials[];
|
||||
|
||||
get cacheKey() {
|
||||
return `role:${this.scope}:${this.name}`;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,14 @@
|
||||
import { Column, Entity, ManyToOne, PrimaryColumn } from 'typeorm';
|
||||
import { CredentialsEntity } from './CredentialsEntity';
|
||||
import { User } from './User';
|
||||
import { Role } from './Role';
|
||||
import { WithTimestamps } from './AbstractEntity';
|
||||
|
||||
export type CredentialSharingRole = 'credential:owner' | 'credential:user';
|
||||
|
||||
@Entity()
|
||||
export class SharedCredentials extends WithTimestamps {
|
||||
@ManyToOne('Role', 'sharedCredentials', { nullable: false })
|
||||
role: Role;
|
||||
|
||||
@Column()
|
||||
roleId: string;
|
||||
role: CredentialSharingRole;
|
||||
|
||||
@ManyToOne('User', 'sharedCredentials')
|
||||
user: User;
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
import { Column, Entity, ManyToOne, PrimaryColumn } from 'typeorm';
|
||||
import { WorkflowEntity } from './WorkflowEntity';
|
||||
import { User } from './User';
|
||||
import { Role } from './Role';
|
||||
import { WithTimestamps } from './AbstractEntity';
|
||||
|
||||
export type WorkflowSharingRole = 'workflow:owner' | 'workflow:editor' | 'workflow:user';
|
||||
|
||||
@Entity()
|
||||
export class SharedWorkflow extends WithTimestamps {
|
||||
@ManyToOne('Role', 'sharedWorkflows', { nullable: false })
|
||||
role: Role;
|
||||
|
||||
@Column()
|
||||
roleId: string;
|
||||
role: WorkflowSharingRole;
|
||||
|
||||
@ManyToOne('User', 'sharedWorkflows')
|
||||
user: User;
|
||||
|
||||
@@ -6,13 +6,11 @@ import {
|
||||
Entity,
|
||||
Index,
|
||||
OneToMany,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
BeforeInsert,
|
||||
} from 'typeorm';
|
||||
import { IsEmail, IsString, Length } from 'class-validator';
|
||||
import type { IUser, IUserSettings } from 'n8n-workflow';
|
||||
import { Role } from './Role';
|
||||
import type { SharedWorkflow } from './SharedWorkflow';
|
||||
import type { SharedCredentials } from './SharedCredentials';
|
||||
import { NoXss } from '../utils/customValidators';
|
||||
@@ -23,10 +21,13 @@ import type { AuthIdentity } from './AuthIdentity';
|
||||
import { ownerPermissions, memberPermissions, adminPermissions } from '@/permissions/roles';
|
||||
import { hasScope, type ScopeOptions, type Scope } from '@n8n/permissions';
|
||||
|
||||
const STATIC_SCOPE_MAP: Record<string, Scope[]> = {
|
||||
owner: ownerPermissions,
|
||||
member: memberPermissions,
|
||||
admin: adminPermissions,
|
||||
export type GlobalRole = 'global:owner' | 'global:admin' | 'global:member';
|
||||
export type AssignableRole = Exclude<GlobalRole, 'global:owner'>;
|
||||
|
||||
const STATIC_SCOPE_MAP: Record<GlobalRole, Scope[]> = {
|
||||
'global:owner': ownerPermissions,
|
||||
'global:member': memberPermissions,
|
||||
'global:admin': adminPermissions,
|
||||
};
|
||||
|
||||
@Entity()
|
||||
@@ -72,11 +73,8 @@ export class User extends WithTimestamps implements IUser {
|
||||
})
|
||||
settings: IUserSettings | null;
|
||||
|
||||
@ManyToOne('Role', 'globalForUsers', { nullable: false })
|
||||
globalRole: Role;
|
||||
|
||||
@Column()
|
||||
globalRoleId: string;
|
||||
role: GlobalRole;
|
||||
|
||||
@OneToMany('AuthIdentity', 'user')
|
||||
authIdentities: AuthIdentity[];
|
||||
@@ -127,11 +125,11 @@ export class User extends WithTimestamps implements IUser {
|
||||
|
||||
@AfterLoad()
|
||||
computeIsOwner(): void {
|
||||
this.isOwner = this.globalRole?.name === 'owner';
|
||||
this.isOwner = this.role === 'global:owner';
|
||||
}
|
||||
|
||||
get globalScopes() {
|
||||
return STATIC_SCOPE_MAP[this.globalRole?.name] ?? [];
|
||||
return STATIC_SCOPE_MAP[this.role] ?? [];
|
||||
}
|
||||
|
||||
hasGlobalScope(scope: Scope | Scope[], scopeOptions?: ScopeOptions): boolean {
|
||||
|
||||
@@ -6,7 +6,6 @@ import { EventDestinations } from './EventDestinations';
|
||||
import { ExecutionEntity } from './ExecutionEntity';
|
||||
import { InstalledNodes } from './InstalledNodes';
|
||||
import { InstalledPackages } from './InstalledPackages';
|
||||
import { Role } from './Role';
|
||||
import { Settings } from './Settings';
|
||||
import { SharedCredentials } from './SharedCredentials';
|
||||
import { SharedWorkflow } from './SharedWorkflow';
|
||||
@@ -29,7 +28,6 @@ export const entities = {
|
||||
ExecutionEntity,
|
||||
InstalledNodes,
|
||||
InstalledPackages,
|
||||
Role,
|
||||
Settings,
|
||||
SharedCredentials,
|
||||
SharedWorkflow,
|
||||
|
||||
Reference in New Issue
Block a user