question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

<Can /> or condition

See original GitHub issue

Hi,

Versions used :

"@casl/ability": "^3.4.0",
"@casl/react": "^1.0.4",

New to casl React, I’ve set it up with my permissions model and rest services. I’ve used it on a first component successfully :

 <Can I="create" a="product">
        <Button variant="contained" className={classes.button} size="small" onClick={this.startCreation} data-cy="create-user-button">
              <AddIcon classes={{root: classes.iconThemeColorGreen}} className={classes.leftIcon} />
                   Create {this.componentName}
          </Button>
 </Can>

But now, I have a case where user can go on a product with one button and permission Read OR Edit.

How can I make it using usefull component <Can /> and without having to write a multiple test with ability.can || ability.can … and create a specific variable injected in my JSX afterwards ?

Thanks,

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jkeruzeccommented, May 4, 2021

I have check and I did both world : it was a long time ago …

In ability:

  /**
     * Wrapper for simple ability can API
     * 
     */
    can(I, object) {
        if(Array.isArray(I) && I.length > 0) {
            return I.some(perm => {
             return this.ability.can(perm, object);
            });
        } 
        return this.ability.can(I, object);
    }
    
    /**
     * Wrapper for simple ability cannot API
     * 
     */
    cannot(I, object) {
        return !this.can(I, object);
    }

Use it : abilityServices.can([SecurityConst.permissionType.edit], this.props.form.formObject)

OR

import { createCanBoundTo } from "@casl/react"
import abilityServices from "../../../services/security/AbilityServices"

export default createCanBoundTo(abilityServices.ability)

Then //Security import Can from '../../../../components/ui/security/CanControl'

and finally: <Can I={SecurityConst.permissionType.create} a={SecurityConst.permission.toolAdministration.module + "_" + SecurityConst.permission.toolAdministration.objects.user}>

0reactions
lasithagurugecommented, May 5, 2021

I have check and I did both world : it was a long time ago …

In ability:

  /**
     * Wrapper for simple ability can API
     * 
     */
    can(I, object) {
        if(Array.isArray(I) && I.length > 0) {
            return I.some(perm => {
             return this.ability.can(perm, object);
            });
        } 
        return this.ability.can(I, object);
    }
    
    /**
     * Wrapper for simple ability cannot API
     * 
     */
    cannot(I, object) {
        return !this.can(I, object);
    }

Use it : abilityServices.can([SecurityConst.permissionType.edit], this.props.form.formObject)

OR

import { createCanBoundTo } from "@casl/react"
import abilityServices from "../../../services/security/AbilityServices"

export default createCanBoundTo(abilityServices.ability)

Then //Security import Can from '../../../../components/ui/security/CanControl'

and finally: <Can I={SecurityConst.permissionType.create} a={SecurityConst.permission.toolAdministration.module + "_" + SecurityConst.permission.toolAdministration.objects.user}>

Thank you for your quick reply and sharing this. This is great! Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

English verb conjugation CAN CONDITION
English verb conjugation can condition to the masculine with a modal can. Regular verb: condition - conditioned - conditioned.
Read more >
SQL: Combining the AND and OR Conditions
The SQL AND condition and OR condition can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement....
Read more >
Using IF with AND, OR and NOT functions
The AND and OR functions can support up to 255 individual conditions, but it's not good practice to use more than a few...
Read more >
Conditionals | Grammar | EnglishClub
The word "condition" means "situation or circumstance". If a particular condition is true, then a particular result happens:
Read more >
Conditionals: other expressions ( unless, should, as long as )
Conditional clauses can begin with unless. Unless means something similar to 'if … not' or 'except if'. The verb forms in the examples...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found