forked from hczhcz/matt.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.ts
93 lines (78 loc) · 2.86 KB
/
user.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
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
'use strict';
import {ErrFunc, ValFunc} from './util';
import {Context, User} from './interface';
export class NoUser implements User {
group(context: Context, callback: ValFunc<string>, fail: ErrFunc): void {
callback('nobody');
}
name(context: Context, callback: ValFunc<string>, fail: ErrFunc): void {
callback('nobody');
}
superuser(context: Context, callback: ValFunc<boolean>, fail: ErrFunc): void {
callback(false);
}
distance(context: Context, callback: ValFunc<number>, fail: ErrFunc): void {
callback(Infinity);
}
};
function userDistance(
context: Context, owner: User,
callback: ValFunc<number>, fail: ErrFunc
): void {
context.user((user: User): void => {
user.superuser(context, (superuser: boolean): void => {
if (superuser) {
callback(0);
} else {
user.name(context, (userName: string): void => {
owner.name(context, (ownerName: string): void => {
if (userName === ownerName) {
callback(0);
} else {
user.group(context, (userGroup: string): void => {
owner.group(context, (ownerGroup: string): void => {
if (userGroup === ownerGroup) {
callback(1);
} else {
callback(Infinity);
}
}, fail);
}, fail);
}
}, fail);
}, fail);
}
}, fail);
});
}
export class UnixUser implements User {
constructor(private _group: string, private _name: string) {
//
}
group(context: Context, callback: ValFunc<string>, fail: ErrFunc): void {
callback(this._group);
}
name(context: Context, callback: ValFunc<string>, fail: ErrFunc): void {
callback(this._name);
}
superuser(context: Context, callback: ValFunc<boolean>, fail: ErrFunc): void {
callback(false);
}
distance(context: Context, callback: ValFunc<number>, fail: ErrFunc): void {
userDistance(context, this, callback, fail);
}
};
export class UnixSuperUser implements User {
group(context: Context, callback: ValFunc<string>, fail: ErrFunc): void {
callback('root');
}
name(context: Context, callback: ValFunc<string>, fail: ErrFunc): void {
callback('root');
}
superuser(context: Context, callback: ValFunc<boolean>, fail: ErrFunc): void {
callback(true);
}
distance(context: Context, callback: ValFunc<number>, fail: ErrFunc): void {
userDistance(context, this, callback, fail);
}
};