react-access-permission
Easy to control react component permissions.
Install
npm install -S react-access-permission
Usage
import permission from 'react-access-permission';// 1. Set user's permissions.permission; // 2. Add annotation on Component.@ { return <div> /* set dom element permission */ <div ="a">MyComponent</div> /* set component element permissions */ <SubComponent = /> </div> ; } // 3. Use the Component.;
Set User's permissions.
setGlobalPermissions()
Sync to set User's permissions, method receive number, string or array args.
;// numberpermission;// or stringpermission;// or string with dotpermission;// or arraypermission;
setGlobalPermissionsAsync()
Async to set User's permissions. method receive a Promise instance.
;// or even promisevar promise = { ;};permission;
Get User's permissions.
getGlobalPermissions()
Sync to get userPermissions data.
var userPermissions = permission;
getGlobalPermissionsAsync()
Async to get userPermissions data. If you use setGlobalPermissionsAsync() method to set user's permissions, you better to use this method to get userPermissions data.
permission;
Set Element's permissions.
It means User need to have these permissions which is elements set, then them will activate in this Component.
import permission from 'react-access-permission'; @ { return <div> /* use "data-permission" attribute for dom element */ <div ="1"> <h1>MyComponent</h1> <p ="2,3"> p1 <span>span1</span> </p> </div> /* use "permission" attribute for component element */ <SubComponent1 = /> <SubComponent2 ="2" /> <SubComponent3 ="3,4,5"> <SubComponent4 = /> <SubComponent5 = /> </SubComponent3> </div> ; }
Set Component's permissions.
It means User need to have these permissions to use this Components.
import permission from 'react-access-permission'; @ { return <div> ... </div> ; }
Component's permissions with denied callback.
@ ...
Set Function Component's permissions.
withPermission method is use for Function Component.
import setGlobalPermissions withPermission from 'react-access-permission'; ; var Permission = ; ;
Set Function Component's permission.
var Permission = ;
Function Component with denied callback.
var Permission = ;// orvar Permission = ;
Handle denied.
import permission setGlobalPermissions from 'react-access-permission'; ; @ { return <div> <p ="1">Hello</p> <p ="2">World</p> </div> ; }
Set Default options.
import permission from 'react-access-permission'; permission;
API
/** * @desc enable permission feature for Class Component. * @param { number | string | array } permissions set required permissions of component. * @param { function } onDenied when permission denied occur(under the Component Class), this method will be invoked everytime, function receive required permission, denied element and element index in parent children, return a replace element or nothing. */ /** * @desc set global permissions for user. * @param { number | string | array } permissions set user's permissions. */permission /** * @desc async to set global permissions for user. * @param { promise } promise a promise instance to async receive user's permissions. */permission /** * @desc sync to get user's permissions. * @return user's permissions. */permission /** * @desc async to get user's permissions. * @param { function } callback method receive a param that is userPermissions data. */permission /** * @desc * @param { object } options set default options. * @param { function } options.comparePermission custom compare function, receive(requiredPermissions, userPermissions). return true means authorized, false means denied. * @param { function } options.onDenied custom global onDenied, recieve(requiredPermissions, deniedElement, index). * @param { function } options.transformData will transform setGlobalPermissions's data,, default null. */permission /** * @desc enable permission feature for Function Component. * @param { function } WrappedComponent Function Component. * @param { number | string | array } permissions set required permissions of Function Component. * @param { function } onDenied when permission denied occur(under the Function Component), this method will be invoked everytime, function receive required permission, denied element and element index in parent children, return a replace element or nothing. */