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

admin-bro-users-permissions

v1.0.4

Published

Library to easily implement a Role-Based Access Control(RBAC). Highly extensible, this library will help you to have an Admin Login Page and inside the admin, 2 Resources: Users and Roles. **Only for Mongoose**

Downloads

6

Readme

Admin Bro: Users & Permisions

Library to easily implement a Role-Based Access Control(RBAC). Highly extensible, this library will help you to have an Admin Login Page and inside the admin, 2 Resources: Users and Roles. Only for Mongoose

How it looks

Getting Started

npm install admin-bro-users-permissions
npm install cookie-parser
npm install express-session

Import the resources and the authentication closure

const userResource = require('admin-bro-user-permissions/resources/user')
const roleResource = require('admin-bro-user-permissions/resources/role')
const { authenticationClosure } = require('admin-bro-user-permissions/authentication')
const isAccessGranted = require('admin-bro-user-permissions/policies/isAccessGranted')

Set the resources to the AdminBro. eg:

const storeResource = {
    resource: mongoose.model('Store', {
        name: { type: String, required: true },
    }),
    options: {
        actions: {
            list: { // Added the role policy
                isAccessible: isAccessGranted({ resourceName: 'Store', actionRequested: 'list' }),
            },
            edit: { 
                isAccessible: isAccessGranted({ resourceName: 'Store', actionRequested: 'list' }),
            },
            //...etc
        },
    }
}

const adminBro = new AdminBro({
        resources: [
            ...storeResource,
            userResource.initResource(mongoose, {
                resourceSchema: {
                    name: { type: String, required: true }, //optional
                    ...yourSchema
                },
                resourceOptions: {
                    parent: {
                        name: 'Access'
                    },
                },
            }),
            roleResource.initResource(mongoose, {
                resourceOptions: {
                    parent: {
                        name: 'Access'
                    },
                },
            }),
        ],
    })

Build the authentication route passing the authentication closure:

const router = AdminBroExpress.buildAuthenticatedRouter(adminBro, {
    authenticate: authenticationClosure({ userModel: userResource.getModel(mongoose), roleModel: roleResource.getModel(mongoose) }),
})

Enable cookie-parsers and express-session into your express app:

app.use(cookieParser('secret'))
app.use(cookieSession())

Run migrations to create a Role and User in your database:

node ./node_modules/.bin/admin-bro-users-permissions-migrations --connectionString="mongodb://localhost:27017/yourDataBase"

You must receive the email and password to login in the panel

Done :white_check_mark::tada::tada: You now can start the AdminBro and you will see the login page and the Users & Roles resource.

Highly extensible

You have access to all the pieces which is building this library. With that, you can extend or even create your on pieces to overwrite the main one.

Resources Methods

Method | Parameters ------ | ------- initResource | mongoose, Object{resourceSchema: Mongoose Object Schema, resourceOptions: ResourceOptions, resourceFeatures: Array of Features} Returns: Array<AdminBro Resources> getSchema | mongoose, Object{Mongoose Object Schema} Returns: Mongoose Schema getOptions | Object{ResourceOptions} Returns: Object of ResourceOptions getFeatures | Array<Features> Returns: Array of AdminBro Features

Authentication

Method | Parameters ------ | ------- authenticationClosure | Object({ userModel: User Mongoose Model, roleModel: Role Mongoose Model }) Returns: Function<authentication(email, password)> authentication | (email, password) Returns: False or Object({email: String, password: String, role: Object})

Lets suppose besides all the login validations, you want to extend and add your own. You could do it using the authentication method. eg:

const userResource = require('admin-bro-user-permissions/resources/user')
const roleResource = require('admin-bro-user-permissions/resources/role')
const { authentication } = require('admin-bro-user-permissions/authentication')

const authenticationClosure = () => {
    return async (email, password) => {
        const matched = await authentication(email,password, userResource.getModel(mongoose), roleResource.getModel(mongoose))

        // Add your business logic here
        // return true or false
    }
}

const router = AdminBroExpress.buildAuthenticatedRouter(adminBro, {
    authenticate: authenticationClosure,
})
Policies

Policy | Parameters ------ | ------- isAccessGranted | Object({resourceName: String, actionRequested: String})

You can also add business logic to policy. eg:

const isAccessGranted = require('admin-bro-user-permissions/policies/isAccessGranted')

const myPolicy = ({ currentAdmin }) => {
    const isAccessGrantedClosure = isAccessGranted({ resourceName: 'Store', actionRequested: 'list' })
    const isGranted = isAccessGrantedClosure({currentAdmin})

    // add your business logic
    // return true or false
}

const storeResource = {
    resource: mongoose.model('Store', {
        name: { type: String, required: true },
    }),
    options: {
        actions: {
            list: { // Added the role policy
                isAccessible: isAccessGranted({ resourceName: 'Store', actionRequested: 'list' }),
            },
            //...etc
        },
    }
}

Contribution

If you need features that is not implemented - feel free to implement and create PRs! Plus we need some documentation, so if you are good in it - you are welcome.