-// Do not use the barrel (dependency loop)
-import { User } from '../../shared/users/user.model';
+import { User } from '@app/core/users/user.model'
+import { peertubeLocalStorage } from '@app/helpers/peertube-web-storage'
+import {
+ hasUserRight,
+ MyUser as ServerMyUserModel,
+ MyUserSpecialPlaylist,
+ NSFWPolicyType,
+ User as ServerUserModel,
+ UserRight,
+ UserRole
+} from '@shared/models'
+
+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',
- DISPLAY_NSFW: 'display_nsfw'
- };
+ 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);
+ 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
+ }
+
+ static flush () {
+ peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
+ peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
+ peertubeLocalStorage.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 () {
+ peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
+ peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
+ peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
+ }
+}
+
+export class AuthUser extends User implements ServerMyUserModel {
+ tokens: Tokens
+ specialPlaylists: MyUserSpecialPlaylist[]
+
+ static load () {
+ const usernameLocalStorage = peertubeLocalStorage.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),
- displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true'
+ 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,
+ webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true',
+ autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true',
+ videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true'
},
Tokens.load()
- );
+ )
}
- return null;
+ return null
}
- static flush() {
- localStorage.removeItem(this.KEYS.USERNAME);
- localStorage.removeItem(this.KEYS.ID);
- localStorage.removeItem(this.KEYS.ROLE);
- localStorage.removeItem(this.KEYS.DISPLAY_NSFW);
- Tokens.flush();
+ static flush () {
+ peertubeLocalStorage.removeItem(this.KEYS.USERNAME)
+ peertubeLocalStorage.removeItem(this.KEYS.ID)
+ peertubeLocalStorage.removeItem(this.KEYS.ROLE)
+ peertubeLocalStorage.removeItem(this.KEYS.EMAIL)
+ Tokens.flush()
}
- constructor(userHash: {
- id: number,
- username: string,
- role: string,
- displayNSFW: boolean
- }, hashTokens: any) {
- super(userHash);
- this.tokens = new Tokens(hashTokens);
- }
+ constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) {
+ super(userHash)
- getAccessToken() {
- return this.tokens.access_token;
+ this.tokens = new Tokens(hashTokens)
+ this.specialPlaylists = userHash.specialPlaylists
}
- getRefreshToken() {
- return this.tokens.refresh_token;
+ getAccessToken () {
+ return this.tokens.accessToken
}
- getTokenType() {
- return this.tokens.token_type;
+ getRefreshToken () {
+ return this.tokens.refreshToken
}
- refreshTokens(access_token: string, refresh_token: string) {
- this.tokens.access_token = access_token;
- this.tokens.refresh_token = refresh_token;
+ getTokenType () {
+ return this.tokens.tokenType
}
- save() {
- localStorage.setItem(AuthUser.KEYS.ID, this.id.toString());
- localStorage.setItem(AuthUser.KEYS.USERNAME, this.username);
- localStorage.setItem(AuthUser.KEYS.ROLE, this.role);
- localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW));
- this.tokens.save();
+ refreshTokens (accessToken: string, refreshToken: string) {
+ this.tokens.accessToken = accessToken
+ this.tokens.refreshToken = refreshToken
}
-}
-
-// 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
- });
- }
-
- return null;
+ hasRight (right: UserRight) {
+ return hasUserRight(this.role, right)
}
- static flush() {
- localStorage.removeItem(this.KEYS.ACCESS_TOKEN);
- localStorage.removeItem(this.KEYS.REFRESH_TOKEN);
- localStorage.removeItem(this.KEYS.TOKEN_TYPE);
- }
+ canManage (user: ServerUserModel) {
+ const myRole = this.role
- constructor(hash?: any) {
- if (hash) {
- this.access_token = hash.access_token;
- this.refresh_token = hash.refresh_token;
+ if (myRole === UserRole.ADMINISTRATOR) return true
- if (hash.token_type === 'bearer') {
- this.token_type = 'Bearer';
- } else {
- this.token_type = hash.token_type;
- }
- }
+ // I'm a moderator: I can only manage users
+ return user.role === UserRole.USER
}
- 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.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled))
+ peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo))
+ this.tokens.save()
}
}