UNPKG

react-rbac-lib

Version:

A tiny useful library to integrate role based access control for react (Typescript)

117 lines (99 loc) 4.04 kB
# react-rbac-lib [![npm install][install-img]][npm-url] <br> [![npm version][npm-version]][npm-url] [![license][license-url]][npm-url] [![Known Vulnerabilities][vulner-url]][npm-url] [![Library][tiny-lib]][npm-url] A tiny useful library to integrate role based access control for react (Typescript) Installation ------------ To use in your projects: ```sh npm install react-rbac-lib --save ``` Usage ----- ```ts import RBAC from "react-rbac-lib"; import _ from "lodash"; let userData: any; try { const localStorageUserData: any = localStorage.getItem("user"); userData = JSON.parse(localStorageUserData || "{}"); } catch (error) const abilities = RBAC.hasAccessFor("member", _.get(userData, "data.role", {}) //eg.Method for user access check const checkUsrAccess = () => { return ( abilities.can('create', 'userRoleManagement.userAccessPermissions.roleAccess_1') || //returns true abilities.can('view', 'userRoleManagement.userAccessPermissions.roleAccess_1') || //returns false abilities.can('update', 'userRoleManagement.userAccessPermissions.roleAccess_1') || //returns true abilities.can('delete', 'userRoleManagement.userAccessPermissions.roleAccess_1') || //returns false abilities.can('create', 'userRoleManagement.userAccessPermissions.roleAccess_2') || //returns false abilities.can('edit', 'userRoleManagement.userAccessPermissions.roleAccess_2') //returns false ); }; return ( checkUsrAccess() && (<ComponentsAccordingToTheUserAccess/>) ); ``` Sample json data ```json { "data": { ".....": { ..... ..... }, "role": { "isSuperAdminPermission": false, // [true - overrides role checks to true.] // [false - checks individual role permissions] "userRoleManagement": { "userAccessPermissions": { "roleAccess_1": { "create": true, "view": false, "update": true, "delete": false, }, "roleAccess_2": { "create": false, "edit": false, ...... ...... } }, ........ ........ }, ........... ........... } }, } ``` Author ------ [**Sreekanth T U**](https://moistrona.in/) <br/> [![Follow on Facebook][facebook-follow-img]][facebook-follow-url] [![Follow on LinkedIn][linkedin-follow-img]][linkedin-follow-url] [![Follow on Twitter][twitter-follow-img]][twitter-follow-url] License ------- MIT License. See [LICENSE.md](LICENSE.md) for details. [npm-url]: https://www.npmjs.com/package/react-rbac-lib [npm-version]: https://img.shields.io/npm/v/react-rbac-lib [install-img]: https://nodei.co/npm/react-rbac-lib.png?compact=true [license-url]: https://img.shields.io/npm/l/react-rbac-lib [vulner-url]: https://img.shields.io/snyk/vulnerabilities/npm/react-rbac-lib [tiny-lib]: https://img.shields.io/badge/Tiny%20Lib-rbac%20library-brightgreen [twitter-follow-url]: https://twitter.com/Sreekanth_TU [twitter-follow-img]: https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Ftwitter.com%2FSreekanth_TU [facebook-follow-url]: https://www.facebook.com/sreekanth.tu [facebook-follow-img]: https://img.shields.io/badge/FB-Follow-blue [linkedin-follow-url]: https://www.linkedin.com/in/sreekanth-unnikrishnan-aa15b4111/ [linkedin-follow-img]: https://img.shields.io/badge/in-Follow-informational [github-follow-url]: https://github.com/Sreekanth-Unnikrishnan [github-follow-img]: https://img.shields.io/github/followers/Sreekanth-Unnikrishnan?label=follow&style=social