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

rs-can

v0.0.2

Published

authorization library

Downloads

3

Readme

rs-can

rs-can is an authentication library. It allows to build access control list. And then test access control.

How to use

import AccessControl from "rs-can";

// set up access control list

const accessControl = new AccessControl();
// allow user can manage its own post
accessControl.allow(User, "manage", Post, (user: User, post: Post) => user.id === post.userId);
// condition with extra params,
// allow user to apply for the ad if  
accessControl.allow(User, "apply", Ad,
  (user: User, ad: Ad, application: AdApplication) => {
    const isClient = user.role === ROLES.CLIENT;
    const notOwner = user.id !== ad.userId;
    const notApplied = !application;
    const adActive = ad.active;

    return isClient && notOwner && notApplied && adActive;
  }
);
accessControl.allow(UnauthenticatedUser, "viewAd");

// check access 

const currentUser = new User({id: 123});
const post = new Post({user_id: 123});
accessControl.can(currentUser, "manage", post); // true

const ad = new Ad({active: true, user_id: 123});
const application = undefined;
const client = new User({id: 124, role: ROLES.CLIENT});
accessControl.can(currentUser, "apply", ad, application); // true  

API

ActionControl has

  • allow
  • can

access.allow Set up access control. Everything which is not allowed is denied. allow method allows a perform to perform action on a target.


accessControl.allow(
    performer: Type<any>,
    action: string,
    target?: Type<any>,
    condition?: CheckCondition
): void;

export interface Type<T> extends Function {
    new (...args: any[]): T;
}

export type CheckCondition = (
    performer: any,
    target: any,
    options?: any
) => boolean;
  • performer - required, class name(User, Admin, etc). If you want to test access for a guest user that couldn't be initiated with User object - add new dummy class GuestUser and pass it as a performer argument.
  • action - required, string action name(update, manage, create)
  • target - optional, target class name. Can user update {target}? (Post, Settings, etc)
  • condition? - optional condition function. If it's provided condition will be invoked with performer object, target object and optional options.

access.can Checks the control

accessControl.can(
    performer: any,
    action: string,
    target?: any,
    options?: any
) => boolean;
  • performer - required. The performer object - new User, new Admin. Usually it's currently logged in user.
  • action - required, action name(update, manage, create)
  • target - optional. Object of the taget class - new Post, new Settings. Can be omitted when accessContol.allow(User, "createPosts")
  • options - optional options. For example, you want to check if user can apply for the ad and you can pass extra object adApplication as additional data.