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.

[Question] Get permission from AbilityContext to use it in state or as prop

See original GitHub issue

I want to set a form to readonly by wrapping it in a <fieldset disable={someState}> tag.

Now I wonder if I can use the AbilityContext which already holds the correct Ability instance to check if for a specific user the form should be en- or disabled. For sure I can create a new Ability object based on the permissions stored for the logged in user in the class component that contains the form but this would somehow mean to duplicate the Ability instance.

Here is a short code snippet which visualizes the described scenario in the first line of the render function:

import React from 'react';
import { Form, Input } from 'antd';
import PropTypes from 'prop-types';

class WrappedEditor extends React.Component {
  constructor( props ) {
    super(props);

    this.state = {
      editable: false,
    };
  }

  render() {
    const { getFieldDecorator } = this.props.form;
    return (
      // TODO: How can I use the AbilityContext to set fileset :: disabled to a dynamic permission based value?
        <fieldset disabled={!this.state.editable}>
          <Form id="editor">
            {getFieldDecorator('someText', {
              rules: [{ required: true, message: 'Please enter a text', initialValue: this.props.editableObject.someText }],
            })}
            <Form.Item label="Some Text">
              <Input/>
            </Form.Item>
          </Form>
        </fieldset>
    );
  }
}

WrappedEditor.propTypes = {
  form: PropTypes.object.isRequired,
  editableObject: PropTypes.object.required,
};

const Editor = Form.create()(WrappedEditor);
export {Editor};

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
gibeliumcommented, May 12, 2019

Thank you for thinking along. The missing link in my brain was about the programatically access to the AbilityContext within a component. I now defined the static ContextType within my component and then can access the ability with this.context.ability.

class WrappedEditor extends React.Component {

...

  render() {
    const { getFieldDecorator } = this.props.form;
    const editorDisabled = !this.context.can('access', 'myEditor');

    return (
      // TODO: How can I use the AbilityContext to set fileset :: disabled to a dynamic permission based value?
      <fieldset disabled={editorDisabled}>
        <Form id="editor">
          {getFieldDecorator('someText', {
            rules: [{ required: true, message: 'Please enter a text', initialValue: this.props.editableObject.someText }],
          })}
          <Form.Item label="Some Text">
            <Input/>
          </Form.Item>
        </Form>
      </fieldset>
    );
  }
}

...

WrappedEditor.contextType = AbilityContext;
const Editor = Form.create()(WrappedEditor);
export {Editor};

I guess this is the way to go with the new Context API even though this.context is marked as deprecated in my IDE. Might this have something to do with createContextualCan returning a StatelessComponent which is also marked as deprecated?

0reactions
stalniycommented, May 14, 2019

Thanks for the hint. I will not update this until the next major release. Don’t want to break types for users who uses older version of react

Read more comments on GitHub >

github_iconTop Results From Across the Web

casl/react
Getting Started. This package provides Can component which can be used to conditionally show UI elements based on user abilities.
Read more >
CASL React
CASL (pronounced /ˈkæsəl/, like castle) is an isomorphic authorization JavaScript library which restricts what resources a given user is allowed to access.
Read more >
stalniy-casl/casl - Gitter
Hi guys does anyone have any pointers using '@casl/react' with redux? ... store={store}> <LocalizeProvider store={this.state.store}> <AbilityContext.
Read more >
Unexpected double redirect react Router v6
I am using react-router V6 for navigation and noticed strange behavior in navigation. When you go to a new page, the following happens:...
Read more >
Chapter 5 - Advanced Degree or Exceptional Ability
The beneficiary must have not only had the advanced degree or its equivalent on the date that the permanent labor certification application was...
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