Giter Club home page Giter Club logo

permission-controller's Introduction

npm

PermissionController

这个类用于处理权限控制逻辑。

用法

导入类:

import PermissionController from "permission-controller";

// 初始化
const permission = new PermissionController({
  userRole: "admin",
  rolePermissions: ["access_backend"],
  userPermissions: ["edit_article"],
  redirect: () => router.push("/403"),
});

// 检查权限
const canEdit = permission.checkPermission("edit_article");

// 权限保护组件
const ArticleEdit = () => {
  // 编辑文章的组件
};

const NoPermission = () => {
  // 无权限提示组件
};

const ProtectedComponent = permission.AuthorizedComponent(
  ArticleEdit,
  "edit_article",
  NoPermission
);

// 更新用户信息
permission.updateUser({
  userRole: "editor",
  rolePermissions: ["edit_article"],
  userPermissions: [],
});

API

构造函数

new PermissionController(options)
  • options
    • userRole - 当前用户角色
    • rolePermissions - 角色对应的权限列表
    • userPermissions - 用户个人拥有的权限列表
    • redirect - 无权限时的重定向方法

实例方法

  • checkPermission(permission) - 检查权限
  • updateUser(options) - 更新用户信息
  • AuthorizedComponent(Component, permission, NoPermissionComponent) - 权限保护组件

详细说明

  1. 权限判定顺序:用户个人权限 > 角色权限
  2. 提供无权限组件增强可定制性
  3. 支持权限变更后的更新

permission-controller's People

Contributors

wp0403 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.