1 import { Component, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
4 import { AuthService, AuthStatus } from '../auth'
5 import { ServerService } from '../server'
6 import { UserRight } from '../../../../../shared/models/users/user-right.enum'
10 templateUrl: './menu.component.html',
11 styleUrls: [ './menu.component.scss' ]
13 export class MenuComponent implements OnInit {
15 userHasAdminAccess = false
17 private routesPerRight = {
18 [UserRight.MANAGE_USERS]: '/admin/users',
19 [UserRight.MANAGE_APPLICATION_FOLLOW]: '/admin/friends',
20 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/video-abuses',
21 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist'
25 private authService: AuthService,
26 private serverService: ServerService,
27 private router: Router
31 this.isLoggedIn = this.authService.isLoggedIn()
32 this.computeIsUserHasAdminAccess()
34 this.authService.loginChangedSource.subscribe(
36 if (status === AuthStatus.LoggedIn) {
37 this.isLoggedIn = true
38 this.computeIsUserHasAdminAccess()
39 console.log('Logged in.')
40 } else if (status === AuthStatus.LoggedOut) {
41 this.isLoggedIn = false
42 this.computeIsUserHasAdminAccess()
43 console.log('Logged out.')
45 console.error('Unknown auth status: ' + status)
51 isRegistrationAllowed () {
52 return this.serverService.getConfig().signup.allowed
55 getFirstAdminRightAvailable () {
56 const user = this.authService.getUser()
57 if (!user) return undefined
60 UserRight.MANAGE_USERS,
61 UserRight.MANAGE_APPLICATION_FOLLOW,
62 UserRight.MANAGE_VIDEO_ABUSES,
63 UserRight.MANAGE_VIDEO_BLACKLIST
66 for (const adminRight of adminRights) {
67 if (user.hasRight(adminRight)) {
75 getFirstAdminRouteAvailable () {
76 const right = this.getFirstAdminRightAvailable()
78 return this.routesPerRight[right]
82 this.authService.logout()
83 // Redirect to home page
84 this.router.navigate(['/videos/list'])
87 private computeIsUserHasAdminAccess () {
88 const right = this.getFirstAdminRightAvailable()
90 this.userHasAdminAccess = right !== undefined