一个React包装器的应用 - 权限管理

简介: 在我们的应用中,每个按钮都有个权限点,前端通过接口的方式取得此用户的权限点列表,再来决定按钮是否显示。假設我们权限点列表取得后存在```window.globals.permissions```。 传统的方式可以在redner的时候,判断用户是否有此权限,在来决定按钮是否显示。例如: ``` class Page1 extend React.Component { rende

在我们的应用中,每个按钮都有个权限点,前端通过接口的方式取得此用户的权限点列表,再来决定按钮是否显示。假設我们权限点列表取得后存在window.globals.permissions

传统的方式可以在redner的时候,判断用户是否有此权限,在来决定按钮是否显示。例如:

class Page1 extend React.Component {
    render() {
        const {create, update} = window.globals.permissions;
        return (
            {create && <Button>Create Project</Button>}
            {update && <Button>Update Project</Button>}
        );
        
    }
}

如果现在又有个页面叫做Page2,又要再写类似的判断在Page2里面。再者如果需求改变成:"当用户没有权限的时候,按钮设定为disabled"。这样又要把分散再各页面的权限管理逻辑,通通修改一变,如:

class Page1 extend React.Component {
    render() {
        const {create, update} = window.globals.permissions;
        return (
            {<Button disabled={!create}>Create Project</Button>}
            {<Button disabled={!update}>Update Project</Button>}
        );
        
    }
}

这显然不是一个优雅的解法。其实用户是否有某按钮的权限,只需要关心權限點的名稱,能不能这样写?

class Page1 extend React.Component {
    render() {
        return (
            {<Button auth="create">Create Project</Button>}
            {<Button auth="update">Update Project</Button>}
        );        
    }
}

在按钮的地方,只要指定权限点名称就好了。要判断window.globals.permissions和是否隐藏/disabled就交由包装器统一处理即可。接下来我们看一下包装器的部份:

export default function injectAuth(component) {
    const checkAuth = (props) => {
      let {auth} = props;
      // No Auth required
      if(auth == null) {
        return true;
      }
      return windows.globals.permissions[auth];      
    }

    let WrappedClass = class extends component {

      render() {
        if(checkAuth(this.props)) {
          return super.render();
        }
        else {
          const noAuthType = this.props.noAuthType || 'hidden';
          if(noAuthType === 'hidden') {
            return null;
          } else if(noAuthType === 'disabled') {
            // React doesn't allow to modify props like this.prpos.disabled=ture
            // Should clone the element to with inital props
            // See: http://stackoverflow.com/questions/32370994/how-to-pass-props-to-this-props-children
            return React.cloneElement(super.render(), {disabled:true});
          } else {
             throw new Error('noAuthType必须是hidden或disabled');
          }

        }
      }
    }

    return WrappedClass;
}

injectAuth(component)方法的入参是组件的Class,这里的例子是Button。injectAuth将产生了一个新的Class,这个Class继承了Button Class,所以原本Button组件的行为都会被保留下来。

内部再调用checkAuth方法,检查此外部传进来的auth property是否在windows.globals.permissions是存在的。如果权限检查通过,就直接返回原本Button的render结果:

return super.render();

如果权限检查不通过,則根据noAuthType来决定是要隐藏或者disabled组件。

OK, 有了这个injector后,在我们可以在组件本身都统一置入这样的功能:

class Button { 
    ...
}
export default injectAuth(Button);

这样在个页面使用组件的时候,就不用一一的去写injectAuth的带代码了。

目录
相关文章
|
前端开发 开发者
探索前端框架的新趋势:React Hooks的应用与实践
本文将深入探讨前端开发中的新趋势,重点介绍React Hooks的应用与实践。通过学习和使用React Hooks,开发者可以更高效地构建可维护、可扩展的前端应用程序。本文将详细介绍React Hooks的原理、优势以及如何在实际项目中运用Hooks来提高开发效率并改善代码结构。无论你是刚入门前端开发还是经验丰富的工程师,本文都将对你有所启发。
|
前端开发 API 数据安全/隐私保护
【第45期】一文解决React项目的权限管理
【第45期】一文解决React项目的权限管理
773 0
|
开发框架 自然语言处理 前端开发
【第25期】一文读懂React企业级前端应用框架Umi
【第25期】一文读懂React企业级前端应用框架Umi
1360 0
|
前端开发 JavaScript API
React团队回应用Vite替换Create React App的建议
React团队回应用Vite替换Create React App的建议
706 0
|
前端开发 安全 Swift
【教程】React Native 应用中的代码混淆与安全性管理
【教程】React Native 应用中的代码混淆与安全性管理
462 0
|
前端开发 IDE 小程序
【社区每周】React Native 初探;应用中支持添加应用管理员(2月第一期)
【社区每周】React Native 初探;应用中支持添加应用管理员(2月第一期)
256 11
|
前端开发 JavaScript 安全
使用React、TypeScript和Ant Design构建现代化前端应用
使用React、TypeScript和Ant Design构建现代化前端应用
1014 0
|
前端开发 JavaScript
React 16.8 新特性:让你的应用更出色(下)
React 16.8 新特性:让你的应用更出色(下)
React 16.8 新特性:让你的应用更出色(下)
|
缓存 前端开发 JavaScript
React 16.8 新特性:让你的应用更出色(上)
React 16.8 新特性:让你的应用更出色(上)
React 16.8 新特性:让你的应用更出色(上)
|
Shell 开发工具 git
(亲测好用)构建React-app应用时create-react-app卡住超慢的解决办法
(亲测好用)构建React-app应用时create-react-app卡住超慢的解决办法
1668 0
(亲测好用)构建React-app应用时create-react-app卡住超慢的解决办法