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 hotkeysService: HotkeysService
40 this.serverConfig = this.serverService.getTmpConfig()
41 this.serverService.getConfig()
42 .subscribe(config => this.serverConfig = config)
44 this.isLoggedIn = this.authService.isLoggedIn()
45 if (this.isLoggedIn === true) this.user = this.authService.getUser()
46 this.computeIsUserHasAdminAccess()
48 this.authService.loginChangedSource.subscribe(
50 if (status === AuthStatus.LoggedIn) {
51 this.isLoggedIn = true
52 this.user = this.authService.getUser()
53 this.computeIsUserHasAdminAccess()
54 console.log('Logged in.')
55 } else if (status === AuthStatus.LoggedOut) {
56 this.isLoggedIn = false
58 this.computeIsUserHasAdminAccess()
59 console.log('Logged out.')
61 console.error('Unknown auth status: ' + status)
66 this.hotkeysService.cheatSheetToggle.subscribe(isOpen => {
67 this.helpVisible = isOpen
71 isRegistrationAllowed () {
72 return this.serverConfig.signup.allowed &&
73 this.serverConfig.signup.allowedForCurrentIP
76 getFirstAdminRightAvailable () {
77 const user = this.authService.getUser()
78 if (!user) return undefined
81 UserRight.MANAGE_USERS,
82 UserRight.MANAGE_SERVER_FOLLOW,
83 UserRight.MANAGE_VIDEO_ABUSES,
84 UserRight.MANAGE_VIDEO_BLACKLIST,
85 UserRight.MANAGE_JOBS,
86 UserRight.MANAGE_CONFIGURATION
89 for (const adminRight of adminRights) {
90 if (user.hasRight(adminRight)) {
98 getFirstAdminRouteAvailable () {
99 const right = this.getFirstAdminRightAvailable()
101 return this.routesPerRight[right]
104 logout (event: Event) {
105 event.preventDefault()
107 this.authService.logout()
108 // Redirect to home page
109 this.redirectService.redirectToHomepage()
112 openLanguageChooser () {
113 this.languageChooserModal.show()
116 openHotkeysCheatSheet () {
117 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
120 private computeIsUserHasAdminAccess () {
121 const right = this.getFirstAdminRightAvailable()
123 this.userHasAdminAccess = right !== undefined