Display username instead of email in menu
[oweals/peertube.git] / client / src / app / menu / menu.component.ts
1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { UserRight } from '../../../../shared/models/users/user-right.enum'
3 import { AuthService, AuthStatus, RedirectService, ServerService } from '../core'
4 import { User } from '../shared/users/user.model'
5 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
6
7 @Component({
8   selector: 'my-menu',
9   templateUrl: './menu.component.html',
10   styleUrls: [ './menu.component.scss' ]
11 })
12 export class MenuComponent implements OnInit {
13   @ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent
14
15   user: User
16   isLoggedIn: boolean
17   userHasAdminAccess = false
18
19   private routesPerRight = {
20     [UserRight.MANAGE_USERS]: '/admin/users',
21     [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
22     [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/video-abuses',
23     [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist'
24   }
25
26   constructor (
27     private authService: AuthService,
28     private serverService: ServerService,
29     private redirectService: RedirectService
30   ) {}
31
32   ngOnInit () {
33     this.isLoggedIn = this.authService.isLoggedIn()
34     if (this.isLoggedIn === true) this.user = this.authService.getUser()
35     this.computeIsUserHasAdminAccess()
36
37     this.authService.loginChangedSource.subscribe(
38       status => {
39         if (status === AuthStatus.LoggedIn) {
40           this.isLoggedIn = true
41           this.user = this.authService.getUser()
42           this.computeIsUserHasAdminAccess()
43           console.log('Logged in.')
44         } else if (status === AuthStatus.LoggedOut) {
45           this.isLoggedIn = false
46           this.user = undefined
47           this.computeIsUserHasAdminAccess()
48           console.log('Logged out.')
49         } else {
50           console.error('Unknown auth status: ' + status)
51         }
52       }
53     )
54   }
55
56   isRegistrationAllowed () {
57     return this.serverService.getConfig().signup.allowed &&
58            this.serverService.getConfig().signup.allowedForCurrentIP
59   }
60
61   getFirstAdminRightAvailable () {
62     const user = this.authService.getUser()
63     if (!user) return undefined
64
65     const adminRights = [
66       UserRight.MANAGE_USERS,
67       UserRight.MANAGE_SERVER_FOLLOW,
68       UserRight.MANAGE_VIDEO_ABUSES,
69       UserRight.MANAGE_VIDEO_BLACKLIST
70     ]
71
72     for (const adminRight of adminRights) {
73       if (user.hasRight(adminRight)) {
74         return adminRight
75       }
76     }
77
78     return undefined
79   }
80
81   getFirstAdminRouteAvailable () {
82     const right = this.getFirstAdminRightAvailable()
83
84     return this.routesPerRight[right]
85   }
86
87   logout (event: Event) {
88     event.preventDefault()
89
90     this.authService.logout()
91     // Redirect to home page
92     this.redirectService.redirectToHomepage()
93   }
94
95   openLanguageChooser () {
96     this.languageChooserModal.show()
97   }
98
99   private computeIsUserHasAdminAccess () {
100     const right = this.getFirstAdminRightAvailable()
101
102     this.userHasAdminAccess = right !== undefined
103   }
104 }