]>
Commit | Line | Data |
---|---|---|
dfe3f7b7 K |
1 | import { Observable, of } from 'rxjs' |
2 | import { map } from 'rxjs/operators' | |
67ed6552 C |
3 | import { User } from '@app/core/users/user.model' |
4 | import { peertubeLocalStorage } from '@app/helpers/peertube-web-storage' | |
5 | import { | |
6 | hasUserRight, | |
7 | MyUser as ServerMyUserModel, | |
8 | MyUserSpecialPlaylist, | |
9 | NSFWPolicyType, | |
10 | User as ServerUserModel, | |
11 | UserRight, | |
dfe3f7b7 K |
12 | UserRole, |
13 | UserVideoQuota | |
67ed6552 | 14 | } from '@shared/models' |
df98563e C |
15 | |
16 | export type TokenOptions = { | |
17 | accessToken: string | |
18 | refreshToken: string | |
19 | tokenType: string | |
20 | } | |
21 | ||
22 | // Private class only used by User | |
23 | class Tokens { | |
24 | private static KEYS = { | |
25 | ACCESS_TOKEN: 'access_token', | |
26 | REFRESH_TOKEN: 'refresh_token', | |
27 | TOKEN_TYPE: 'token_type' | |
28 | } | |
29 | ||
30 | accessToken: string | |
31 | refreshToken: string | |
32 | tokenType: string | |
33 | ||
34 | static load () { | |
0bd78bf3 C |
35 | const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN) |
36 | const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN) | |
37 | const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE) | |
df98563e C |
38 | |
39 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | |
40 | return new Tokens({ | |
41 | accessToken: accessTokenLocalStorage, | |
42 | refreshToken: refreshTokenLocalStorage, | |
43 | tokenType: tokenTypeLocalStorage | |
44 | }) | |
45 | } | |
46 | ||
47 | return null | |
48 | } | |
49 | ||
50 | static flush () { | |
0bd78bf3 C |
51 | peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN) |
52 | peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN) | |
53 | peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE) | |
df98563e C |
54 | } |
55 | ||
56 | constructor (hash?: TokenOptions) { | |
57 | if (hash) { | |
58 | this.accessToken = hash.accessToken | |
59 | this.refreshToken = hash.refreshToken | |
60 | ||
61 | if (hash.tokenType === 'bearer') { | |
62 | this.tokenType = 'Bearer' | |
63 | } else { | |
64 | this.tokenType = hash.tokenType | |
65 | } | |
66 | } | |
67 | } | |
68 | ||
69 | save () { | |
0bd78bf3 C |
70 | peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) |
71 | peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) | |
72 | peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) | |
df98563e C |
73 | } |
74 | } | |
7da18e44 | 75 | |
b7819090 | 76 | export class AuthUser extends User implements ServerMyUserModel { |
df98563e | 77 | tokens: Tokens |
b7819090 | 78 | specialPlaylists: MyUserSpecialPlaylist[] |
bd5c83a8 | 79 | |
dfe3f7b7 K |
80 | canSeeVideosLink = true |
81 | ||
df98563e | 82 | static load () { |
0bd78bf3 | 83 | const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME) |
bd5c83a8 | 84 | if (usernameLocalStorage) { |
7da18e44 C |
85 | return new AuthUser( |
86 | { | |
0bd78bf3 C |
87 | id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10), |
88 | username: peertubeLocalStorage.getItem(this.KEYS.USERNAME), | |
89 | email: peertubeLocalStorage.getItem(this.KEYS.EMAIL), | |
90 | role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole, | |
2243730c | 91 | nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType, |
ed638e53 | 92 | webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true', |
276d9652 C |
93 | autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true', |
94 | videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true' | |
7da18e44 | 95 | }, |
629d8d6f | 96 | Tokens.load() |
df98563e | 97 | ) |
bd5c83a8 C |
98 | } |
99 | ||
df98563e | 100 | return null |
bd5c83a8 C |
101 | } |
102 | ||
df98563e | 103 | static flush () { |
0bd78bf3 C |
104 | peertubeLocalStorage.removeItem(this.KEYS.USERNAME) |
105 | peertubeLocalStorage.removeItem(this.KEYS.ID) | |
106 | peertubeLocalStorage.removeItem(this.KEYS.ROLE) | |
0bd78bf3 | 107 | peertubeLocalStorage.removeItem(this.KEYS.EMAIL) |
df98563e | 108 | Tokens.flush() |
bd5c83a8 C |
109 | } |
110 | ||
b7819090 | 111 | constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) { |
df98563e | 112 | super(userHash) |
b7819090 | 113 | |
df98563e | 114 | this.tokens = new Tokens(hashTokens) |
b7819090 | 115 | this.specialPlaylists = userHash.specialPlaylists |
bd5c83a8 C |
116 | } |
117 | ||
df98563e C |
118 | getAccessToken () { |
119 | return this.tokens.accessToken | |
bd5c83a8 | 120 | } |
bd5c83a8 | 121 | |
df98563e C |
122 | getRefreshToken () { |
123 | return this.tokens.refreshToken | |
bd5c83a8 C |
124 | } |
125 | ||
df98563e C |
126 | getTokenType () { |
127 | return this.tokens.tokenType | |
bd5c83a8 C |
128 | } |
129 | ||
df98563e C |
130 | refreshTokens (accessToken: string, refreshToken: string) { |
131 | this.tokens.accessToken = accessToken | |
132 | this.tokens.refreshToken = refreshToken | |
bd5c83a8 C |
133 | } |
134 | ||
757f0da3 | 135 | hasRight (right: UserRight) { |
954605a8 C |
136 | return hasUserRight(this.role, right) |
137 | } | |
138 | ||
a6051ba7 | 139 | canManage (user: ServerUserModel) { |
a95a4cc8 C |
140 | const myRole = this.role |
141 | ||
142 | if (myRole === UserRole.ADMINISTRATOR) return true | |
143 | ||
144 | // I'm a moderator: I can only manage users | |
145 | return user.role === UserRole.USER | |
146 | } | |
147 | ||
df98563e | 148 | save () { |
0bd78bf3 C |
149 | peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) |
150 | peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username) | |
151 | peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email) | |
152 | peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) | |
2243730c | 153 | peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString()) |
ed638e53 | 154 | peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled)) |
0bd78bf3 | 155 | peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) |
df98563e | 156 | this.tokens.save() |
bd5c83a8 | 157 | } |
dfe3f7b7 K |
158 | |
159 | computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> { | |
160 | if (!this.isUploadDisabled()) { | |
161 | this.canSeeVideosLink = true | |
162 | return of(this.canSeeVideosLink) | |
163 | } | |
164 | ||
165 | // Check if the user has videos | |
166 | return quotaObservable.pipe( | |
167 | map(({ videoQuotaUsed }) => { | |
168 | if (videoQuotaUsed !== 0) { | |
169 | // User already uploaded videos, so it can see the link | |
170 | this.canSeeVideosLink = true | |
171 | } else { | |
172 | // No videos, no upload so the user don't need to see the videos link | |
173 | this.canSeeVideosLink = false | |
174 | } | |
175 | ||
176 | return this.canSeeVideosLink | |
177 | }) | |
178 | ) | |
179 | } | |
bd5c83a8 | 180 | } |