X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fauth%2Fauth-user.model.ts;h=74ed1c5806c5b0730fa957a8052257c4c4f818b8;hb=ec769c89fdcbcaf2d15de8d211d3ede3b6f69c1a;hp=5d61954d6105e2777d2d9d5c42ca13bb3f05920c;hpb=e2a2d6c86c7ca39074fdff3b545947d1d58dc008;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 5d61954d6..74ed1c580 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts @@ -1,118 +1,144 @@ +import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' +import { UserRight } from '../../../../../shared/models/users/user-right.enum' // Do not use the barrel (dependency loop) -import { User } from '../../shared/users/user.model'; +import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role' +import { User, UserConstructorHash } from '../../shared/users/user.model' +import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type' + +export type TokenOptions = { + accessToken: string + refreshToken: string + tokenType: string +} -export class AuthUser extends User { +// Private class only used by User +class Tokens { private static KEYS = { - ID: 'id', - ROLE: 'role', - USERNAME: 'username' - }; + ACCESS_TOKEN: 'access_token', + REFRESH_TOKEN: 'refresh_token', + TOKEN_TYPE: 'token_type' + } - tokens: Tokens; + accessToken: string + refreshToken: string + tokenType: string - static load() { - const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); - if (usernameLocalStorage) { - return new AuthUser( - { - id: parseInt(localStorage.getItem(this.KEYS.ID)), - username: localStorage.getItem(this.KEYS.USERNAME), - role: localStorage.getItem(this.KEYS.ROLE) - }, - Tokens.load() - ); + static load () { + const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN) + const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN) + const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE) + + if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { + return new Tokens({ + accessToken: accessTokenLocalStorage, + refreshToken: refreshTokenLocalStorage, + tokenType: tokenTypeLocalStorage + }) } - return null; + return null } - static flush() { - localStorage.removeItem(this.KEYS.USERNAME); - localStorage.removeItem(this.KEYS.ID); - localStorage.removeItem(this.KEYS.ROLE); - Tokens.flush(); + static flush () { + peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN) + peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN) + peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE) } - constructor(userHash: { id: number, username: string, role: string }, hashTokens: any) { - super(userHash); - this.tokens = new Tokens(hashTokens); - } + constructor (hash?: TokenOptions) { + if (hash) { + this.accessToken = hash.accessToken + this.refreshToken = hash.refreshToken - getAccessToken() { - return this.tokens.access_token; + if (hash.tokenType === 'bearer') { + this.tokenType = 'Bearer' + } else { + this.tokenType = hash.tokenType + } + } } - getRefreshToken() { - return this.tokens.refresh_token; + save () { + peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) + peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) + peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) } +} - getTokenType() { - return this.tokens.token_type; +export class AuthUser extends User { + private static KEYS = { + ID: 'id', + ROLE: 'role', + EMAIL: 'email', + USERNAME: 'username', + NSFW_POLICY: 'nsfw_policy', + AUTO_PLAY_VIDEO: 'auto_play_video' } - refreshTokens(access_token: string, refresh_token: string) { - this.tokens.access_token = access_token; - this.tokens.refresh_token = refresh_token; - } + tokens: Tokens - save() { - localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()); - localStorage.setItem(AuthUser.KEYS.USERNAME, this.username); - localStorage.setItem(AuthUser.KEYS.ROLE, this.role); - this.tokens.save(); - } -} + static load () { + const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME) + if (usernameLocalStorage) { + return new AuthUser( + { + id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10), + username: peertubeLocalStorage.getItem(this.KEYS.USERNAME), + email: peertubeLocalStorage.getItem(this.KEYS.EMAIL), + role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole, + nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType, + autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true' + }, + Tokens.load() + ) + } -// Private class only used by User -class Tokens { - private static KEYS = { - ACCESS_TOKEN: 'access_token', - REFRESH_TOKEN: 'refresh_token', - TOKEN_TYPE: 'token_type', - }; + return null + } - access_token: string; - refresh_token: string; - token_type: string; + static flush () { + peertubeLocalStorage.removeItem(this.KEYS.USERNAME) + peertubeLocalStorage.removeItem(this.KEYS.ID) + peertubeLocalStorage.removeItem(this.KEYS.ROLE) + peertubeLocalStorage.removeItem(this.KEYS.NSFW_POLICY) + peertubeLocalStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO) + peertubeLocalStorage.removeItem(this.KEYS.EMAIL) + Tokens.flush() + } - static load() { - const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN); - const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN); - const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE); + constructor (userHash: UserConstructorHash, hashTokens: TokenOptions) { + super(userHash) + this.tokens = new Tokens(hashTokens) + } - if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { - return new Tokens({ - access_token: accessTokenLocalStorage, - refresh_token: refreshTokenLocalStorage, - token_type: tokenTypeLocalStorage - }); - } + getAccessToken () { + return this.tokens.accessToken + } - return null; + getRefreshToken () { + return this.tokens.refreshToken } - static flush() { - localStorage.removeItem(this.KEYS.ACCESS_TOKEN); - localStorage.removeItem(this.KEYS.REFRESH_TOKEN); - localStorage.removeItem(this.KEYS.TOKEN_TYPE); + getTokenType () { + return this.tokens.tokenType } - constructor(hash?: any) { - if (hash) { - this.access_token = hash.access_token; - this.refresh_token = hash.refresh_token; + refreshTokens (accessToken: string, refreshToken: string) { + this.tokens.accessToken = accessToken + this.tokens.refreshToken = refreshToken + } - if (hash.token_type === 'bearer') { - this.token_type = 'Bearer'; - } else { - this.token_type = hash.token_type; - } - } + hasRight (right: UserRight) { + return hasUserRight(this.role, right) } - save() { - localStorage.setItem('access_token', this.access_token); - localStorage.setItem('refresh_token', this.refresh_token); - localStorage.setItem('token_type', this.token_type); + save () { + peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) + peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username) + peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email) + peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) + peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString()) + peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) + this.tokens.save() } }