]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/menu/menu.component.ts
872d29819bbbf343b889f6fdc897a5b85b7b7e65
[github/Chocobozzz/PeerTube.git] / client / src / app / core / menu / menu.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
3
4 import { AuthService, AuthStatus } from '../auth'
5 import { ServerService } from '../server'
6 import { UserRight } from '../../../../../shared/models/users/user-right.enum'
7
8 @Component({
9 selector: 'my-menu',
10 templateUrl: './menu.component.html',
11 styleUrls: [ './menu.component.scss' ]
12 })
13 export class MenuComponent implements OnInit {
14 isLoggedIn: boolean
15 userHasAdminAccess = false
16
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'
22 }
23
24 constructor (
25 private authService: AuthService,
26 private serverService: ServerService,
27 private router: Router
28 ) {}
29
30 ngOnInit () {
31 this.isLoggedIn = this.authService.isLoggedIn()
32 this.computeIsUserHasAdminAccess()
33
34 this.authService.loginChangedSource.subscribe(
35 status => {
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.')
44 } else {
45 console.error('Unknown auth status: ' + status)
46 }
47 }
48 )
49 }
50
51 isRegistrationAllowed () {
52 return this.serverService.getConfig().signup.allowed
53 }
54
55 getFirstAdminRightAvailable () {
56 const user = this.authService.getUser()
57 if (!user) return undefined
58
59 const adminRights = [
60 UserRight.MANAGE_USERS,
61 UserRight.MANAGE_APPLICATION_FOLLOW,
62 UserRight.MANAGE_VIDEO_ABUSES,
63 UserRight.MANAGE_VIDEO_BLACKLIST
64 ]
65
66 for (const adminRight of adminRights) {
67 if (user.hasRight(adminRight)) {
68 return adminRight
69 }
70 }
71
72 return undefined
73 }
74
75 getFirstAdminRouteAvailable () {
76 const right = this.getFirstAdminRightAvailable()
77
78 return this.routesPerRight[right]
79 }
80
81 logout () {
82 this.authService.logout()
83 // Redirect to home page
84 this.router.navigate(['/videos/list'])
85 }
86
87 private computeIsUserHasAdminAccess () {
88 const right = this.getFirstAdminRightAvailable()
89
90 this.userHasAdminAccess = right !== undefined
91 }
92 }