1 import { Component, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { UserRight } from '../../../../shared/models/users/user-right.enum'
4 import { AuthService, AuthStatus, ServerService } from '../core'
5 import { User } from '../shared/users/user.model'
9 templateUrl: './menu.component.html',
10 styleUrls: [ './menu.component.scss' ]
12 export class MenuComponent implements OnInit {
15 userHasAdminAccess = false
17 private routesPerRight = {
18 [UserRight.MANAGE_USERS]: '/admin/users',
19 [UserRight.MANAGE_SERVER_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 if (this.isLoggedIn === true) this.user = this.authService.getUser()
33 this.computeIsUserHasAdminAccess()
35 this.authService.loginChangedSource.subscribe(
37 if (status === AuthStatus.LoggedIn) {
38 this.isLoggedIn = true
39 this.user = this.authService.getUser()
40 this.computeIsUserHasAdminAccess()
41 console.log('Logged in.')
42 } else if (status === AuthStatus.LoggedOut) {
43 this.isLoggedIn = false
45 this.computeIsUserHasAdminAccess()
46 console.log('Logged out.')
48 console.error('Unknown auth status: ' + status)
55 return this.user.getAvatarUrl()
58 isRegistrationAllowed () {
59 return this.serverService.getConfig().signup.allowed
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.router.navigate(['/videos/list'])
96 private computeIsUserHasAdminAccess () {
97 const right = this.getFirstAdminRightAvailable()
99 this.userHasAdminAccess = right !== undefined