forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.ts
More file actions
125 lines (121 loc) · 3.82 KB
/
Copy pathBase.ts
File metadata and controls
125 lines (121 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { Ace } from "./Ace";
export class WellknownIds {
static root: string = "59f1f6e6f0a22200126638d8";
static admins: string = "5a1702fa245d9013697656fb";
static users: string = "5a17f157c4815318c8536c21";
static robots: string = "5ac0850ca538fee1ebdb996c";
static nodered_users: string = "5a23f18a2e8987292ddbe061";
static nodered_admins: string = "5a17f157c4815318c8536c20";
static filestore_users: string = "5b6ab63c8d4a64b7c47f4a8f";
static filestore_admins: string = "5b6ab63c8d4a64b7c47f4a8e";
static robot_users: string = "5aef0142f3683977b0aa3dd3";
static robot_admins: string = "5aef0142f3683977b0aa3dd2";
}
export class Rights {
static create = 1;
static read = 2;
static update = 3;
static delete = 4;
static invoke = 5;
static full_control = -1;
}
interface IBase {
_id:string;
_type:string;
name:string;
getRight(_id:string, deny:boolean):Ace;
addRight(_id:string,name:string, Rights:number[],deny:boolean):void;
removeRight(_id:string,Rights:number[],deny:boolean):void;
}
export class Base implements IBase {
_id:string;
_type:string = "unknown";
_acl:Ace[] = [];
name:string;
_name:string;
_encrypt:string[] = [];
_createdbyid: string;
_createdby: string;
_created: Date;
_modifiedbyid: string;
_modifiedby: string;
_modified: Date;
_version: Number = 0;
constructor() {
this.addRight(WellknownIds.admins, "admins", [Rights.full_control]);
}
/**
* Create new instance of object, using values from input object
* @param {T} o Base object
* @returns T New object as Type
*/
static assign<T>(o:T): T {
return Object.assign(new Base(), o);
}
/**
* Enumerate ACL for specefic ID
* @param {string} _id Id to search for
* @param {boolean=false} deny look for deny or allow permission
* @returns Ace Ace if found, else null
*/
getRight(_id:string, deny:boolean=false):Ace {
var result:Ace = null;
if(!this._acl) { this._acl = []; }
this._acl.forEach((a, index) => {
if(a._id===_id && a.deny===deny) {
this._acl[index] = Ace.assign(a);
result = this._acl[index];
}
});
if(result) {
result = Ace.assign(result);
}
return result;
}
/**
* Set right for specefic id, if exists
* @param {Ace} x
* @returns void
*/
setRight(x:Ace): void {
if(!this._acl) { this._acl = []; }
this._acl.forEach((a, index) => {
if(a._id===x._id && a.deny===x.deny) {
this._acl[index] = x;
}
});
}
/**
* Add/update right for user/role
* @param {string} _id user/role id
* @param {string} name Displayname for user/role
* @param {number[]} rights Right to set
* @param {boolean=false} deny Deny the right
* @returns void
*/
addRight(_id:string,name:string, rights:number[],deny:boolean=false):void {
var right:Ace = this.getRight(_id, deny);
if(!right) { right = new Ace(); this._acl.push(right); }
right.deny = deny;right._id = _id; right.name = name;
rights.forEach(bit => {
right.setBit(bit);
});
this.setRight(right);
}
/**
* Remove a right from user/role
* @param {string} _id user/role id
* @param {number[]=null} rights Right to revoke
* @param {boolean=false} deny Deny right
* @returns void
*/
removeRight(_id:string,rights:number[]=null,deny:boolean=false): void {
if(!this._acl) { this._acl = []; }
var right:Ace = this.getRight(_id, deny);
if(!right) { return; }
rights.forEach(bit => {
right.unsetBit(bit);
});
this.setRight(right);
}
}