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'
7 import { ServerConfig } from '@shared/models'
11 templateUrl: './menu.component.html',
12 styleUrls: [ './menu.component.scss' ]
14 export class MenuComponent implements OnInit {
15 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
19 userHasAdminAccess = false
22 private serverConfig: ServerConfig
23 private routesPerRight: { [ role in UserRight ]?: string } = {
24 [UserRight.MANAGE_USERS]: '/admin/users',
25 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
26 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses',
27 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blacklist',
28 [UserRight.MANAGE_JOBS]: '/admin/jobs',
29 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
33 private authService: AuthService,
34 private serverService: ServerService,
35 private redirectService: RedirectService,
36 private themeService: ThemeService,
37 private hotkeysService: HotkeysService
41 this.serverConfig = this.serverService.getTmpConfig()
42 this.serverService.getConfig()
43 .subscribe(config => this.serverConfig = config)
45 this.isLoggedIn = this.authService.isLoggedIn()
46 if (this.isLoggedIn === true) this.user = this.authService.getUser()
47 this.computeIsUserHasAdminAccess()
49 this.authService.loginChangedSource.subscribe(
51 if (status === AuthStatus.LoggedIn) {
52 this.isLoggedIn = true
53 this.user = this.authService.getUser()
54 this.computeIsUserHasAdminAccess()
55 console.log('Logged in.')
56 } else if (status === AuthStatus.LoggedOut) {
57 this.isLoggedIn = false
59 this.computeIsUserHasAdminAccess()
60 console.log('Logged out.')
62 console.error('Unknown auth status: ' + status)
67 this.hotkeysService.cheatSheetToggle.subscribe(isOpen => {
68 this.helpVisible = isOpen
72 isRegistrationAllowed () {
73 return this.serverConfig.signup.allowed &&
74 this.serverConfig.signup.allowedForCurrentIP
77 getFirstAdminRightAvailable () {
78 const user = this.authService.getUser()
79 if (!user) return undefined
82 UserRight.MANAGE_USERS,
83 UserRight.MANAGE_SERVER_FOLLOW,
84 UserRight.MANAGE_VIDEO_ABUSES,
85 UserRight.MANAGE_VIDEO_BLACKLIST,
86 UserRight.MANAGE_JOBS,
87 UserRight.MANAGE_CONFIGURATION
90 for (const adminRight of adminRights) {
91 if (user.hasRight(adminRight)) {
99 getFirstAdminRouteAvailable () {
100 const right = this.getFirstAdminRightAvailable()
102 return this.routesPerRight[right]
105 logout (event: Event) {
106 event.preventDefault()
108 this.authService.logout()
109 // Redirect to home page
110 this.redirectService.redirectToHomepage()
113 openLanguageChooser () {
114 this.languageChooserModal.show()
117 openHotkeysCheatSheet () {
118 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
121 private computeIsUserHasAdminAccess () {
122 const right = this.getFirstAdminRightAvailable()
124 this.userHasAdminAccess = right !== undefined