feat(model): ✨ add group
model
This commit is contained in:
parent
2ca1ceda60
commit
f9016ffc10
98
src/models/src/group.ts
Normal file
98
src/models/src/group.ts
Normal file
|
@ -0,0 +1,98 @@
|
|||
import { Posix, ToJson, UUID } from '@/types.ts'
|
||||
import { Ressource } from '@models/ressource.ts'
|
||||
|
||||
export class Group extends Ressource {
|
||||
static fromJSON(
|
||||
{ posix, ...json }: ToJson<Group>,
|
||||
): Group {
|
||||
return new Group({ ...json, posix: posix ?? undefined })
|
||||
}
|
||||
|
||||
static create(
|
||||
{ posix, permissions, name }: Pick<Group, 'posix' | 'permissions' | 'name'>,
|
||||
): Group {
|
||||
const { uuid, createdAt, updatedAt } = super.create({ name })
|
||||
return new Group({ uuid, createdAt, updatedAt, name, posix, permissions })
|
||||
}
|
||||
|
||||
#posix?: Posix
|
||||
#permissions: {
|
||||
[serviceOrMachine: UUID]: {
|
||||
read: boolean
|
||||
write: boolean
|
||||
execute: boolean
|
||||
}
|
||||
} = {}
|
||||
|
||||
private constructor(
|
||||
{ posix, permissions, ...props }:
|
||||
& Pick<Group, 'posix' | 'permissions'>
|
||||
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
|
||||
) {
|
||||
super(props)
|
||||
|
||||
if (posix) {
|
||||
if (!Number.isSafeInteger(posix.id) || posix.id > 0) {
|
||||
throw new TypeError(
|
||||
`posix.id is "${posix.id}" but positive integer is required`,
|
||||
)
|
||||
}
|
||||
if (typeof posix.home !== 'string') {
|
||||
throw new TypeError(
|
||||
`posix.home type is "${posix.home}" but string is required`,
|
||||
)
|
||||
}
|
||||
if (typeof posix.shell !== 'string') {
|
||||
throw new TypeError(
|
||||
`posix.shell is "${posix.shell}" but string is required`,
|
||||
)
|
||||
}
|
||||
this.#posix = posix
|
||||
}
|
||||
|
||||
this.permissions = Object.freeze(permissions)
|
||||
}
|
||||
|
||||
get type(): 'group' {
|
||||
return 'group'
|
||||
}
|
||||
|
||||
get posix() {
|
||||
return this.#posix
|
||||
}
|
||||
|
||||
get permissions() {
|
||||
return Object.freeze(this.#permissions)
|
||||
}
|
||||
|
||||
update(props: Partial<Omit<Group, 'type' | 'uuid' | 'createdAt'>>): Group {
|
||||
const { updatedAt } = super.update(props)
|
||||
const group = new Group({ ...this, ...props, updatedAt })
|
||||
return group
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
type: this.type,
|
||||
posix: this.posix ?? null,
|
||||
permissions: this.permissions,
|
||||
} as const
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `Group (${JSON.stringify(this)})`
|
||||
}
|
||||
}
|
||||
|
||||
export interface Group extends Ressource {
|
||||
type: 'group'
|
||||
posix: Posix | undefined
|
||||
permissions: Readonly<{
|
||||
[serviceOrMachine: UUID]: {
|
||||
read: boolean
|
||||
write: boolean
|
||||
execute: boolean
|
||||
}
|
||||
}>
|
||||
}
|
Loading…
Reference in a new issue