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