Skip to main content

`user-role` System

User Roles

Role hierarchy

Role stringNumeric valueDescription
room_owner1Highest permission
admin2Administrative privileges
editor3Content editing privileges
moderator4Moderation privileges
user5Standard user
guest6Lowest permission

UserRole type

type UserRole =
| "room_owner"
| "admin"
| "editor"
| "moderator"
| "user"
| "guest";

Overview

The user-role system:

  • Stores the current user role at the scene level
  • Normalizes numeric and string role values
  • Provides helpers for permission checks
  • Emits events when roles change
  • Automatically converts numeric values to role strings

API Reference

getRole(): UserRole

Returns the current role as a string.

roleSystem.getRole(); // 'admin'

getRoleNumber(): number

Returns the numeric permission level (1–6).

roleSystem.getRoleNumber(); // 2

getRoleAsInt(): number

Alias for getRoleNumber().

roleSystem.getRoleAsInt(); // 2

setRole(role: UserRole | number | string): void

Sets the role using either a string or number and automatically normalizes numeric values.

Also emits a role-changed event on the scene.

roleSystem.setRole("editor");
roleSystem.setRole(3);
roleSystem.setRole("6"); // -> 'guest'

Emitted event: role-changed

Payload:

{
role: "admin";
}

Listener example:

this.el.sceneEl.addEventListener("role-changed", (e) => {
console.log("Role changed to:", e.detail.role);
});

hasPermission(requiredRole: UserRole | number): boolean

Checks whether the current user has equal or higher permission than the required role.

Permission logic:

currentRoleNumber <= requiredRoleNumber

Examples:

roleSystem.hasPermission("editor"); // true for owner/admin/editor
roleSystem.hasPermission(3); // numeric equivalent

Convenience role checks

isOwner(): boolean

roleSystem.isOwner();

isAdmin(): boolean

roleSystem.isAdmin();

isEditor(): boolean

roleSystem.isEditor();

isModerator(): boolean

roleSystem.isModerator();

Role conversion utilities

roleToNumber(role: UserRole): number

Converts a role string to its numeric permission level.

roleToNumber("admin"); // 2
roleToNumber("guest"); // 6

Defaults to 5 (user) for unknown values.


numberToRole(num: number): UserRole

Converts a numeric permission level to a role string.

numberToRole(2); // 'admin'
numberToRole(6); // 'guest'

Defaults to 'user' for unknown values.


Initialization behavior

  • On system initialization, numeric role values are normalized to strings
  • Console logs indicate role initialization and updates

Example: Permission-gated component

init() {
const roleSystem = this.el.sceneEl.systems['user-role'];

if (!roleSystem.hasPermission('admin')) {
this.el.setAttribute('visible', false);
}
}