Smart Site
Home Docs Smart Data Permission Management

Permission Management

The Permission module provides a full Role-Based Access Control (RBAC) layer for the Smart SCADA platform. It governs who can log in, which menus are visible, which routes are reachable, and which buttons/actions are available on each page.

Interface Screenshot

Permission - User Management (English UI)

Permission - Role Management (English UI)

Captured from the live Smart Site system with the English UI. See the zh-CN counterpart for the Chinese-UI screenshot.

1. Core Concepts

The system uses a three-level role hierarchy with numeric weights to resolve conflicts:

Role Code Weight Description
Admin admin 2 Full access, can manage users and roles
User user 1 Standard operator, can edit operational data
Viewer viewer 0 Read-only access

The weight ordering is viewer (0) < user (1) < admin (2). When a menu or button requires a minimum role, the user's role weight must be greater than or equal to the required weight.

2. Functional Areas

2.1 User Management

  • Create / edit / disable users
  • Reset password, force password change on first login
  • Assign roles and positions to each user
  • Bind users to specific projects and device groups (data-level authorization)

Users are stored in the sys_users table and managed by UserService in @ss-node/ss-core.

2.2 Role Management

  • Built-in roles (admin, user, viewer) are marked isSystem: true and cannot be deleted
  • Custom roles can be created with a granular permission code list (permissions: string[])
  • Each role carries a menuIds array that controls which menu items are visible to its members
  • The RoleService exposes CRUD and permission assignment endpoints

2.3 Position Management

  • Positions represent organizational titles (e.g. "Operator", "Engineer")
  • Managed via PositionController / PositionService
  • Used for filtering and reporting, independent of role-based permissions

2.4 Menu Permission Configuration

Menu visibility is driven by two sources that must stay in sync:

  1. Frontend — system-menu.config.ts defines menu items with roles and optional permission codes
  2. Backend — system-menu-config.service.ts exposes the DEFAULT_SYSTEM_MENU and persists items to the sys_menus table

The backend insertMissingMenus() routine automatically backfills any missing menu rows on startup, so newly added items appear without manual SQL.

2.5 Button-Level Permissions

For fine-grained control inside a page, use the usePermission composable:

const { can, hasRole, hasPermission } = usePermission()

// Check by permission code
if (can('device:delete')) { ... }

// Check by minimum role weight
if (hasRole('user')) { ... }

Button-level checks read from the user's permissions array and role weight.

2.6 Route Access Control

Route guards rely on two helpers:

  • buildMenuRoleMap(menuConfig) — builds a lookup of route path → required role/permission
  • canAccessPath(path, userRole, userPermissions) — returns true if the current user may enter the route

The frontend router guard calls canAccessPath before each navigation; unreachable routes are also hidden from the sidebar.

3. Permission Codes

Permission codes follow a module:action convention, for example:

Code Scope
device:read View devices
device:write Create / update devices
device:delete Delete devices
alarm:ack Acknowledge alarms
user:manage Manage users and roles

Codes are centrally defined in permission-codes.ts and enforced by the PermissionsGuard on the backend.

4. Authentication Flow

  1. The user submits credentials to POST /api/v1/auth/login
  2. AuthService validates the password and returns a JWT access token + refresh token
  3. The frontend stores the token and attaches it as Authorization: Bearer <token>
  4. JwtAuthGuard verifies the token on every protected endpoint
  5. Optional MFA (TOTP) can be enabled per user via the MfaController

5. FAQ

Q: A new menu item is invisible after deployment. A: Confirm all 9 registration points listed in AGENTS.md section 10.5 are updated, especially init-system-db.sql (for existing deployments) and the roles array in system-menu.config.ts. Then restart the backend so insertMissingMenus() runs.

Q: A user with the user role cannot see a button that requires device:write. A: Either add device:write to the role's permissions list, or raise the user's role to admin. Button visibility is driven by the usePermission().can() check.

Q: How is the role weight used? A: Weights are compared numerically (viewer 0 < user 1 < admin 2). hasRole('user') returns true when the current user's weight is >= 1.

Q: Can I delete the built-in roles? A: No. Roles with isSystem: true are protected and the delete endpoint returns 409 Conflict.