Role Schema

The Role Schema defines the structure and relationships of user roles within the system. Roles determine the permissions and access levels that users have when interacting with resources.

Key Fields of the Role Schema

title: The name or title of the role. Only used for display purposes. alias: A unique identifier or alias for the role, defined by an enumeration of RoleAlias. description: An optional field providing more details about the role. Only used for display purposes. permissions: An array of ObjectId references to the Permission model, denoting the permissions assigned to this role. system: System-related fields of the role, such as timestamps for creation and modification.

Role Schema Definition

import { Schema, Prop, SchemaFactory } from "@nestjs/mongoose";
import { Document, Schema as MongooseSchema } from "mongoose";
import { SystemSchema } from "./SystemSchema"; // Assumed import for SystemSchema
import { Permission } from "./Permission"; // Assumed import for Permission

@Schema({
  timestamps: { createdAt: "system.createdAt", updatedAt: "system.modifiedAt" },
})
export class Role extends Document {
  @Prop()
  title: string;

  @Prop({ enum: RoleAlias })
  alias: RoleAlias;

  @Prop({ required: false })
  description?: string;

  @Prop([
    {
      type: [MongooseSchema.Types.ObjectId],
      ref: "Permission",
    },
  ])
  permissions: Permission[];

  @Prop({ type: SystemSchema, required: false })
  system?: System;
}

export const RoleSchema = SchemaFactory.createForClass(Role);

Permission Schema

The Permission Schema outlines the specific actions or operations that roles can perform within the system. Permissions are granular and can be combined to form the access level for a given role.

Key Fields of the Permission Schema

alias: A unique identifier or alias for the permission. description: An optional field providing more details about the permission. type: The category or type of permission, defined by an enumeration of PermissionType. system: System-related fields of the permission, including timestamps for creation and modification.

Permission Schema Definition

import { Schema, Prop, SchemaFactory } from "@nestjs/mongoose";
import { Document, Schema as MongooseSchema } from "mongoose";
import { SystemSchema } from "./SystemSchema"; // Assumed import for SystemSchema

@Schema({
  timestamps: { createdAt: "system.createdAt", updatedAt: "system.modifiedAt" },
})
export class Permission extends Document {
  @Prop()
  alias: string;

  @Prop({ required: false })
  description?: string;

  @Prop({ enum: PermissionType, type: String })
  type: string;

  @Prop({ type: SystemSchema, required: false })
  system?: System;
}

export const PermissionSchema = SchemaFactory.createForClass(Permission);
These schemas are integral to the access control mechanisms within the system, ensuring that users have appropriate permissions to perform their functions while maintaining security and order within the application’s ecosystem.