npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-casl

v0.1.0

Published

Ember addon for CASL which makes it easy to add permissions in any Ember application

Downloads

3

Readme

ember-casl

Ember addon for CASL which makes it easy to add permissions in any Ember application.

This package allows to integrate @casl/ability with Ember application.

It provides:

  • Ability service that allows you to define CASL ability compatible with Ember tracked properties.
  • Can/Cannot component that allow to hide or show UI elements based on user ability to see them.

Compatibility

  • Ember.js v3.20 or above
  • Ember CLI v3.20 or above
  • Node.js v10 or above
  • ember-auto-import v2.0 or above

Installation

ember install ember-casl

Usage

Ability Service

Define ability like regular Ember services by extending AbilityService.

// services/post-ability.js
import { tracked } from '@glimmer/tracking';
import AbilityService from 'ember-casl/services/ability';

export default class PostAbility extends AbilityService {
  @tracked isWriter = false;

  get definition() {
    return ({ can, cannot }) => {
      can('read', 'post');
      if (this.isWriter) {
        can('write', 'post');
      } else {
        cannot('write', 'post').because("You're not a writer");
      }
    };
  }
}

The definition getter should return a function taking destructured AbilityBuilder as argument (build excluded).

It allows you to define very complex reactive ability (eg. Ability based on tracked property or another service).

Your newly created Ability service expose some fields

| Name | Type | Description | | ----------------- | ------------ | -------------------------------------------------------------------------------------------------------------- | | ability | CASL Ability | Original CASL Ability | | can | Method | Wrapper on CASL Ability method can | | cannot | Method | Wrapper on CASL Ability method cannot | | relevantRuleFor | Method | Wrapper on CASL Ability method relevantRuleFor |

Then you can use those abilities as regular services in your Ember application.

// components/dummy.js
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';

export default class Dummy Component extends Component {
  @service postAbility;

  get canRead() {
    return this.postAbility.can('read', 'post');
  }

  get cannotWriteReason() {
    return this.postAbility.relevantRuleFor('read', 'post').reason;
  }
}

Can / Cannot Helpers

You can use can & cannot helpers in your templates.

{{#if (can 'read' 'post' service='post-ability')}}
  <span>You're a reader</span>
{{/if}}
{{#if (cannot 'write' 'post' service='post-ability')}}
  <span>You're not a writer</span>
{{/if}}

Positional params are CASL Ability can / cannot arguments. service is the name of the Ember service.

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.