]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/menu/menu.component.ts
feature: IP filtering on signup page
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
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'
6
7 @Component({
8 selector: 'my-menu',
9 templateUrl: './menu.component.html',
10 styleUrls: [ './menu.component.scss' ]
11 })
12 export class MenuComponent implements OnInit {
13 user: User
14 isLoggedIn: boolean
15 userHasAdminAccess = false
16
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'
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 if (this.isLoggedIn === true) this.user = this.authService.getUser()
33 this.computeIsUserHasAdminAccess()
34
35 this.authService.loginChangedSource.subscribe(
36 status => {
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
44 this.user = undefined
45 this.computeIsUserHasAdminAccess()
46 console.log('Logged out.')
47 } else {
48 console.error('Unknown auth status: ' + status)
49 }
50 }
51 )
52 }
53
54 isRegistrationAllowed () {
55 return this.serverService.getConfig().signup.allowed &&
56 this.serverService.getConfig().signup.allowedForCurrentIP
57 }
58
59 getFirstAdminRightAvailable () {
60 const user = this.authService.getUser()
61 if (!user) return undefined
62
63 const adminRights = [
64 UserRight.MANAGE_USERS,
65 UserRight.MANAGE_SERVER_FOLLOW,
66 UserRight.MANAGE_VIDEO_ABUSES,
67 UserRight.MANAGE_VIDEO_BLACKLIST
68 ]
69
70 for (const adminRight of adminRights) {
71 if (user.hasRight(adminRight)) {
72 return adminRight
73 }
74 }
75
76 return undefined
77 }
78
79 getFirstAdminRouteAvailable () {
80 const right = this.getFirstAdminRightAvailable()
81
82 return this.routesPerRight[right]
83 }
84
85 logout (event: Event) {
86 event.preventDefault()
87
88 this.authService.logout()
89 // Redirect to home page
90 this.router.navigate(['/videos/list'])
91 }
92
93 private computeIsUserHasAdminAccess () {
94 const right = this.getFirstAdminRightAvailable()
95
96 this.userHasAdminAccess = right !== undefined
97 }
98 }