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/video-abuses',
23 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist'
27 private authService: AuthService,
28 private serverService: ServerService,
29 private redirectService: RedirectService,
30 private themeService: ThemeService
34 this.isLoggedIn = this.authService.isLoggedIn()
35 if (this.isLoggedIn === true) this.user = this.authService.getUser()
36 this.computeIsUserHasAdminAccess()
38 this.authService.loginChangedSource.subscribe(
40 if (status === AuthStatus.LoggedIn) {
41 this.isLoggedIn = true
42 this.user = this.authService.getUser()
43 this.computeIsUserHasAdminAccess()
44 console.log('Logged in.')
45 } else if (status === AuthStatus.LoggedOut) {
46 this.isLoggedIn = false
48 this.computeIsUserHasAdminAccess()
49 console.log('Logged out.')
51 console.error('Unknown auth status: ' + status)
57 isRegistrationAllowed () {
58 return this.serverService.getConfig().signup.allowed &&
59 this.serverService.getConfig().signup.allowedForCurrentIP
62 getFirstAdminRightAvailable () {
63 const user = this.authService.getUser()
64 if (!user) return undefined
67 UserRight.MANAGE_USERS,
68 UserRight.MANAGE_SERVER_FOLLOW,
69 UserRight.MANAGE_VIDEO_ABUSES,
70 UserRight.MANAGE_VIDEO_BLACKLIST
73 for (const adminRight of adminRights) {
74 if (user.hasRight(adminRight)) {
82 getFirstAdminRouteAvailable () {
83 const right = this.getFirstAdminRightAvailable()
85 return this.routesPerRight[right]
88 logout (event: Event) {
89 event.preventDefault()
91 this.authService.logout()
92 // Redirect to home page
93 this.redirectService.redirectToHomepage()
96 openLanguageChooser () {
97 this.languageChooserModal.show()
101 this.themeService.toggleDarkTheme()
104 private computeIsUserHasAdminAccess () {
105 const right = this.getFirstAdminRightAvailable()
107 this.userHasAdminAccess = right !== undefined