]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/menu/menu.component.ts
Fix admin access to moderators
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
... / ...
CommitLineData
1import { Component, OnInit, ViewChild } from '@angular/core'
2import { UserRight } from '../../../../shared/models/users/user-right.enum'
3import { AuthService, AuthStatus, RedirectService, ServerService, ThemeService } from '../core'
4import { User } from '../shared/users/user.model'
5import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
6
7@Component({
8 selector: 'my-menu',
9 templateUrl: './menu.component.html',
10 styleUrls: [ './menu.component.scss' ]
11})
12export class MenuComponent implements OnInit {
13 @ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent
14
15 user: User
16 isLoggedIn: boolean
17 userHasAdminAccess = false
18
19 private routesPerRight = {
20 [UserRight.MANAGE_USERS]: '/admin/users',
21 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
22 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses',
23 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blacklist',
24 [UserRight.MANAGE_JOBS]: '/admin/jobs',
25 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
26 }
27
28 constructor (
29 private authService: AuthService,
30 private serverService: ServerService,
31 private redirectService: RedirectService,
32 private themeService: ThemeService
33 ) {}
34
35 ngOnInit () {
36 this.isLoggedIn = this.authService.isLoggedIn()
37 if (this.isLoggedIn === true) this.user = this.authService.getUser()
38 this.computeIsUserHasAdminAccess()
39
40 this.authService.loginChangedSource.subscribe(
41 status => {
42 if (status === AuthStatus.LoggedIn) {
43 this.isLoggedIn = true
44 this.user = this.authService.getUser()
45 this.computeIsUserHasAdminAccess()
46 console.log('Logged in.')
47 } else if (status === AuthStatus.LoggedOut) {
48 this.isLoggedIn = false
49 this.user = undefined
50 this.computeIsUserHasAdminAccess()
51 console.log('Logged out.')
52 } else {
53 console.error('Unknown auth status: ' + status)
54 }
55 }
56 )
57 }
58
59 isRegistrationAllowed () {
60 return this.serverService.getConfig().signup.allowed &&
61 this.serverService.getConfig().signup.allowedForCurrentIP
62 }
63
64 getFirstAdminRightAvailable () {
65 const user = this.authService.getUser()
66 if (!user) return undefined
67
68 const adminRights = [
69 UserRight.MANAGE_USERS,
70 UserRight.MANAGE_SERVER_FOLLOW,
71 UserRight.MANAGE_VIDEO_ABUSES,
72 UserRight.MANAGE_VIDEO_BLACKLIST,
73 UserRight.MANAGE_JOBS,
74 UserRight.MANAGE_CONFIGURATION
75 ]
76
77 for (const adminRight of adminRights) {
78 if (user.hasRight(adminRight)) {
79 return adminRight
80 }
81 }
82
83 return undefined
84 }
85
86 getFirstAdminRouteAvailable () {
87 const right = this.getFirstAdminRightAvailable()
88
89 return this.routesPerRight[right]
90 }
91
92 logout (event: Event) {
93 event.preventDefault()
94
95 this.authService.logout()
96 // Redirect to home page
97 this.redirectService.redirectToHomepage()
98 }
99
100 openLanguageChooser () {
101 this.languageChooserModal.show()
102 }
103
104 toggleDarkTheme () {
105 this.themeService.toggleDarkTheme()
106 }
107
108 private computeIsUserHasAdminAccess () {
109 const right = this.getFirstAdminRightAvailable()
110
111 this.userHasAdminAccess = right !== undefined
112 }
113}