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=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..f10b37e5a 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts @@ -1,118 +1,115 @@ -// Do not use the barrel (dependency loop) -import { User } from '../../shared/users/user.model'; - -export class AuthUser extends User { - private static KEYS = { - ID: 'id', - ROLE: 'role', - USERNAME: 'username' - }; - - tokens: Tokens; - - 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() - ); - } - - return null; +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[] + + canSeeVideosLink = true + + static load () { + const tokens = Tokens.load() + if (!tokens) return null + + const userInfo = getUserInfoFromLocalStorage() + if (!userInfo) return null + + return new AuthUser(userInfo, tokens) } - static flush() { - localStorage.removeItem(this.KEYS.USERNAME); - localStorage.removeItem(this.KEYS.ID); - localStorage.removeItem(this.KEYS.ROLE); - Tokens.flush(); + static flush () { + flushUserInfoFromLocalStorage() + + Tokens.flush() } - constructor(userHash: { id: number, username: string, role: string }, hashTokens: any) { - super(userHash); - this.tokens = new Tokens(hashTokens); + constructor (userHash: Partial, hashTokens: TokenOptions) { + super(userHash) + + this.tokens = new Tokens(hashTokens) + this.specialPlaylists = userHash.specialPlaylists } - getAccessToken() { - return this.tokens.access_token; + getAccessToken () { + return this.tokens.accessToken } - getRefreshToken() { - return this.tokens.refresh_token; + getRefreshToken () { + return this.tokens.refreshToken } - getTokenType() { - return this.tokens.token_type; + getTokenType () { + return this.tokens.tokenType } - refreshTokens(access_token: string, refresh_token: string) { - this.tokens.access_token = access_token; - this.tokens.refresh_token = refresh_token; + refreshTokens (accessToken: string, refreshToken: string) { + this.tokens.accessToken = accessToken + this.tokens.refreshToken = refreshToken } - 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(); + hasRight (right: UserRight) { + return hasUserRight(this.role, right) } -} -// Private class only used by User -class Tokens { - private static KEYS = { - ACCESS_TOKEN: 'access_token', - REFRESH_TOKEN: 'refresh_token', - TOKEN_TYPE: 'token_type', - }; - - access_token: string; - refresh_token: string; - token_type: 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({ - access_token: accessTokenLocalStorage, - refresh_token: refreshTokenLocalStorage, - token_type: tokenTypeLocalStorage - }); - } + canManage (user: ServerUserModel) { + const myRole = this.role - return null; - } + if (myRole === UserRole.ADMINISTRATOR) return true - static flush() { - localStorage.removeItem(this.KEYS.ACCESS_TOKEN); - localStorage.removeItem(this.KEYS.REFRESH_TOKEN); - localStorage.removeItem(this.KEYS.TOKEN_TYPE); + // I'm a moderator: I can only manage users + return user.role === UserRole.USER } - constructor(hash?: any) { - if (hash) { - this.access_token = hash.access_token; - this.refresh_token = hash.refresh_token; + save () { + 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() + } - if (hash.token_type === 'bearer') { - this.token_type = 'Bearer'; - } else { - this.token_type = hash.token_type; - } + computeCanSeeVideosLink (quotaObservable: Observable): Observable { + if (!this.isUploadDisabled()) { + this.canSeeVideosLink = true + return of(this.canSeeVideosLink) } - } - save() { - localStorage.setItem('access_token', this.access_token); - localStorage.setItem('refresh_token', this.refresh_token); - localStorage.setItem('token_type', this.token_type); + // 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 + }) + ) } }