Skip to content

Role & Permission

The role and permission system provides fine-grained access control with menu-based permissions and button-level authorization.

Features

  • Role CRUD with status management
  • Menu permission tree for each role
  • Button-level permission control via permissions array
  • Duplicate role key/name detection
  • Role-menu relationship management

Data Model

SysRole (sys_role)

FieldTypeDescription
roleIdLongPrimary key
roleNameStringDisplay name (e.g., "Administrator")
roleKeyStringUnique identifier (e.g., "admin")
roleSortIntegerSort order
statusIntegerStatus (0: disabled, 1: enabled)
remarkStringNotes

SysRoleMenu (sys_role_menu)

FieldTypeDescription
roleIdLongRole ID
menuIdLongMenu ID

This is a many-to-many join table linking roles to their permitted menus.

API Endpoints

Admin API

MethodEndpointDescription
GET/admin-api/list-all-roleList all roles
POST/admin-api/list-role-idsGet role IDs for a user
POST/admin-api/roleList roles (paginated)
POST/admin-api/role/createCreate role
PUT/admin-api/role/updateUpdate role
POST/admin-api/role/deleteDelete role(s)
POST/admin-api/role/statusToggle role status
POST/admin-api/role/save-menuSave menu permissions for role
POST/admin-api/role-menuList role-menu data
POST/admin-api/role-menu-idsGet menu IDs for a role

RESTful API

MethodEndpointDescription
GET/system/roleList all roles
GET/system/role/{id}Get role by ID
GET/system/role/key/{roleKey}Get role by key
GET/system/role/allList all roles (no pagination)
GET/system/role/activeList active roles
POST/system/roleCreate role
PUT/system/role/{id}Update role
DELETE/system/role/{id}Delete role
GET/system/role/exists/keyCheck if role key exists
GET/system/role/exists/nameCheck if role name exists

Permission Model

Each role is assigned a set of menus. When a user logs in, the system loads all menus associated with their roles to build the sidebar navigation. Menus not in the user's role set are hidden.

Button-Level Permissions

Menu items can have isButton = true with a permission string (e.g., system:user:create). These permissions are returned in the meta.auths array of the route data.

On the frontend, use the hasPerms() utility to conditionally render buttons:

vue
<template>
  <el-button v-if="hasPerms(['system:user:create'])">
    Create User
  </el-button>
</template>

Permission Format

Permissions follow the pattern: module:entity:action

PermissionDescription
system:user:createCreate user
system:user:updateUpdate user
system:user:deleteDelete user
system:role:createCreate role
system:menu:createCreate menu

Role Assignment Flow

  1. Admin creates a role and assigns menu permissions via the permission tree
  2. Admin assigns roles to users in the user management page
  3. On login, the backend fetches the user's roles and their combined menu permissions
  4. The frontend builds the sidebar and button visibility based on these permissions

Released under the MIT License.