]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/auth/auth-user.model.ts
Move watch later logic in miniature
[github/Chocobozzz/PeerTube.git] / client / src / app / core / auth / auth-user.model.ts
CommitLineData
88a7f93f 1import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
0bd78bf3 2import { UserRight } from '../../../../../shared/models/users/user-right.enum'
b7819090 3import { MyUser as ServerMyUserModel, MyUserSpecialPlaylist } from '../../../../../shared/models/users/user.model'
e2a2d6c8 4// Do not use the barrel (dependency loop)
954605a8 5import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role'
276d9652 6import { User } from '../../shared/users/user.model'
0883b324 7import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type'
df98563e
C
8
9export type TokenOptions = {
10 accessToken: string
11 refreshToken: string
12 tokenType: string
13}
14
15// Private class only used by User
16class Tokens {
17 private static KEYS = {
18 ACCESS_TOKEN: 'access_token',
19 REFRESH_TOKEN: 'refresh_token',
20 TOKEN_TYPE: 'token_type'
21 }
22
23 accessToken: string
24 refreshToken: string
25 tokenType: string
26
27 static load () {
0bd78bf3
C
28 const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)
29 const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)
30 const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)
df98563e
C
31
32 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
33 return new Tokens({
34 accessToken: accessTokenLocalStorage,
35 refreshToken: refreshTokenLocalStorage,
36 tokenType: tokenTypeLocalStorage
37 })
38 }
39
40 return null
41 }
42
43 static flush () {
0bd78bf3
C
44 peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
45 peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
46 peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
df98563e
C
47 }
48
49 constructor (hash?: TokenOptions) {
50 if (hash) {
51 this.accessToken = hash.accessToken
52 this.refreshToken = hash.refreshToken
53
54 if (hash.tokenType === 'bearer') {
55 this.tokenType = 'Bearer'
56 } else {
57 this.tokenType = hash.tokenType
58 }
59 }
60 }
61
62 save () {
0bd78bf3
C
63 peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
64 peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
65 peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
df98563e
C
66 }
67}
7da18e44 68
b7819090 69export class AuthUser extends User implements ServerMyUserModel {
bd5c83a8 70 private static KEYS = {
629d8d6f
C
71 ID: 'id',
72 ROLE: 'role',
66dd264f 73 EMAIL: 'email',
276d9652 74 VIDEOS_HISTORY_ENABLED: 'videos-history-enabled',
92fb909c 75 USERNAME: 'username',
2243730c 76 NSFW_POLICY: 'nsfw_policy',
ed638e53 77 WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled',
7efe153b 78 AUTO_PLAY_VIDEO: 'auto_play_video'
df98563e 79 }
bd5c83a8 80
df98563e 81 tokens: Tokens
b7819090 82 specialPlaylists: MyUserSpecialPlaylist[]
bd5c83a8 83
df98563e 84 static load () {
0bd78bf3 85 const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME)
bd5c83a8 86 if (usernameLocalStorage) {
7da18e44
C
87 return new AuthUser(
88 {
0bd78bf3
C
89 id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10),
90 username: peertubeLocalStorage.getItem(this.KEYS.USERNAME),
91 email: peertubeLocalStorage.getItem(this.KEYS.EMAIL),
92 role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole,
2243730c 93 nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType,
ed638e53 94 webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true',
276d9652
C
95 autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true',
96 videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true'
7da18e44 97 },
629d8d6f 98 Tokens.load()
df98563e 99 )
bd5c83a8
C
100 }
101
df98563e 102 return null
bd5c83a8
C
103 }
104
df98563e 105 static flush () {
0bd78bf3
C
106 peertubeLocalStorage.removeItem(this.KEYS.USERNAME)
107 peertubeLocalStorage.removeItem(this.KEYS.ID)
108 peertubeLocalStorage.removeItem(this.KEYS.ROLE)
2243730c 109 peertubeLocalStorage.removeItem(this.KEYS.NSFW_POLICY)
ed638e53 110 peertubeLocalStorage.removeItem(this.KEYS.WEBTORRENT_ENABLED)
276d9652 111 peertubeLocalStorage.removeItem(this.KEYS.VIDEOS_HISTORY_ENABLED)
0bd78bf3
C
112 peertubeLocalStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO)
113 peertubeLocalStorage.removeItem(this.KEYS.EMAIL)
df98563e 114 Tokens.flush()
bd5c83a8
C
115 }
116
b7819090 117 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) {
df98563e 118 super(userHash)
b7819090 119
df98563e 120 this.tokens = new Tokens(hashTokens)
b7819090 121 this.specialPlaylists = userHash.specialPlaylists
bd5c83a8
C
122 }
123
df98563e
C
124 getAccessToken () {
125 return this.tokens.accessToken
bd5c83a8 126 }
bd5c83a8 127
df98563e
C
128 getRefreshToken () {
129 return this.tokens.refreshToken
bd5c83a8
C
130 }
131
df98563e
C
132 getTokenType () {
133 return this.tokens.tokenType
bd5c83a8
C
134 }
135
df98563e
C
136 refreshTokens (accessToken: string, refreshToken: string) {
137 this.tokens.accessToken = accessToken
138 this.tokens.refreshToken = refreshToken
bd5c83a8
C
139 }
140
757f0da3 141 hasRight (right: UserRight) {
954605a8
C
142 return hasUserRight(this.role, right)
143 }
144
b7819090 145 canManage (user: ServerMyUserModel) {
a95a4cc8
C
146 const myRole = this.role
147
148 if (myRole === UserRole.ADMINISTRATOR) return true
149
150 // I'm a moderator: I can only manage users
151 return user.role === UserRole.USER
152 }
153
df98563e 154 save () {
0bd78bf3
C
155 peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString())
156 peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username)
157 peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email)
158 peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString())
2243730c 159 peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString())
ed638e53 160 peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled))
0bd78bf3 161 peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo))
df98563e 162 this.tokens.save()
bd5c83a8
C
163 }
164}