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