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