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
|
import { Observable, of } from 'rxjs'
import { map } from 'rxjs/operators'
import { User } from '@app/core/users/user.model'
import {
flushUserInfoFromLocalStorage,
getUserInfoFromLocalStorage,
saveUserInfoIntoLocalStorage,
TokenOptions,
Tokens
} from '@root-helpers/users'
import { hasUserRight } from '@shared/core-utils/users'
import {
MyUser as ServerMyUserModel,
MyUserSpecialPlaylist,
User as ServerUserModel,
UserRight,
UserRole,
UserVideoQuota
} from '@shared/models'
export class AuthUser extends User implements ServerMyUserModel {
tokens: Tokens
specialPlaylists: MyUserSpecialPlaylist[]
canSeeVideosLink = true
static load () {
const userInfo = getUserInfoFromLocalStorage()
if (!userInfo) return null
return new AuthUser(userInfo, Tokens.load())
}
static flush () {
flushUserInfoFromLocalStorage()
Tokens.flush()
}
constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) {
super(userHash)
this.tokens = new Tokens(hashTokens)
this.specialPlaylists = userHash.specialPlaylists
}
getAccessToken () {
return this.tokens.accessToken
}
getRefreshToken () {
return this.tokens.refreshToken
}
getTokenType () {
return this.tokens.tokenType
}
refreshTokens (accessToken: string, refreshToken: string) {
this.tokens.accessToken = accessToken
this.tokens.refreshToken = refreshToken
}
hasRight (right: UserRight) {
return hasUserRight(this.role, right)
}
canManage (user: ServerUserModel) {
const myRole = this.role
if (myRole === UserRole.ADMINISTRATOR) return true
// I'm a moderator: I can only manage users
return user.role === UserRole.USER
}
save () {
saveUserInfoIntoLocalStorage({
id: this.id,
username: this.username,
email: this.email,
role: this.role,
nsfwPolicy: this.nsfwPolicy,
webTorrentEnabled: this.webTorrentEnabled,
autoPlayVideo: this.autoPlayVideo
})
this.tokens.save()
}
computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> {
if (!this.isUploadDisabled()) {
this.canSeeVideosLink = true
return of(this.canSeeVideosLink)
}
// Check if the user has videos
return quotaObservable.pipe(
map(({ videoQuotaUsed }) => {
if (videoQuotaUsed !== 0) {
// User already uploaded videos, so it can see the link
this.canSeeVideosLink = true
} else {
// No videos, no upload so the user don't need to see the videos link
this.canSeeVideosLink = false
}
return this.canSeeVideosLink
})
)
}
}
|