Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
m03geek committed May 31, 2019
0 parents commit 07e2700
Show file tree
Hide file tree
Showing 20 changed files with 12,342 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
tab_width = 2

# Indentation override for all JS under lib directory
[*.js]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 100
8 changes: 8 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
logs/
coverage/
node_modules/
.idea/
.vscode/
static/
**/*.xxx.*
dist/
31 changes: 31 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
extends: ['eslint:recommended', 'google'],
parserOptions: {
ecmaVersion: 2018,
},
env: {
es6: true,
node: true,
jest: false,
},
rules: {
'new-cap': ['error', {capIsNewExceptions: ['ObjectId', 'Fastify']}],
'max-len': [
'error',
{
code: 80,
comments: 999,
ignoreComments: true,
ignoreStrings: true,
ignoreTrailingComments: true,
ignoreUrls: true,
ignoreTemplateLiterals: true,
},
],
indent: ['error', 2, {SwitchCase: 1}],
'spaced-comment': ['error', 'always', {markers: ['/']}],
'no-console': 'warn',
'valid-jsdoc': 'off',
'require-jsdoc': 'off',
},
};
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.idea
node_modules
deploy
build
coverage
config/local.*
**/*.xxx.js
dist/
63 changes: 63 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
image: node:latest

cache:
key: "$CI_BUILD_REF_NAME"
untracked: true
paths:
- node_modules/

stages:
- verify
- test
- publish

codestyle:
stage: verify
script:
- npm ci
- npm run lint
# - npm audit
- npm run build

node:8:
stage: test
image: node:8
only:
- /^v\d+\.\d+\.\d+(-rc.\d+|)$/
script:
- npm i
- npm run unit
coverage: /^All files[^|]*\|[^|]*\s+([\d\.]+)/

node:10:
stage: test
image: node:10
only:
- /^v\d+\.\d+\.\d+(-rc.\d+|)$/
script:
- npm i
- npm run unit
coverage: /^All files[^|]*\|[^|]*\s+([\d\.]+)/

node:latest:
stage: test
image: node:latest
script:
- npm i
- npm run unit
coverage: /^All files[^|]*\|[^|]*\s+([\d\.]+)/

npm:
stage: publish
script:
- VERSION_TAG=$([[ $CI_COMMIT_TAG == *"-rc"* ]] && echo "next" || echo "latest")
- echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > .npmrc
- npm ci
- npm run build
- npm publish --tag $VERSION_TAG
environment:
name: npm
url: https://www.npmjs.com/package/${CI_PROJECT_NAME}
when: manual
only:
- /^v\d+\.\d+\.\d+(-rc.\d+|)$/
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.vscode
test
tsconfig.json
coverage
.*
*.config.js
docs/
src/
example/
Empty file added CHANGELOG.md
Empty file.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 m03geek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
135 changes: 135 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# fast-rbac

