forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenUser.ts
More file actions
42 lines (40 loc) · 1.33 KB
/
Copy pathTokenUser.ts
File metadata and controls
42 lines (40 loc) · 1.33 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
import { User } from "./User";
import { Rolemember } from "./Role";
import { WellknownIds } from "./base";
import { Crypt } from "./Crypt";
export class TokenUser {
_type:string;
_id:string;
name:string;
username:string;
roles:Rolemember[] = [];
constructor(user:User | TokenUser) {
if(user===null || user===undefined) { return; }
this._type = user._type;
this._id = user._id;
this.name = user.name;
this.username = user.username;
this.roles = user.roles;
}
static assign<T>(o:T): T {
var newo:TokenUser = new TokenUser(null);
return Object.assign(newo, o);
}
static rootUser():User {
var result:User = new User();
result._type = "user"; result.name = "root"; result.username = "root"; result._id = WellknownIds.root;
result.roles = []; result.roles.push(new Rolemember("admins", WellknownIds.admins));
return result;
}
static rootToken():string {
return Crypt.createToken(TokenUser.rootUser());
}
hasrolename(name:string):Boolean {
var hits:Rolemember[] = this.roles.filter(member=>member.name===name);
return (hits.length===1);
}
hasroleid(id:string):boolean {
var hits:Rolemember[] = this.roles.filter(member=>member._id===id);
return (hits.length===1);
}
}