Merge pull request #32733 from BendingBender/acl

[acl] Export all module interfaces, fix lint exceptions
This commit is contained in:
Armando Aguirre
2019-02-07 17:57:28 -08:00
committed by GitHub
5 changed files with 199 additions and 247 deletions
+159 -130
View File
@@ -1,150 +1,179 @@
// Type definitions for node_acl 0.4.8
// Type definitions for acl 0.4
// Project: https://github.com/optimalbits/node_acl
// Definitions by: Qubo <https://github.com/tkQubo>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
/// <reference types="node"/>
/// <reference types="express"/>
import http = require('http');
import Promise = require("bluebird");
import express = require("express");
import Promise = require('bluebird');
import express = require('express');
import redis = require('redis');
import mongo = require('mongodb');
type strings = string|string[];
type Value = string|number;
type Values = Value|Value[];
export = AclStatic;
declare const AclStatic: AclStatic;
type strings = string | string[];
type Value = string | number;
type Values = Value | Value[];
type Action = () => any;
type Callback = (err: Error) => any;
type Callback = (err?: Error) => any;
type AnyCallback = (err: Error, obj: any) => any;
type AllowedCallback = (err: Error, allowed: boolean) => any;
type GetUserId = (req: http.IncomingMessage, res: http.ServerResponse) => Value;
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;
new (
backend: AclStatic.Backend<any>,
logger?: AclStatic.Logger,
options?: AclStatic.Option
): AclStatic.Acl;
readonly memoryBackend: AclStatic.MemoryBackendStatic;
readonly mongodbBackend: AclStatic.MongodbBackendStatic;
readonly redisBackend: AclStatic.RedisBackendStatic;
}
interface Logger {
debug: (msg: string) => any;
}
interface Acl {
addUserRoles: (userId: Value, roles: strings, cb?: Callback) => Promise<void>;
removeUserRoles: (userId: Value, roles: strings, cb?: Callback) => 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?: Callback) => Promise<void>;
removeRole: (role: string, cb?: Callback) => Promise<void>;
removeResource: (resource: string, cb?: Callback) => Promise<void>;
allow: {
(roles: Values, resources: strings, permissions: strings, cb?: Callback): Promise<void>;
(aclSets: AclSet | AclSet[]): Promise<void>;
declare namespace AclStatic {
interface Logger {
debug: (msg: string) => any;
}
removeAllow: (role: string, resources: strings, permissions: strings, cb?: Callback) => 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, cb?: AnyCallback): Promise<any>;
(roles: strings, permissions: strings, cb?: AnyCallback): Promise<any>;
interface Acl {
addUserRoles: (userId: Value, roles: strings, cb?: Callback) => Promise<void>;
removeUserRoles: (userId: Value, roles: strings, cb?: Callback) => 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?: Callback) => Promise<void>;
removeRole: (role: string, cb?: Callback) => Promise<void>;
removeResource: (resource: string, cb?: Callback) => Promise<void>;
allow: {
(roles: Values, resources: strings, permissions: strings, cb?: Callback): Promise<void>;
(aclSets: AclSet | AclSet[]): Promise<void>;
};
removeAllow: (
role: string,
resources: strings,
permissions: strings,
cb?: Callback
) => Promise<void>;
removePermissions: (
role: string,
resources: strings,
permissions: strings,
cb?: Callback
) => 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, cb?: AnyCallback): Promise<any>;
(roles: strings, permissions: strings, cb?: AnyCallback): Promise<any>;
};
permittedResources: (roles: strings, permissions: strings, cb?: Callback) => Promise<void>;
middleware: (
numPathComponents?: number,
userId?: Value | GetUserId,
actions?: strings
) => express.RequestHandler;
}
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 MemoryBackend extends Backend<Action[]> {}
interface MemoryBackendStatic {
new (): MemoryBackend;
}
//
// For internal use
//
interface Backend<T> {
begin: () => T;
end: (transaction: T, cb?: Action) => void;
clean: (cb?: Action) => void;
get: (bucket: string, key: Value, cb?: Action) => void;
union: (bucket: string, keys: Value[], cb?: Action) => void;
add: (transaction: T, bucket: string, key: Value, values: Values) => void;
del: (transaction: T, bucket: string, keys: Value[]) => void;
remove: (transaction: T, bucket: string, key: Value, values: Values) => void;
endAsync: (transaction: T, cb?: (err: Error | null) => void) => Promise<void>;
getAsync: (
bucket: string,
key: Value,
cb?: (err: Error | null, value: any) => void
) => Promise<any>;
cleanAsync: (cb?: (error?: Error) => void) => Promise<void>;
unionAsync: (
bucket: string,
keys: Value[],
cb?: (error: Error | undefined, results: any[]) => void
) => Promise<any[]>;
}
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;
}
// for redis backend
interface RedisBackend extends Backend<redis.RedisClient> {}
interface RedisBackendStatic {
new (redis: redis.RedisClient, prefix?: string): RedisBackend;
}
// for mongodb backend
interface MongodbBackend extends Backend<Callback> {}
interface MongodbBackendStatic {
new (db: mongo.Db, prefix?: string, useSingle?: boolean): MongodbBackend;
}
permittedResources: (roles: strings, permissions: strings, cb?: Function) => Promise<void>;
middleware: (numPathComponents?: number, userId?: Value | GetUserId, actions?: strings) => express.RequestHandler;
}
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 MemoryBackend extends Backend<Action[]> { }
interface MemoryBackendStatic {
new (): MemoryBackend;
}
//
// For internal use
//
interface Backend<T> {
begin: () => T;
end: (transaction: T, cb?: Action) => void;
clean: (cb?: Action) => void;
get: (bucket: string, key: Value, cb?: Action) => void;
union: (bucket: string, keys: Value[], cb?: Action) => void;
add: (transaction: T, bucket: string, key: Value, values: Values) => void;
del: (transaction: T, bucket: string, keys: Value[]) => void;
remove: (transaction: T, bucket: string, key: Value, values: Values) => void;
endAsync: Function; //TODO: Give more specific function signature
getAsync: Function;
cleanAsync: Function;
unionAsync: Function;
}
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;
}
// for redis backend
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;
}
// for mongodb backend
import mongo = require('mongodb');
interface AclStatic {
mongodbBackend: MongodbBackendStatic;
}
interface MongodbBackend extends Backend<Callback> { }
interface MongodbBackendStatic {
new (db: mongo.Db, prefix: string, useSingle: boolean): MongodbBackend;
new (db: mongo.Db, prefix: string): MongodbBackend;
new (db: mongo.Db): MongodbBackend;
}
declare var _: AclStatic;
export = _;
+35 -36
View File
@@ -2,15 +2,15 @@
// 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);
const report = (err: Error, value: any) => {
if (err) {
console.error(err);
}
console.info(value);
};
// Using the memory backend
var acl = new Acl(new Acl.memoryBackend());
const acl: Acl.Acl = new Acl(new Acl.memoryBackend());
// middleware with no optional parameters
acl.middleware();
@@ -18,11 +18,11 @@ acl.middleware();
acl.middleware(1);
acl.middleware(1, () => {
return "joed";
return 'joed';
});
acl.middleware(1, () => {
return 2;
return 2;
});
acl.middleware(1, 'joed');
@@ -33,36 +33,36 @@ acl.middleware(3, 'joed', 'post');
acl.allow('guest', 'blogs', 'view');
// allow function accepts arrays as any parameter
acl.allow('member', 'blogs', ['edit','view', 'delete']);
acl.allow('member', 'blogs', ['edit', 'view', 'delete']);
acl.addUserRoles('joed', 'guest');
acl.addRoleParents('baz', ['foo','bar']);
acl.addRoleParents('baz', ['foo', 'bar']);
acl.allow('foo', ['blogs','forums','news'], ['view', 'delete']);
acl.allow('foo', ['blogs', 'forums', 'news'], ['view', 'delete']);
acl.allow('admin', ['blogs','forums'], '*');
acl.allow('admin', ['blogs', 'forums'], '*');
acl.allow([
{
roles:['guest','special-member'],
allows:[
{resources:'blogs', permissions:'get'},
{resources:['forums','news'], permissions:['get','put','delete']}
]
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']}
]
}
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");
console.log('User joed is allowed to view blogs');
}
});
@@ -78,15 +78,14 @@ acl.whatResources('foo', 'view', (err, res) => {
}
});
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);
});
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);
});
+2 -2
View File
@@ -5,10 +5,10 @@ import mongodb = require('mongodb');
declare var db: mongodb.Db;
// Using the mongo db backend
var acl = new Acl(new Acl.mongodbBackend(db, 'acl_', true));
const 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']);
acl.allow('member', 'blogs', ['edit', 'view', 'delete']);
+2 -2
View File
@@ -5,10 +5,10 @@ import redis = require('redis');
declare var client: redis.RedisClient;
// Using the redis backend
var acl = new Acl(new Acl.redisBackend(client, 'acl_'));
const 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']);
acl.allow('member', 'blogs', ['edit', 'view', 'delete']);
+1 -77
View File
@@ -1,79 +1,3 @@
{
"extends": "dtslint/dt.json",
"rules": {
"adjacent-overload-signatures": false,
"array-type": false,
"arrow-return-shorthand": false,
"ban-types": false,
"callable-types": false,
"comment-format": false,
"dt-header": false,
"eofline": false,
"export-just-namespace": false,
"import-spacing": false,
"interface-name": false,
"interface-over-type-literal": false,
"jsdoc-format": false,
"max-line-length": false,
"member-access": false,
"new-parens": false,
"no-any-union": false,
"no-boolean-literal-compare": false,
"no-conditional-assignment": false,
"no-consecutive-blank-lines": false,
"no-construct": false,
"no-declare-current-package": false,
"no-duplicate-imports": false,
"no-duplicate-variable": false,
"no-empty-interface": false,
"no-for-in-array": false,
"no-inferrable-types": false,
"no-internal-module": false,
"no-irregular-whitespace": false,
"no-mergeable-namespace": false,
"no-misused-new": false,
"no-namespace": false,
"no-object-literal-type-assertion": false,
"no-padding": false,
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": false,
"no-redundant-undefined": false,
"no-reference-import": false,
"no-relative-import-in-test": false,
"no-self-import": false,
"no-single-declare-module": false,
"no-string-throw": false,
"no-unnecessary-callback-wrapper": false,
"no-unnecessary-class": false,
"no-unnecessary-generics": false,
"no-unnecessary-qualifier": false,
"no-unnecessary-type-assertion": false,
"no-useless-files": false,
"no-var-keyword": false,
"no-var-requires": false,
"no-void-expression": false,
"no-trailing-whitespace": false,
"object-literal-key-quotes": false,
"object-literal-shorthand": false,
"one-line": false,
"one-variable-per-declaration": false,
"only-arrow-functions": false,
"prefer-conditional-expression": false,
"prefer-const": false,
"prefer-declare-function": false,
"prefer-for-of": false,
"prefer-method-signature": false,
"prefer-template": false,
"radix": false,
"semicolon": false,
"space-before-function-paren": false,
"space-within-parens": false,
"strict-export-declare-modifiers": false,
"trim-file": false,
"triple-equals": false,
"typedef-whitespace": false,
"unified-signatures": false,
"void-return": false,
"whitespace": false
}
"extends": "dtslint/dt.json"
}