X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fauth%2Fauth-user.model.ts;h=2260752653820f950518db0b83e63545f69abbcd;hb=98bd5e2256bfdeba6d5ab07f0421acfde1a0de26;hp=4155aea1927a1f131e5208d6a583378d049dd365;hpb=33c4972d5b54155540267f4c9c9ee55c539b8385;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 4155aea19..226075265 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts @@ -1,139 +1,79 @@ -// Do not use the barrel (dependency loop) -import { UserRole } from '../../../../../shared/models/users/user-role.type' -import { User } from '../../shared/users/user.model' - -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' - } - - accessToken: string - refreshToken: string - tokenType: string - - 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 - }) - } - - 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 +import { Observable, of } from 'rxjs' +import { map } from 'rxjs/operators' +import { User } from '@app/core/users/user.model' +import { OAuthUserTokens } 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 { + oauthTokens: OAuthUserTokens + specialPlaylists: MyUserSpecialPlaylist[] + + canSeeVideosLink = true + + constructor (userHash: Partial, hashTokens: Partial) { + super(userHash) - if (hash.tokenType === 'bearer') { - this.tokenType = 'Bearer' - } else { - this.tokenType = hash.tokenType - } - } + this.oauthTokens = new OAuthUserTokens(hashTokens) + this.specialPlaylists = userHash.specialPlaylists } - 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) + getAccessToken () { + return this.oauthTokens.accessToken } -} -export class AuthUser extends User { - private static KEYS = { - ID: 'id', - ROLE: 'role', - EMAIL: 'email', - USERNAME: 'username', - DISPLAY_NSFW: 'display_nsfw' + getRefreshToken () { + return this.oauthTokens.refreshToken } - 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: localStorage.getItem(this.KEYS.ROLE) as UserRole, - displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true' - }, - Tokens.load() - ) - } - - return null + getTokenType () { + return this.oauthTokens.tokenType } - 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.EMAIL) - Tokens.flush() + refreshTokens (accessToken: string, refreshToken: string) { + this.oauthTokens.accessToken = accessToken + this.oauthTokens.refreshToken = refreshToken } - constructor (userHash: { - id: number, - username: string, - role: UserRole, - email: string, - displayNSFW: boolean - }, hashTokens: TokenOptions) { - super(userHash) - this.tokens = new Tokens(hashTokens) + hasRight (right: UserRight) { + return hasUserRight(this.role.id, right) } - getAccessToken () { - return this.tokens.accessToken - } + canManage (user: ServerUserModel) { + const myRole = this.role.id - getRefreshToken () { - return this.tokens.refreshToken - } + if (myRole === UserRole.ADMINISTRATOR) return true - getTokenType () { - return this.tokens.tokenType + // I'm a moderator: I can only manage users + return user.role.id === UserRole.USER } - refreshTokens (accessToken: string, refreshToken: string) { - this.tokens.accessToken = accessToken - this.tokens.refreshToken = refreshToken - } + computeCanSeeVideosLink (quotaObservable: Observable): Observable { + if (!this.isUploadDisabled()) { + this.canSeeVideosLink = true + return of(this.canSeeVideosLink) + } - 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) - localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)) - this.tokens.save() + // 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 + }) + ) } }