User Role Assignment Schema

The User Role Assignment Schema specifies how roles are assigned to users and resources within the system.

Key Fields of the UserRoleAssign Schema

resource: The resource field defines for which resource (Porject) the role is assigned, referenced by an ObjectId. user: The user to whom the role is assigned, referenced by an ObjectId. (Optional, as the role might not be user-specific). role: The role assigned to the user or resource, typically referenced by an ObjectId. validFrom: The date from which the role assignment becomes valid. validUntil: The date until which the role assignment remains valid. membership: The membership type of the user, defining whether this role is bound to the user or the resource itself. type: If more than one user collection is needed, this field can be used to differentiate between them. (For Example Users and Dashboard users could be differentiated) system: System-related fields of the user role assignment, such as timestamps for creation and modification.

User Role Assignment Schema Definition

@Schema({
  timestamps: { createdAt: "system.createdAt", updatedAt: "system.modifiedAt" },
})
export class UserRoleAssign extends Document {
  @Prop({
    type: MongooseTypes.ObjectId,
    ref: "Project",
  })
  resource: MongooseTypes.ObjectId;

  @Prop({
    type: MongooseTypes.ObjectId,
    ref: "User",
  })
  user?: MongooseTypes.ObjectId;

  @Prop({
    type: MongooseTypes.ObjectId,
    ref: "Role",
  })
  role: MongooseTypes.ObjectId;

  @Prop()
  validFrom: Date;

  @Prop()
  validUntil: Date;

  @Prop({ enum: RoleMembership, default: RoleMembership.USER })
  membership: RoleMembership;

  @Prop({ enum: ResourceType, default: ResourceType.USER })
  type: ResourceType;

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

export const UserRoleAssignSchema =
  SchemaFactory.createForClass(UserRoleAssign);