Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit b8ed7f9

Browse files
author
Dilip Kola
committed
Fix tslint issues
1 parent 5b146a0 commit b8ed7f9

25 files changed

+640
-626
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,18 @@ You can set/get grants any time:
314314
const ac = new AccessControl();
315315
ac.setGrants(grantsObject);
316316
console.log(ac.getGrants());
317+
// You can save ac.getGrants() to DB
318+
// Please note: User should be part of your code and wraps calls to User to table/collection.
319+
await User.save({permissions: JSON.stringify(acl.getGrants())});
320+
// Retrieve from DB
321+
const perms = await User.getBydId(userId);
322+
ac.setGrants(JSON.parse(user.permissions));
323+
324+
// if your DB supports storing JSON natively then you can use following code.
325+
await User.save({permissions: acl.getGrants()});
326+
// Retrieve from DB
327+
const perms = await User.getBydId(userId);
328+
ac.setGrants(user.permissions);
317329
```
318330

319331
### Extending Roles

app.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const AccessControl = require('./index');
2-
const matcher = require('matcher');
1+
const AccessControl = require("./index");
2+
const matcher = require("matcher");
33

4-
AccessControl.isAllowed = function(account, user, resource, action, context) {
4+
AccessControl.isAllowed = function (account, user, resource, action, context) {
55
const acl = new AccessControl(account.acl);
66
const permission = acl.permission({
77
resource,
@@ -12,7 +12,7 @@ AccessControl.isAllowed = function(account, user, resource, action, context) {
1212
return permission.granted;
1313
}
1414

15-
AccessControl.getAllowedAttributes = function(account, user, resource, action, context) {
15+
AccessControl.getAllowedAttributes = function (account, user, resource, action, context) {
1616
const acl = new AccessControl(account.acl);
1717
const permission = acl.permission({
1818
resource,
@@ -23,15 +23,15 @@ AccessControl.getAllowedAttributes = function(account, user, resource, action, c
2323
return permission.attributes;
2424
}
2525

26-
AccessControl.allowedResources = function(account, user) {
26+
AccessControl.allowedResources = function (account, user) {
2727
const acl = new AccessControl(account.acl);
2828
const permittedResources = acl.allowedResources({
2929
role: user.policy.roles
3030
});
3131
return matcher(Object.keys(account.permissions), permittedResources);
3232
}
3333

34-
AccessControl.allowedActions = function(account, user, resource) {
34+
AccessControl.allowedActions = function (account, user, resource) {
3535
const acl = new AccessControl(account.acl);
3636
const permittedActions = acl.allowedActions({
3737
role: user.policy.roles,
@@ -40,14 +40,14 @@ AccessControl.allowedActions = function(account, user, resource) {
4040
return matcher(account.permissions[resource], permittedActions);
4141
}
4242

43-
AccessControl.allowedCategories = function(account, user, resource, action) {
43+
AccessControl.allowedCategories = function (account, user, resource, action) {
4444
const acl = new AccessControl(account.acl);
4545
const permittedCategories = acl.permission({
4646
role: user.policy.roles,
47-
resource: 'category',
48-
action: 'search'
47+
resource: "category",
48+
action: "search"
4949
}).attributes;
50-
if (permittedCategories.length === 1 && permittedCategories[0] === '*') {
50+
if (permittedCategories.length === 1 && permittedCategories[0] === "*") {
5151
return permittedCategories;
5252
}
5353
return permittedCategories.filter((category => {

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var AC = require('./lib').AccessControl;
1+
var AC = require("./lib").AccessControl;
22
module.exports = AC;
33
// adding circular ref to allow easy importing in both ES5/6 and TS projects
44
module.exports.AccessControl = AC;

src/AccessControl.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ class AccessControl {
217217
/**
218218
* Get roles which allow this permission
219219
* @param {IQueryInfo} - permission query object we want to check
220-
*
221-
* @returns {String[]} - roles
220+
*
221+
* @returns {String[]} - roles
222222
*/
223223
allowingRoles(query: IQueryInfo) {
224224
return utils.getAllowingRoles(this._grants, query);
@@ -228,7 +228,7 @@ class AccessControl {
228228
/**
229229
* Get allowed actions of resource when conditions are skipped
230230
* @param {IQueryInfo} - permission query object we want to check
231-
*
231+
*
232232
* @returns {String[]} - actions
233233
*/
234234
allowedActions(query: IQueryInfo) {
@@ -307,7 +307,7 @@ class AccessControl {
307307
* var ac = new AccessControl(grants);
308308
* var permission = ac.permission({
309309
* role: "user",
310-
* action: "update,
310+
* action: "update",
311311
* resource: "profile"
312312
* });
313313
* permission.granted; // Boolean

src/conditions/AndCondition.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { IConditionFunction } from "./IConditionFunction";
2-
import { conditionEvaluator } from "./index";
3-
import {AccessControlError, ICondition} from '../core';
1+
import { IConditionFunction } from './IConditionFunction';
2+
import { conditionEvaluator } from './index';
3+
import { AccessControlError, ICondition } from '../core';
44
import utils from '../utils';
55

66
/**
77
* And condition
8-
*
8+
*
99
* @author Dilip Kola <dilip@tensult.com>
1010
*/
1111
export class AndCondition implements IConditionFunction {
@@ -19,16 +19,16 @@ export class AndCondition implements IConditionFunction {
1919
return false;
2020
}
2121

22-
if(utils.type(args) !== 'array' && utils.type(args) !== 'object') {
23-
throw new AccessControlError('AndCondition expects type of args to be array or object')
22+
if (utils.type(args) !== 'array' && utils.type(args) !== 'object') {
23+
throw new AccessControlError('AndCondition expects type of args to be array or object')
2424
}
2525

2626
const conditions = utils.toArray(args);
2727

2828
return conditions.every((condition) => {
2929
return conditionEvaluator(condition, context);
3030
});
31-
31+
3232
}
3333
}
3434

src/conditions/EqualsCondition.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { IConditionFunction } from "./IConditionFunction";
2-
import {AccessControlError} from '../core';
1+
import { IConditionFunction } from './IConditionFunction';
2+
import { AccessControlError } from '../core';
33
import utils from '../utils';
44

55
/**
66
* Equals condition
7-
*
7+
*
88
* @author Dilip Kola <dilip@tensult.com>
99
*/
1010

@@ -19,8 +19,8 @@ export class EqualsCondition implements IConditionFunction {
1919
return false;
2020
}
2121

22-
if(utils.type(args) !== 'object') {
23-
throw new AccessControlError('EqualsCondition expects type of args to be object')
22+
if (utils.type(args) !== 'object') {
23+
throw new AccessControlError('EqualsCondition expects type of args to be object')
2424
}
2525

2626
return Object.keys(args).every((key) => {

src/conditions/IConditionFunction.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Condition function interface
33
* @interface
4-
*
4+
*
55
* @author Dilip Kola <dilip@tensult.com>
66
*/
77

src/conditions/ListContainsCondition.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { IConditionFunction } from "./IConditionFunction";
1+
import { IConditionFunction } from './IConditionFunction';
22
import { AccessControlError } from '../core';
33
import utils from '../utils';
44
/**
5-
* List contains condition
6-
*
5+
* List contains condition
6+
*
77
* @author Dilip Kola <dilip@tensult.com>
88
*/
99
export class ListContainsCondition implements IConditionFunction {

src/conditions/NotCondition.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { IConditionFunction } from "./IConditionFunction";
2-
import { conditionEvaluator } from "./index";
1+
import { IConditionFunction } from './IConditionFunction';
2+
import { conditionEvaluator } from './index';
33
import { AccessControlError, ICondition } from '../core';
44
import utils from '../utils';
55

66
/**
77
* Not condition
8-
*
8+
*
99
* @author Dilip Kola <dilip@tensult.com>
1010
*/
1111

src/conditions/NotEqualsCondition.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { IConditionFunction } from "./IConditionFunction";
2-
import {AccessControlError} from '../core';
1+
import { IConditionFunction } from './IConditionFunction';
2+
import { AccessControlError } from '../core';
33
import utils from '../utils';
44

55
/**
66
* Not equals condition
7-
*
7+
*
88
* @author Dilip Kola <dilip@tensult.com>
99
*/
1010
export class NotEqualsCondition implements IConditionFunction {
@@ -16,8 +16,8 @@ export class NotEqualsCondition implements IConditionFunction {
1616
if (!context) {
1717
return false;
1818
}
19-
if(utils.type(args) !== 'object') {
20-
throw new AccessControlError('EqualsCondition expects type of args to be object')
19+
if (utils.type(args) !== 'object') {
20+
throw new AccessControlError('EqualsCondition expects type of args to be object')
2121
}
2222
return Object.keys(args).every((key) => {
2323
return utils.matchesAllElement(args[key], (elm) => { return elm !== context[key] })

src/conditions/OrCondition.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { IConditionFunction } from "./IConditionFunction";
2-
import { conditionEvaluator } from "./index";
3-
import {AccessControlError, ICondition} from '../core';
1+
import { IConditionFunction } from './IConditionFunction';
2+
import { conditionEvaluator } from './index';
3+
import { AccessControlError, ICondition } from '../core';
44
import utils from '../utils';
55

66
/**
77
* Or condition
8-
*
8+
*
99
* @author Dilip Kola <dilip@tensult.com>
1010
*/
1111
export class OrCondition implements IConditionFunction {
@@ -19,8 +19,8 @@ export class OrCondition implements IConditionFunction {
1919
return false;
2020
}
2121

22-
if(utils.type(args) !== 'array' && utils.type(args) !== 'object') {
23-
throw new AccessControlError('OrCondition expects type of args to be array or object')
22+
if (utils.type(args) !== 'array' && utils.type(args) !== 'object') {
23+
throw new AccessControlError('OrCondition expects type of args to be array or object')
2424
}
2525

2626
const conditions = utils.toArray(args);

src/conditions/StartsWithCondition.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { IConditionFunction } from "./IConditionFunction";
1+
import { IConditionFunction } from './IConditionFunction';
22
import { AccessControlError } from '../core';
33
import utils from '../utils';
44

55
/**
66
* Starts with condition
7-
*
7+
*
88
* @author Dilip Kola <dilip@tensult.com>
99
*/
1010
export class StartsWithCondition implements IConditionFunction {

src/conditions/TrueCondition.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { IConditionFunction } from "./IConditionFunction";
1+
import { IConditionFunction } from './IConditionFunction';
22

33
/**
44
* True condition
5-
*
5+
*
66
* @author Dilip Kola <dilip@tensult.com>
77
*/
88
export class TrueCondition implements IConditionFunction {

src/conditions/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { NotEqualsCondition } from './NotEqualsCondition';
44
import { NotCondition } from './NotCondition';
55
import { ListContainsCondition } from './ListContainsCondition';
66
import { IConditionFunction } from './IConditionFunction';
7-
import { OrCondition } from "./OrCondition";
8-
import { AndCondition } from "./AndCondition";
9-
import { StartsWithCondition } from "./StartsWithCondition";
7+
import { OrCondition } from './OrCondition';
8+
import { AndCondition } from './AndCondition';
9+
import { StartsWithCondition } from './StartsWithCondition';
1010
import { AccessControlError, ICondition } from '../core';
1111

1212
export namespace Conditions {
@@ -25,11 +25,11 @@ export const conditionEvaluator = (condition: ICondition, context): boolean => {
2525
return true;
2626
}
2727

28-
if(typeof condition === 'function') {
28+
if (typeof condition === 'function') {
2929
return condition(context);
3030
}
3131

32-
if(typeof condition === 'object') {
32+
if (typeof condition === 'object') {
3333
if (!Conditions[condition.Fn]) {
3434
throw new AccessControlError(`Condtion function:${condition.Fn} not found`)
3535
}

src/core/Access.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IAccessInfo } from '../core';
22
import utils from '../utils';
3-
import { ICondition } from "./ICondition";
3+
import { ICondition } from './ICondition';
44

55
/**
66
* Represents the inner `Access` class that helps build an access information

src/core/AccessControlError.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* @static
77
*/
88
class AccessControlError extends Error {
9-
public name:string = 'AccessControlError';
10-
constructor(public message:string = '') {
9+
public name: string = 'AccessControlError';
10+
constructor(public message: string = '') {
1111
super(message);
1212
// http://stackoverflow.com/a/41429145/112731
1313
Object.setPrototypeOf(this, AccessControlError.prototype);

src/core/IAccessInfo.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ICondition } from "./ICondition";
1+
import { ICondition } from './ICondition';
22

33
/**
44
* An interface that defines an access information to be granted or denied.

src/core/ICondition.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

22

3-
import {IDictionary} from './IDictionary'
3+
import { IDictionary } from './IDictionary'
44
/**
55
* An interface that defines condition for an access grant.
66
* @interface
7-
*
7+
*
88
* @author Dilip Kola <dilip@tensult.com>
99
*/
1010

src/core/IDictionary.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* An interface that defines dictionay type
44
* @interface
5-
*
5+
*
66
* @author Dilip Kola <dilip@tensult.com>
77
*/
88
export interface IDictionary<T> {

src/core/IQueryInfo.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ interface IQueryInfo {
1010
* Indicates a single or multiple roles to be queried.
1111
* @type {String|Array<String>}
1212
*/
13-
role?:string|string[];
13+
role?: string | string[];
1414
/**
1515
* Indicates the resource to be queried.
1616
* @type {String}
1717
*/
18-
resource?:string;
18+
resource?: string;
1919
/**
2020
* Defines the type of the operation that is (or not) to be performed on
2121
* the resource by the defined role(s).
2222
* See {@link ?api=ac#AccessControl.Action|`AccessControl.Action` enumeration}
2323
* for possible values.
2424
* @type {String}
2525
*/
26-
action?:string;
26+
action?: string;
2727
/**
2828
* Indicates if conditions to be skipped while querying.
2929
*/

0 commit comments

Comments
 (0)