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