1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { UserRight } from '../../../../shared/models/users/user-right.enum'
3 import { AuthService, AuthStatus, RedirectService, ServerService } 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
33 this.isLoggedIn = this.authService.isLoggedIn()
34 if (this.isLoggedIn === true) this.user = this.authService.getUser()
35 this.computeIsUserHasAdminAccess()
37 this.authService.loginChangedSource.subscribe(
39 if (status === AuthStatus.LoggedIn) {
40 this.isLoggedIn = true
41 this.user = this.authService.getUser()
42 this.computeIsUserHasAdminAccess()
43 console.log('Logged in.')
44 } else if (status === AuthStatus.LoggedOut) {
45 this.isLoggedIn = false
47 this.computeIsUserHasAdminAccess()
48 console.log('Logged out.')
50 console.error('Unknown auth status: ' + status)
56 isRegistrationAllowed () {
57 return this.serverService.getConfig().signup.allowed &&
58 this.serverService.getConfig().signup.allowedForCurrentIP
61 getFirstAdminRightAvailable () {
62 const user = this.authService.getUser()
63 if (!user) return undefined
66 UserRight.MANAGE_USERS,
67 UserRight.MANAGE_SERVER_FOLLOW,
68 UserRight.MANAGE_VIDEO_ABUSES,
69 UserRight.MANAGE_VIDEO_BLACKLIST
72 for (const adminRight of adminRights) {
73 if (user.hasRight(adminRight)) {
81 getFirstAdminRouteAvailable () {
82 const right = this.getFirstAdminRightAvailable()
84 return this.routesPerRight[right]
87 logout (event: Event) {
88 event.preventDefault()
90 this.authService.logout()
91 // Redirect to home page
92 this.redirectService.redirectToHomepage()
95 openLanguageChooser () {
96 this.languageChooserModal.show()
99 private computeIsUserHasAdminAccess () {
100 const right = this.getFirstAdminRightAvailable()
102 this.userHasAdminAccess = right !== undefined