# react-rbac-lib

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

Latest version **1.0.4** (published 2020-07-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install react-rbac-lib
pnpm add react-rbac-lib
yarn add react-rbac-lib
bun add react-rbac-lib
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2020-07-05 |
| First published | 2020-07-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 19.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Sreekanth T U |
| Maintainers | smussr |
| Keywords | RBAC, react-rbac, rbac library |

## Links

- npm: https://www.npmjs.com/package/react-rbac-lib
- Repository: https://github.com/Sreekanth-Unnikrishnan/react-rbac-lib
- Homepage: https://github.com/Sreekanth-Unnikrishnan/react-rbac-lib#readme
- Issues: https://github.com/Sreekanth-Unnikrishnan/react-rbac-lib/issues
- npm.io page: https://npm.io/package/react-rbac-lib

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [@casl/react](https://npm.io/package/@casl/react.md) ^2.1.1
- [@casl/ability](https://npm.io/package/@casl/ability.md) ^4.1.5

## Recent versions

- 1.0.4 (latest) — 2020-07-05
- 1.0.3 — 2020-07-05
- 1.0.2 — 2020-07-04
- 1.0.1 — 2020-07-04
- 1.0.0 — 2020-07-04

## README

# 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

---
_Source: https://npm.io/package/react-rbac-lib · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
