admin area
This commit is contained in:
74
core/admin/domain/value-objects/UserRole.ts
Normal file
74
core/admin/domain/value-objects/UserRole.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { IValueObject } from '@core/shared/domain';
|
||||
import { AdminDomainValidationError } from '../errors/AdminDomainError';
|
||||
|
||||
export type UserRoleValue = string;
|
||||
|
||||
export interface UserRoleProps {
|
||||
value: UserRoleValue;
|
||||
}
|
||||
|
||||
export class UserRole implements IValueObject<UserRoleProps> {
|
||||
readonly value: UserRoleValue;
|
||||
|
||||
private constructor(value: UserRoleValue) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
static create(value: UserRoleValue): UserRole {
|
||||
// Handle null/undefined
|
||||
if (value === null || value === undefined) {
|
||||
throw new AdminDomainValidationError('Role cannot be empty');
|
||||
}
|
||||
|
||||
const trimmed = value.trim();
|
||||
|
||||
if (!trimmed) {
|
||||
throw new AdminDomainValidationError('Role cannot be empty');
|
||||
}
|
||||
|
||||
return new UserRole(trimmed);
|
||||
}
|
||||
|
||||
static fromString(value: string): UserRole {
|
||||
return this.create(value);
|
||||
}
|
||||
|
||||
get props(): UserRoleProps {
|
||||
return { value: this.value };
|
||||
}
|
||||
|
||||
toString(): UserRoleValue {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
equals(other: IValueObject<UserRoleProps>): boolean {
|
||||
return this.value === other.props.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this role is a system administrator role
|
||||
*/
|
||||
isSystemAdmin(): boolean {
|
||||
const lower = this.value.toLowerCase();
|
||||
return lower === 'owner' || lower === 'admin';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this role has higher authority than another role
|
||||
*/
|
||||
hasHigherAuthorityThan(other: UserRole): boolean {
|
||||
const hierarchy: Record<string, number> = {
|
||||
user: 0,
|
||||
admin: 1,
|
||||
owner: 2,
|
||||
};
|
||||
|
||||
const myValue = this.value.toLowerCase();
|
||||
const otherValue = other.value.toLowerCase();
|
||||
|
||||
const myRank = hierarchy[myValue] ?? 0;
|
||||
const otherRank = hierarchy[otherValue] ?? 0;
|
||||
|
||||
return myRank > otherRank;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user