[![NPM Version](https://img.shields.io/npm/v/fast-rbac.svg)](https://www.npmjs.com/package/fast-rbac)
[![Downloads Count](https://img.shields.io/npm/dm/fast-rbac.svg)](https://www.npmjs.com/package/fast-rbac)
[![Vunerabilities Count](https://snyk.io/test/npm/fast-rbac/badge.svg)](https://www.npmjs.com/package/fast-rbac)
[![Build Status](https://gitlab.com/m03geek/fast-rbac/badges/master/pipeline.svg)](https://gitlab.com/m03geek/fast-rbac/commits/master)
[![License](https://img.shields.io/npm/l/fast-rbac.svg)](https://gitlab.com/m03geek/fast-rbac/blob/master/LICENSE)
[![Coverage Status](https://gitlab.com/m03geek/fast-rbac/badges/master/coverage.svg)](https://gitlab.com/m03geek/fast-rbac/commits/master)

Implementation of RBAC module tuned to be fast

Main rules:

* No RegEx
* O(1) time complexity for checking rules
* Wildcard and inherited rules caching
* No foolproof checks
* Zero dependency

## ToC
- [fast-rbac](#fast-rbac)
- [ToC](#toc)
- [Installation](#installation)
- [Features and requirements](#features-and-requirements)
- [Usage](#usage)
- [ToDo List](#todo-list)
- [Docs](#docs)
- [Changelog](#changelog)
- [Repos info](#repos-info)
- [See also](#see-also)
- [License](#license)

## Installation

```sh
npm i fast-rbac --save
```

<sub>[Back to top](#toc)</sub>

## Features and requirements

* Wildcard rules support
* Inheritance support
* Typescript support
* Prebuilt browser amd and system modules

---

* Node.js `>=8.0.0`.

<sub>[Back to top](#toc)</sub>

## Usage

** NOTE **: No cyclic roles interherence. You've been warned.

`can` method returns `boolean` if 3 arguments are passed to it.
It could return `Promise` if rule has `when` function and `context` is passed.
Even if your `when` function doesn't consume any arguments you need to pass context (e.g. `null` or `{}`) in order to execute it.
Whithout `context` the function `when` will be not executed and `can` will return `true`.

```js
import RBAC from 'fast-rbac';
const a = new RBAC({
roles: {
user: {can: ['cat:create', 'dog:*', {name: 'foo', operation: 'read'}]},
prouser: {can: ['cat:update'], inherits: ['user', 'reader']},
admin: {can: ['*']},
reader: {can: ['*:read'], inherits: ['anon']},
anon: {
can: [
{
name: '*:read',
when: (ctx) => {
const result: Promise<boolean> = new Promise((resolve) => {
resolve(ctx.color === 'red');
});
return result;
},
},
],
},
},
});

console.log(a.can('user', 'dog', 'read'));

(async () => {
console.log(await a.can('anon', 'cat', 'read', {color: 'red'}));
})()
```

<sub>[Back to top](#toc)</sub>

## ToDo List

PRs welcome!

- [ ] Add/modify/delete roles in runtime
- [ ] Add some unit tests
- [ ] Deal with circular role interherence (but I'm totally ok with callstack error)

## Docs

See [docs](docs/README.md).

<sub>[Back to top](#toc)</sub>

## Changelog

See [changelog](CHANGELOG.md).

<sub>[Back to top](#toc)</sub>

## Repos info

* **Main** https://gitlab.com/m03geek/fast-rbac - preferred place for issues and PRs
* **Mirror** https://github.com/SkeLLLa/fast-rbac - only PRs accepted

<sub>[Back to top](#toc)</sub>

## See also

* [rbac](https://www.npmjs.com/package/rbac)
* [@rbac/rbac](https://www.npmjs.com/package/@rbac/rbac)
* [easy-rbac](https://www.npmjs.com/package/easy-rbac)

<sub>[Back to top](#toc)</sub>

## License

Licensed under [MIT](./LICENSE).

<sub>[Back to top](#toc)</sub>
24 changes: 24 additions & 0 deletions browser.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"moduleResolution": "node",
"newLine": "LF",
"noErrorTruncation": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "dist/browser",
"paths": {
"*": ["node_modules/*"]
},
"resolveJsonModule": false,
"sourceMap": true,
"strictNullChecks": true,
"lib": ["es6", "dom", "es2017.object"]
},
"include": ["src/**/*"],
"exclude": ["dist/**/*", "coverage/**/*", "node_modules/**/*"]
}
24 changes: 24 additions & 0 deletions example/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import RBAC from '../src';
const a = new RBAC({
roles: {
user: {can: ['cat:create', 'dog:*', {name: 'foo', operation: 'read'}]},
prouser: {can: ['cat:update'], inherits: ['user', 'reader']},
admin: {can: ['*']},
reader: {can: ['*:read'], inherits: ['anon']},
anon: {
can: [
{
name: '*:read',
when: (ctx) => {
const result: Promise<boolean> = new Promise((resolve) => {
resolve(ctx.color === 'red');
});
return result;
},
},
],
},
},
});

console.log(a.can('user', 'dog', 'read'));
19 changes: 19 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
collectCoverage: true,
collectCoverageFrom: ['src/**/*.(ts|js)'],
coverageReporters: ['lcov', 'text'],
globals: {
'ts-jest': {
tsConfig: 'tsconfig.json',
},
},
moduleFileExtensions: ['ts', 'js'],
testEnvironment: 'node',
testMatch: ['**/test/**/*.spec.(ts|js)'],
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
verbose: true,
preset: 'ts-jest',
};
Loading

0 comments on commit 07e2700

Please sign in to comment.