-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUserService.js
More file actions
61 lines (52 loc) · 1.7 KB
/
UserService.js
File metadata and controls
61 lines (52 loc) · 1.7 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
const { ApplicationService } = require('@themost/common');
const { BehaviorSubject, shareReplay, switchMap, from, of } = require('rxjs');
class UserService extends ApplicationService {
constructor(app) {
super(app);
this.refreshAnonymousUser$ = new BehaviorSubject(void 0);
this.anonymousUser$ = this.refreshAnonymousUser$.pipe(switchMap((value) => {
if (typeof value !== 'undefined') {
return of(value);
}
// create a new context
const context = this.getApplication().createContext();
// get anonymous user
return from(this.getAnonymousUser(context).finally(() => {
// finalize context
return context.finalizeAsync();
}));
}), shareReplay(1));
}
/**
* @param {import('@themost/common').DataContextBase} context
* @param {string} name
* @returns {Promise<any>}
*/
getUser(context, name) {
return context.model('User').asQueryable().where((x, username) => {
return x.name === username;
}, name).expand((x) => x.groups).silent().getItem();
}
/**
* @param {import('@themost/common').DataContextBase} context
* @param {string} name
* @returns {Promise<any>}
*/
getGroup(context, name) {
return context.model('Group').asQueryable().where((x, username) => {
return x.name === username;
}, name).silent().getItem();
}
/**
* @param {import('@themost/common').DataContextBase} context
* @returns {Promise<any>}
*/
getAnonymousUser(context) {
return context.model('User').asQueryable().where((x) => {
return x.name === 'anonymous';
}).expand((x) => x.groups).silent().getItem();
}
}
module.exports = {
UserService
};