Skip to content

Commit

Permalink
Add acl
Browse files Browse the repository at this point in the history
  • Loading branch information
A12893 authored and A12893 committed Mar 12, 2015
1 parent 3b2ed80 commit b6667de
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 0 deletions.
22 changes: 22 additions & 0 deletions acl/acl-mongoDBBackend.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Type definitions for node_acl 0.4.7
// Project: https://github.com/optimalbits/node_acl
// Definitions by: Qubo <https://github.com/tkQubo>
// Definitions: https://github.com/borisyankov/DefinitelyTyped

/// <reference path="acl.d.ts" />
/// <reference path="../mongodb/mongodb.d.ts" />

declare module "acl" {
import mongo = require('mongodb');

interface AclStatic {
mongodbBackend: MongodbBackendStatic;
}

interface MongodbBackend extends Backend<ErrCallback> { }
interface MongodbBackendStatic {
new(db: mongo.Db, prefix: string, useSingle: boolean): MongodbBackend;
new(db: mongo.Db, prefix: string): MongodbBackend;
new(db: mongo.Db): MongodbBackend;
}
}
16 changes: 16 additions & 0 deletions acl/acl-mongodbBackend-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='acl-mongodbBackend.d.ts'/>

// https://github.com/OptimalBits/node_acl/blob/master/Readme.md
import Acl = require('acl');
import mongodb = require('mongodb');

var db: mongodb.Db;

// Using the memory backend
var acl = new Acl(new Acl.mongodbBackend(db, 'acl_', true));

// guest is allowed to view blogs
acl.allow('guest', 'blogs', 'view');

// allow function accepts arrays as any parameter
acl.allow('member', 'blogs', ['edit','view', 'delete']);
16 changes: 16 additions & 0 deletions acl/acl-redisBackend-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='./acl-redisBackend.d.ts'/>

// https://github.com/OptimalBits/node_acl/blob/master/Readme.md
import Acl = require('acl');
import redis = require('redis');

var client: redis.RedisClient;

// Using the memory backend
var acl = new Acl(new Acl.redisBackend(client, 'acl_'));

// guest is allowed to view blogs
acl.allow('guest', 'blogs', 'view');

// allow function accepts arrays as any parameter
acl.allow('member', 'blogs', ['edit','view', 'delete']);
21 changes: 21 additions & 0 deletions acl/acl-redisBackend.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Type definitions for node_acl 0.4.7
// Project: https://github.com/optimalbits/node_acl
// Definitions by: Qubo <https://github.com/tkQubo>
// Definitions: https://github.com/borisyankov/DefinitelyTyped

/// <reference path="acl.d.ts" />
/// <reference path='../redis/redis.d.ts'/>

declare module "acl" {
import redis = require('redis');

interface AclStatic {
redisBackend: RedisBackendStatic;
}

interface RedisBackend extends Backend<redis.RedisClient> { }
interface RedisBackendStatic {
new(redis: redis.RedisClient, prefix: string): RedisBackend;
new(redis: redis.RedisClient): RedisBackend;
}
}
65 changes: 65 additions & 0 deletions acl/acl-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/// <reference path='acl.d.ts'/>

// Sample code from
// https://github.com/OptimalBits/node_acl/blob/master/Readme.md
import Acl = require('acl');

var report = <T>(err: Error, value: T) => {
if (err) {
console.error(err);
}
console.info(value);
};

// Using the memory backend
var acl = new Acl(new Acl.memoryBackend());

// guest is allowed to view blogs
acl.allow('guest', 'blogs', 'view');

// allow function accepts arrays as any parameter
acl.allow('member', 'blogs', ['edit','view', 'delete']);

acl.addUserRoles('joed', 'guest');

acl.addRoleParents('baz', ['foo','bar']);

acl.allow('foo', ['blogs','forums','news'], ['view', 'delete']);

acl.allow('admin', ['blogs','forums'], '*');

acl.allow([
{
roles:['guest','special-member'],
allows:[
{resources:'blogs', permissions:'get'},
{resources:['forums','news'], permissions:['get','put','delete']}
]
},
{
roles:['gold','silver'],
allows:[
{resources:'cash', permissions:['sell','exchange']},
{resources:['account','deposit'], permissions:['put','delete']}
]
}
]);

acl.isAllowed('joed', 'blogs', 'view', (err, res) => {
if (res) {
console.log("User joed is allowed to view blogs");
}
});

