feat(model): refactor Group model and add groups attributes for inheritance

This commit is contained in:
Julien Oculi 2024-06-17 18:10:07 +02:00
parent fda7b03e59
commit 6d08698d10

View file

@ -1,32 +1,49 @@
import type { Posix, ToJson, UUID } from '@/types.ts'
import { Ressource } from '@models/ressource.ts'
import { Ref } from '@models'
export class Group extends Ressource {
static fromJSON(
{ posix, ...json }: ToJson<Group>,
{ posix, groups, ...json }: ToJson<Group>,
): Group {
return new Group({ ...json, posix: posix ?? undefined })
return new Group({
...json,
posix: posix ?? undefined,
groups: groups.map((group) => Ref.fromString<Group>(group)),
})
}
static create(
{ posix, permissions, name }: Pick<Group, 'posix' | 'permissions' | 'name'>,
{ posix, permissions, name, groups }: Pick<
Group,
'posix' | 'permissions' | 'name' | 'groups'
>,
): Group {
const { uuid, createdAt, updatedAt } = super.create({ name })
return new Group({ uuid, createdAt, updatedAt, name, posix, permissions })
return new Group({
uuid,
createdAt,
updatedAt,
name,
posix,
permissions,
groups,
})
}
#posix?: Posix
#permissions: {
#permissions: Readonly<{
[serviceOrMachine: UUID]: {
read: boolean
write: boolean
execute: boolean
}
} = {}
}>
#groups: readonly Ref<Group>[]
private constructor(
{ posix, permissions, ...props }:
& Pick<Group, 'posix' | 'permissions'>
{ posix, permissions, groups, ...props }:
& Pick<Group, 'posix' | 'permissions' | 'groups'>
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
) {
super(props)
@ -50,7 +67,8 @@ export class Group extends Ressource {
this.#posix = posix
}
this.permissions = Object.freeze(permissions)
this.#permissions = Object.freeze(permissions)
this.#groups = Object.freeze(groups)
}
get type(): 'group' {
@ -62,7 +80,11 @@ export class Group extends Ressource {
}
get permissions() {
return Object.freeze(this.#permissions)
return this.#permissions
}
get groups() {
return this.#groups
}
update(props: Partial<Omit<Group, 'type' | 'uuid' | 'createdAt'>>): Group {
@ -77,6 +99,7 @@ export class Group extends Ressource {
type: this.type,
posix: this.posix ?? null,
permissions: this.permissions,
groups: this.groups.map((group) => group.toJSON()),
} as const
}
@ -88,6 +111,7 @@ export class Group extends Ressource {
export interface Group extends Ressource {
type: 'group'
posix: Posix | undefined
groups: readonly Ref<Group>[]
permissions: Readonly<{
[serviceOrMachine: UUID]: {
read: boolean