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
  • Role-level data scope / data permission

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)
dataScopeIntegerData scope (1 all, 2 custom, 3 single dept, 4 dept tree, 5 self-only)
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
POST/system/role/data-scopeUpdate role data scope
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

Data Permission

In addition to menu and button permissions, each role can define a data scope that controls which records the role's members can see. The scope is stored on sys_role.data_scope and applied via the @DataPermission annotation combined with JPA Specifications.

Data Scopes

ScopeValueDescription
All1Access all data
Custom2Access data only from selected departments
Single Department3Access data from the user's own department
Department Tree4Access data from the user's department and all descendants
Self Only5Access only records created by the user

Configuration

Admins set the data scope in the role management UI. When Custom is selected, a department tree lets the admin pick the allowed departments. The backend exposes POST /system/role/data-scope to persist the configuration.

Usage in Code

java
@GetMapping("/system/user")
@DataPermission(deptAlias = "deptId", userAlias = "userId")
public ResponseResult<PageResult<UserVO>> list(SysUserRequest request) {
    Specification<SysUser> spec = dataScopeSpecification.apply(
        queryHelper.build(request), DataScopeContextHolder.get()
    );
    return ResponseResult.success(userRepository.findAll(spec, pageable));
}

Role Assignment Flow

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

Released under the MIT License.