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'
9 templateUrl: './menu.component.html',
10 styleUrls: [ './menu.component.scss' ]
12 export class MenuComponent implements OnInit {
13 @ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent
17 userHasAdminAccess = false
19 private routesPerRight = {
20 [UserRight.MANAGE_USERS]: '/admin/users',
21 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
22 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses',
23 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blacklist',
24 [UserRight.MANAGE_JOBS]: '/admin/jobs',
25 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
29 private authService: AuthService,
30 private serverService: ServerService,
31 private redirectService: RedirectService,
32 private themeService: ThemeService
36 this.isLoggedIn = this.authService.isLoggedIn()
37 if (this.isLoggedIn === true) this.user = this.authService.getUser()
38 this.computeIsUserHasAdminAccess()
40 this.authService.loginChangedSource.subscribe(
42 if (status === AuthStatus.LoggedIn) {
43 this.isLoggedIn = true
44 this.user = this.authService.getUser()
45 this.computeIsUserHasAdminAccess()
46 console.log('Logged in.')
47 } else if (status === AuthStatus.LoggedOut) {
48 this.isLoggedIn = false
50 this.computeIsUserHasAdminAccess()
51 console.log('Logged out.')
53 console.error('Unknown auth status: ' + status)
59 isRegistrationAllowed () {
60 return this.serverService.getConfig().signup.allowed &&
61 this.serverService.getConfig().signup.allowedForCurrentIP
64 getFirstAdminRightAvailable () {
65 const user = this.authService.getUser()
66 if (!user) return undefined
69 UserRight.MANAGE_USERS,
70 UserRight.MANAGE_SERVER_FOLLOW,
71 UserRight.MANAGE_VIDEO_ABUSES,
72 UserRight.MANAGE_VIDEO_BLACKLIST,
73 UserRight.MANAGE_JOBS,
74 UserRight.MANAGE_CONFIGURATION
77 for (const adminRight of adminRights) {
78 if (user.hasRight(adminRight)) {
86 getFirstAdminRouteAvailable () {
87 const right = this.getFirstAdminRightAvailable()
89 return this.routesPerRight[right]
92 logout (event: Event) {
93 event.preventDefault()
95 this.authService.logout()
96 // Redirect to home page
97 this.redirectService.redirectToHomepage()
100 openLanguageChooser () {
101 this.languageChooserModal.show()
105 this.themeService.toggleDarkTheme()
108 private computeIsUserHasAdminAccess () {
109 const right = this.getFirstAdminRightAvailable()
111 this.userHasAdminAccess = right !== undefined