-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathindex.ts
52 lines (44 loc) · 1.34 KB
/
index.ts
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
import axios from 'axios';
interface BaseResponse {
message: string;
data: any;
}
export class Enforcer {
private endpoint: string;
private user : string | undefined;
private profiles: {[key: string]: string[]} = {};
public constructor(endpoint: string) {
this.endpoint = endpoint;
}
/**
* Get the profiles.
*/
public getProfiles() : {[key: string]: string[]} {
return this.profiles;
}
/**
* Get the authority of a given user from Casbin core
*/
public async syncUserProfiles(): Promise<void> {
const resp = await axios.get<BaseResponse>(`${this.endpoint}?casbin_subject=${this.user}`);
this.profiles = JSON.parse(resp.data.data);
console.log("syncUserProfiles is called")
}
/**
* Set the user subject for the enforcer
* @param user The current user
*/
public async setUser(user : string) : Promise<void> {
// Sync with the server and fetch the latest profiles of the new user
if (user != this.user) {
this.user = user;
await this.syncUserProfiles()
}
}
/**
* To enforce if the user have the authority to perform `act` on `obj`
*/
public async enforce(obj: string, act: string): Promise<void> {
throw new Error('Not implemented');
}
}