X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fauth%2Fauth-user.model.ts;h=f10b37e5ad71cff8905f089a5972504d353367fc;hb=421ff4618da64f0849353383f690a014024c40da;hp=9ad275392e359739cd59593d0ddbb1d4ec028563;hpb=7efe153b0bc23e596d5019b9fb3e3e32b6cfeccd;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 9ad275392..f10b37e5a 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts @@ -1,112 +1,50 @@ -// Do not use the barrel (dependency loop) -import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role' -import { User, UserConstructorHash } from '../../shared/users/user.model' -import { UserRight } from '../../../../../shared/models/users/user-right.enum' - -export type TokenOptions = { - accessToken: string - refreshToken: string - tokenType: string -} - -// Private class only used by User -class Tokens { - private static KEYS = { - ACCESS_TOKEN: 'access_token', - REFRESH_TOKEN: 'refresh_token', - TOKEN_TYPE: 'token_type' - } +import { Observable, of } from 'rxjs' +import { map } from 'rxjs/operators' +import { User } from '@app/core/users/user.model' +import { + flushUserInfoFromLocalStorage, + getUserInfoFromLocalStorage, + saveUserInfoIntoLocalStorage, + TokenOptions, + Tokens +} from '@root-helpers/users' +import { hasUserRight } from '@shared/core-utils/users' +import { + MyUser as ServerMyUserModel, + MyUserSpecialPlaylist, + User as ServerUserModel, + UserRight, + UserRole, + UserVideoQuota +} from '@shared/models' + +export class AuthUser extends User implements ServerMyUserModel { + tokens: Tokens + specialPlaylists: MyUserSpecialPlaylist[] - accessToken: string - refreshToken: string - tokenType: string + canSeeVideosLink = true 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) - - if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { - return new Tokens({ - accessToken: accessTokenLocalStorage, - refreshToken: refreshTokenLocalStorage, - tokenType: tokenTypeLocalStorage - }) - } + const tokens = Tokens.load() + if (!tokens) return null - return null - } + const userInfo = getUserInfoFromLocalStorage() + if (!userInfo) return null - static flush () { - localStorage.removeItem(this.KEYS.ACCESS_TOKEN) - localStorage.removeItem(this.KEYS.REFRESH_TOKEN) - localStorage.removeItem(this.KEYS.TOKEN_TYPE) - } - - constructor (hash?: TokenOptions) { - if (hash) { - this.accessToken = hash.accessToken - this.refreshToken = hash.refreshToken - - if (hash.tokenType === 'bearer') { - this.tokenType = 'Bearer' - } else { - this.tokenType = hash.tokenType - } - } - } - - save () { - localStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) - localStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) - localStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) - } -} - -export class AuthUser extends User { - private static KEYS = { - ID: 'id', - ROLE: 'role', - EMAIL: 'email', - USERNAME: 'username', - DISPLAY_NSFW: 'display_nsfw', - AUTO_PLAY_VIDEO: 'auto_play_video' - } - - tokens: Tokens - - static load () { - const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME) - if (usernameLocalStorage) { - return new AuthUser( - { - id: parseInt(localStorage.getItem(this.KEYS.ID), 10), - username: localStorage.getItem(this.KEYS.USERNAME), - email: localStorage.getItem(this.KEYS.EMAIL), - role: parseInt(localStorage.getItem(this.KEYS.ROLE), 10) as UserRole, - displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true', - autoPlayVideo: localStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true' - }, - Tokens.load() - ) - } - - return null + return new AuthUser(userInfo, tokens) } static flush () { - localStorage.removeItem(this.KEYS.USERNAME) - localStorage.removeItem(this.KEYS.ID) - localStorage.removeItem(this.KEYS.ROLE) - localStorage.removeItem(this.KEYS.DISPLAY_NSFW) - localStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO) - localStorage.removeItem(this.KEYS.EMAIL) + flushUserInfoFromLocalStorage() + Tokens.flush() } - constructor (userHash: UserConstructorHash, hashTokens: TokenOptions) { + constructor (userHash: Partial, hashTokens: TokenOptions) { super(userHash) + this.tokens = new Tokens(hashTokens) + this.specialPlaylists = userHash.specialPlaylists } getAccessToken () { @@ -130,13 +68,48 @@ export class AuthUser extends User { return hasUserRight(this.role, right) } + canManage (user: ServerUserModel) { + const myRole = this.role + + if (myRole === UserRole.ADMINISTRATOR) return true + + // I'm a moderator: I can only manage users + return user.role === UserRole.USER + } + save () { - localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) - localStorage.setItem(AuthUser.KEYS.USERNAME, this.username) - localStorage.setItem(AuthUser.KEYS.EMAIL, this.email) - localStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) - localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)) - localStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) + saveUserInfoIntoLocalStorage({ + id: this.id, + username: this.username, + email: this.email, + role: this.role, + nsfwPolicy: this.nsfwPolicy, + webTorrentEnabled: this.webTorrentEnabled, + autoPlayVideo: this.autoPlayVideo + }) + this.tokens.save() } + + computeCanSeeVideosLink (quotaObservable: Observable): Observable { + if (!this.isUploadDisabled()) { + this.canSeeVideosLink = true + return of(this.canSeeVideosLink) + } + + // Check if the user has videos + return quotaObservable.pipe( + map(({ videoQuotaUsed }) => { + if (videoQuotaUsed !== 0) { + // User already uploaded videos, so it can see the link + this.canSeeVideosLink = true + } else { + // No videos, no upload so the user don't need to see the videos link + this.canSeeVideosLink = false + } + + return this.canSeeVideosLink + }) + ) + } }