Skip to content

Permission Explanation ​

This system supports two permission control modes, dynamically managing page access and button display permissions based on user roles or menu lists.

Permission Control Modes ​

Overview ​

The system provides the following two permission control modes:

  • Role-based: Controls page access and button display permissions by obtaining user roles through an interface.
  • Menu-based: Controls page access and button permissions based on menu structure obtained through an interface.

Configuration Method ​

The permission control mode is configured through the .env file in the root directory. Changing the value of VITE_ACCESS_MODE can switch modes:

  • frontend: Frontend control mode, permission control based on role identifiers returned by the backend.
  • backend: Backend control mode, permission control based on menu lists returned by the backend.
env
# Permission control mode (frontend | backend)
VITE_ACCESS_MODE=frontend

Frontend Control Mode ​

Principle ​

The frontend maintains the menu list. After user login, the interface returns a role identifier (e.g., R_SUPER). The frontend traverses the menu list based on the role, and if the roles field of a menu includes that role, access to the corresponding route is allowed. If roles is not set, all users can access by default.

Configuration Example ​

The menu configuration file is located at: /src/router/routes/asyncRoutes.ts

ts
[
  {
    id: 4,
    path: "/system",
    name: "System",
    component: "/index/index",
    meta: {
      title: "menus.system.title",
      icon: "ri:user-3-line",
      keepAlive: false,
    },
    children: [
      // Only R_SUPER and R_ADMIN roles can access
      {
        id: 41,
        path: "user",
        name: "User",
        component: "/system/user",
        meta: {
          title: "menus.system.user",
          keepAlive: true,
          roles: ["R_SUPER", "R_ADMIN"],
        },
      },
      // roles not set, all users can access
      {
        id: 42,
        path: "role",
        name: "Role",
        component: "/system/role",
        meta: {
          title: "menus.system.role",
          keepAlive: true,
        },
      },
    ],
  },
];

Notes ​

  • Ensure that the role identifiers returned by the interface match the roles field in the route table, otherwise users will not be able to access restricted pages.

Backend Control Mode ​

Principle ​

The backend generates the menu list. After user login, the interface returns menu data, which the frontend validates and dynamically registers routes to implement permission control.

Data Structure ​

The menu data structure is defined at: /src/router/routes/asyncRoutes.ts

ts
[
  {
    id: 4,
    path: "/system",
    name: "System",
    component: "/index/index",
    meta: {
      title: "menus.system.title",
      icon: "ri:user-3-line",
      keepAlive: false,
    },
    children: [
      {
        id: 41,
        path: "user",
        name: "User",
        component: "/system/user",
        meta: {
          title: "menus.system.user",
          keepAlive: true,
        },
      },
      {
        id: 42,
        path: "role",
        name: "Role",
        component: "/system/role",
        meta: {
          title: "menus.system.role",
          keepAlive: true,
        },
      },
    ],
  },
];

Notes ​

  • The backend returns the menu data structure must match the frontend definition, otherwise it may cause the route registration failure.

Frontend and Backend Control Mode Comparison ​

  • Frontend control mode:

    • Suitable for systems with fixed roles.
    • Backend role changes need to be synchronized with frontend route configuration updates.
    • Simple implementation, suitable for small projects.
  • Backend control mode:

    • Suitable for systems with complex permissions.
    • Backend returns complete menu list, frontend dynamically registers routes.
    • More flexible, but requires consistency between frontend and backend data structures.

Button Permissions Control ​

Button permissions control supports refined management, dynamically controlling button display through user roles or permission codes returned by the interface.

Permission Codes ​

Permission codes are applicable to both frontend and backend control modes:

  • Frontend control mode: The login interface needs to return a list of permission codes.
  • Backend control mode: The menu list needs to include an authList field, defining button permissions.

Configuration Example (Backend Control Mode) ​

ts
[
  {
    id: 44,
    path: "menu",
    name: "Menus",
    component: "/system/menu",
    meta: {
      title: "menus.system.menu",
      keepAlive: true,
      authList: [
        { id: 441, title: "Add", authMark: "add" },
        { id: 442, title: "Edit", authMark: "edit" },
      ],
    },
  },
];

Usage Method ​

Control button display through the hasAuth method provided by the system:

ts
import { useAuth } from "@/composables/useAuth";
const { hasAuth } = useAuth();
vue
<ElButton v-if="hasAuth('add')">Add</ElButton>

Custom Directive (v-auth) ​

In backend control mode, you can control button display based on the authMark of authList using the custom directive v-auth.

Configuration Example ​

ts
[
  {
    id: 44,
    path: "menu",
    name: "Menus",
    component: "/system/menu",
    meta: {
      title: "menus.system.menu",
      keepAlive: true,
      authList: [
        { id: 441, title: "Add", authMark: "add" },
        { id: 442, title: "Edit", authMark: "edit" },
        { id: 443, title: "Delete", authMark: "delete" },
      ],
    },
  },
];

Usage Method ​

vue
<ElButton v-auth="'add'">Add</ElButton>

Custom Directive (v-roles) ​

Permission control can be based on the roles returned by the user information interface

User Interface ​

ts
{
    "userId": "1",
    "userName": "Super",
    "roles": [
        "R_SUPER"
    ],
    "buttons": [
        "B_CODE1",
        "B_CODE2",
        "B_CODE3"
    ]
}

Usage Example ​

ts
 <el-button v-roles="['R_SUPER', 'R_ADMIN']">按钮</el-button>
 <el-button v-roles="'R_ADMIN'">按钮</el-button>

Notes ​

  • Ensure that the roles or permission codes returned by the login interface are consistent with the route table configuration.
  • In backend control mode, menu data must strictly follow the structure defined by the frontend.
  • When testing permission control, verify that page and button display for different role users meets expectations.

Released under the MIT License