acl.isAllowed('jsmith', 'blogs', ['edit','view','delete'])
.then((result) => {
console.dir('jsmith is allowed blogs ' + result);
acl.addUserRoles('jsmith', 'member');
}).then(() =>
acl.isAllowed('jsmith', 'blogs', ['edit','view','delete'])
).then((result) =>
console.dir('jsmith is allowed blogs ' + result)
).then(() => {
acl.allowedPermissions('james', ['blogs','forums'], report);
acl.allowedPermissions('jsmith', ['blogs','forums'], report);
});
117 changes: 117 additions & 0 deletions acl/acl.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Type definitions for node_acl 0.4.7
// Project: https://github.com/optimalbits/node_acl
// Definitions by: Qubo <https://github.com/tkQubo>
// Definitions: https://github.com/borisyankov/DefinitelyTyped

/// <reference path="../bluebird/bluebird.d.ts" />
/// <reference path='../node/node.d.ts'/>

declare module "acl" {
import http = require('http');
import Promise = require("bluebird");

type Func = ()=>any;
type Value = string|number;
type Values = Value|Value[];
type strings = string|string[];
type ErrCallback = (err: Error) => any;
type AnyCallback = (err: Error, obj: any) => any;
type AllowedCallback = (err: Error, allowed: boolean) => any;
type GetUserId = (req: http.ServerRequest, res: http.ServerResponse) => any;

interface AclStatic {
new (backend: Backend<any>, logger: Logger, options: Option): Acl;
new (backend: Backend<any>, logger: Logger): Acl;
new (backend: Backend<any>): Acl;
memoryBackend: MemoryBackendStatic;
}

interface Logger {
debug: (msg: string)=>any;
}

interface Acl {
addUserRoles: (userId: Value, roles: strings, cb?: ErrCallback) => Promise<void>;
removeUserRoles: (userId: Value, roles: strings, cb?: ErrCallback) => Promise<void>;
userRoles: (userId: Value, cb?: (err: Error, roles: string[])=>any) => Promise<string[]>;
roleUsers: (role: Value, cb?: (err: Error, users: Values)=>any) => Promise<any>;
hasRole: (userId: Value, role: string, cb?: (err: Error, isInRole: boolean)=>any) => Promise<boolean>;
addRoleParents: (role: string, parents: Values, cb?: ErrCallback) => Promise<void>;
removeRole: (role: string, cb?: ErrCallback) => Promise<void>;
removeResource: (resource: string, cb?: ErrCallback) => Promise<void>;
allow: {
(roles: Values, resources: strings, permissions: strings, cb?: ErrCallback): Promise<void>;
(aclSets: AclSet|AclSet[]): Promise<void>;
}
removeAllow: (role: string, resources: strings, permissions: strings, cb?: ErrCallback) => Promise<void>;
removePermissions: (role: string, resources: strings, permissions: strings, cb?: Function) => Promise<void>;
allowedPermissions: (userId: Value, resources: strings, cb?: AnyCallback) => Promise<void>;
isAllowed: (userId: Value, resources: strings, permissions: strings, cb?: AllowedCallback) => Promise<boolean>;
areAnyRolesAllowed: (roles: strings, resource: strings, permissions: strings, cb?: AllowedCallback) => Promise<any>;
whatResources: (roles: strings, permissions: strings, cb?: AnyCallback) => Promise<any>;
permittedResources: (roles: strings, permissions: strings, cb?: Function) => Promise<void>;
middleware: (numPathComponents: number, userId: Value|GetUserId, actions: strings) => Promise<any>;
}

interface Option {
buckets?: BucketsOption;
}

interface BucketsOption {
meta?: string;
parents?: string;
permissions?: string;
resources?: string;
roles?: string;
users?: string;
}

interface AclSet {
roles: strings;
allows: AclAllow[];
}

interface AclAllow {
resources: strings;
permissions: strings;
}

interface Backend<T> {
begin: () => T;
end: (transaction: T, cb?: Func) => void;
clean: (cb?: Func) => void;
get: (bucket: string, key: Value, cb?: Func) => void;
union: (bucket: string, keys: Value[], cb?: Func) => void;
add: (transaction: T, bucket: string, key: Value, values: Value|Value[]) => void;
del: (transaction: T, bucket: string, keys: Value[]) => void;
remove: (transaction: T, bucket: string, key: Value, values: Value|Value[]) => void;

endAsync: Function; //TODO: Give more specific function signature
getAsync: Function;
cleanAsync: Function;
unionAsync: Function;
}

interface MemoryBackend extends Backend<Func[]> { }
interface MemoryBackendStatic {
new(): MemoryBackend;
}

interface Contract {
(args: IArguments): Contract|NoOp;
debug: boolean;
fulfilled: boolean;
args: any[];
checkedParams: string[];
params: (...types: string[]) => Contract|NoOp;
end: () => void;
}

interface NoOp {
params: (...types: string[]) => NoOp;
end: () => void;
}

var _: AclStatic;
export = _;
}

0 comments on commit b6667de

Please sign in to comment.