Git Product home page Git Product logo

react-authorization's Introduction

React Authorization Library

Declarative authorization API for the UI. Allows declarative description of what should and should not be displayed based on the user's role(s), or an authorization function result.

Usage

<IfGranted expected='ROLE_ADMIN' actual={user.getRoles()}>
    <div className="panel">
        Child with restricted access.
    </div>
</IfGranted>

Displays the child div only if the user.getRoles() result contains the role ROLE_ADMIN.

<IfAllGranted expected={['ROLE_USER', 'ROLE_ADMIN']} actual={user.getRoles()} unauthorized={<h3>You shall not pass!</h3>}>
    <div className="panel">
        Child with restricted access.
    </div>
</IfAllGranted>

Displays the child div only if the user.getRoles() contains both ROLE_USER and ROLE_ADMIN. If not, a heading saying You shall not pass! is displayed. Specifying the node to display when expected roles are not met is optional ( see the API section).

<IfAuthorized isAuthorized={() => user.getRoles().indexOf('ROLE_ADMIN') !== -1}>
    <div className="panel">
        Child with restricted access.
    </div>
</IfAuthorized>

Displays the child div only if the specified authorization function returns a truthy value.

<IfAuthorized isAuthorized={hasAccess(AccessLevel.WRITE, AccessLevel.READ)}>
    <div className="panel">
        Child with restricted access.
    </div>
</IfAuthorized>

Displays the child div if the hasAccess function (first parameter being required access level, second the actual granted) returned true. This is basically equivalent to doing

{hasAccess(AccessLevel.WRITE, AccessLevel.READ) && <div className="panel">
  Child with restricted access.
</div>}

You be the judge of which is nicer.

Rendering

The library is using React Fragments to render the children directly without any wrapper element. For example:

<IfAnyGranted expected={['ROLE_USER', 'ROLE_ADMIN']} actual={user.getRoles()}>
    <div className="panel">
        Child with restricted access.
    </div>
    <div>
        And another one.
    </div>
</IfAnyGranted>

Will be rendered directly:

<div class="panel">
    Child with restricted access.
</div>
<div>
    And another one.
</div>

Versions prior to 0.1.0 used a wrapper element (a div by default, but could be overridden).

API

Supported components:

  • IfAllGranted - requires all of the expected roles to be granted,
  • IfAnyGranted - requires at least one of the expected roles to be granted,
  • IfGranted - shorthand for expecting one role only - corresponds to using IfAnyGranted with exactly one role expected,
  • IfNoneGranted - requires that none of the expected roles is granted (e.g., role guest must not be set for editing access),
  • IfAuthorized - invokes the specified authorization function and renders children only if it returns a truthy value ( since 0.3.0).

API of the respective components is described below.

IfAllGranted

Displays the children if and only if all of the expected roles are granted.

Property Type Required Default value Explanation
expected Array true An array of roles required to display the children.
actual String/Array false [] An array of actually granted roles, or a single role (shorthand for an array with one element).
unauthorized Node false null Node to display when the actual roles do not meet the expectations. Defaults to null, which displays nothing.

IfAnyGranted

Displays the children if at least one of the expected roles is granted.

Property Type Required Default value Explanation
expected Array true An array of roles required to display the children.
actual String/Array false [] An array of actually granted roles, or a single role (shorthand for an array with one element).
unauthorized Node false null Node to display when the actual roles do not meet the expectations. Defaults to null, which displays nothing.

IfGranted

Displays the children if the expected role is granted.

Property Type Required Default value Explanation
expected String true The role required to display the children.
actual String/Array false [] An array of actually granted roles, or a single role (shorthand for an array with one element).
unauthorized Node false null Node to display when the actual roles do not meet the expectations. Defaults to null, which displays nothing.

IfNoneGranted

Displays the children if none of the expected roles is granted. Useful, for example, to prevent display of editing components to guests or otherwise restricted users.

Property Type Required Default value Explanation
expected String/Array true An array of roles (or a single role) which must not be present to display children.
actual String/Array false [] An array of actually granted roles, or a single role (shorthand for an array with one element).
unauthorized Node false null Node to display when the actual roles do not meet the expectations. Defaults to null, which displays nothing.

IfAuthorized

Displays the children if the provided authorization function returns a truthy value or if the provided boolean value is true. Useful for more complex authorization logic which should still be declaratively used.

Property Type Required Default value Explanation
isAuthorized Function/boolean false An authorization function with signature () => boolean or a boolean. Defaults to undefined, which is equivalent to false.
unauthorized Node false null Node to display when the authorization function returns a falsy value. Defaults to null, which displays nothing.

Installation

Install with npm using

npm install --save react-authorization

License

MIT

react-authorization's People

Contributors

dependabot[bot] avatar ledsoft avatar narender56 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-authorization's Issues

Dealing with the "Property 'children' does not exist on type" error when upgrading to React 18

It seems that updating the React version to 18 is leading to some typescript errors in the code. Specifically, it is now necessary to define the "children" property on components, which was not required before.

React version 18 likely includes changes or updates to the way the library handles TypeScript, resulting in a stricter set of requirements for component definition. To prevent errors and ensure the code functions properly, developers must now explicitly define the "children" property on components, which represents the content nested within the component element.

Failing to define "children" in components can lead to TypeScript errors, as the compiler will flag any attempts to use or modify the property. Therefore, it is essential for developers to adjust their code to account for this change when upgrading to React version 18.

image

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.