1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { UserRight } from '../../../../shared/models/users/user-right.enum'
3 import { AuthService, AuthStatus, RedirectService, ServerService, ThemeService } from '../core'
4 import { User } from '../shared/users/user.model'
5 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
6 import { HotkeysService } from 'angular2-hotkeys'
10 templateUrl: './menu.component.html',
11 styleUrls: [ './menu.component.scss' ]
13 export class MenuComponent implements OnInit {
14 @ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent
18 userHasAdminAccess = false
21 private routesPerRight: { [ role in UserRight ]?: string } = {
22 [UserRight.MANAGE_USERS]: '/admin/users',
23 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
24 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses',
25 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blacklist',
26 [UserRight.MANAGE_JOBS]: '/admin/jobs',
27 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
31 private authService: AuthService,
32 private serverService: ServerService,
33 private redirectService: RedirectService,
34 private themeService: ThemeService,
35 private hotkeysService: HotkeysService
39 this.isLoggedIn = this.authService.isLoggedIn()
40 if (this.isLoggedIn === true) this.user = this.authService.getUser()
41 this.computeIsUserHasAdminAccess()
43 this.authService.loginChangedSource.subscribe(
45 if (status === AuthStatus.LoggedIn) {
46 this.isLoggedIn = true
47 this.user = this.authService.getUser()
48 this.computeIsUserHasAdminAccess()
49 console.log('Logged in.')
50 } else if (status === AuthStatus.LoggedOut) {
51 this.isLoggedIn = false
53 this.computeIsUserHasAdminAccess()
54 console.log('Logged out.')
56 console.error('Unknown auth status: ' + status)
61 this.hotkeysService.cheatSheetToggle.subscribe(isOpen => {
62 this.helpVisible = isOpen
66 isRegistrationAllowed () {
67 return this.serverService.getConfig().signup.allowed &&
68 this.serverService.getConfig().signup.allowedForCurrentIP
71 getFirstAdminRightAvailable () {
72 const user = this.authService.getUser()
73 if (!user) return undefined
76 UserRight.MANAGE_USERS,
77 UserRight.MANAGE_SERVER_FOLLOW,
78 UserRight.MANAGE_VIDEO_ABUSES,
79 UserRight.MANAGE_VIDEO_BLACKLIST,
80 UserRight.MANAGE_JOBS,
81 UserRight.MANAGE_CONFIGURATION
84 for (const adminRight of adminRights) {
85 if (user.hasRight(adminRight)) {
93 getFirstAdminRouteAvailable () {
94 const right = this.getFirstAdminRightAvailable()
96 return this.routesPerRight[right]
99 logout (event: Event) {
100 event.preventDefault()
102 this.authService.logout()
103 // Redirect to home page
104 this.redirectService.redirectToHomepage()
107 openLanguageChooser () {
108 this.languageChooserModal.show()
111 openHotkeysCheatSheet () {
112 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
116 this.themeService.toggleDarkTheme()
119 private computeIsUserHasAdminAccess () {
120 const right = this.getFirstAdminRightAvailable()
122 this.userHasAdminAccess = right !== undefined