]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { Component, OnInit } from '@angular/core' |
2 | import { Router } from '@angular/router' | |
602eb142 | 3 | |
df98563e | 4 | import { AuthService, AuthStatus } from '../auth' |
db7af09b | 5 | import { ServerService } from '../server' |
954605a8 | 6 | import { UserRight } from '../../../../../shared/models/users/user-right.enum' |
602eb142 C |
7 | |
8 | @Component({ | |
9 | selector: 'my-menu', | |
383bfc83 | 10 | templateUrl: './menu.component.html', |
3eeeb87f | 11 | styleUrls: [ './menu.component.scss' ] |
602eb142 C |
12 | }) |
13 | export class MenuComponent implements OnInit { | |
df98563e | 14 | isLoggedIn: boolean |
954605a8 C |
15 | userHasAdminAccess = false |
16 | ||
17 | private routesPerRight = { | |
18 | [UserRight.MANAGE_USERS]: '/admin/users', | |
51548b31 | 19 | [UserRight.MANAGE_APPLICATION_FOLLOW]: '/admin/friends', |
954605a8 C |
20 | [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/video-abuses', |
21 | [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist' | |
22 | } | |
602eb142 C |
23 | |
24 | constructor ( | |
25 | private authService: AuthService, | |
db7af09b | 26 | private serverService: ServerService, |
602eb142 C |
27 | private router: Router |
28 | ) {} | |
29 | ||
df98563e C |
30 | ngOnInit () { |
31 | this.isLoggedIn = this.authService.isLoggedIn() | |
954605a8 | 32 | this.computeIsUserHasAdminAccess() |
602eb142 C |
33 | |
34 | this.authService.loginChangedSource.subscribe( | |
35 | status => { | |
36 | if (status === AuthStatus.LoggedIn) { | |
df98563e | 37 | this.isLoggedIn = true |
954605a8 | 38 | this.computeIsUserHasAdminAccess() |
df98563e | 39 | console.log('Logged in.') |
602eb142 | 40 | } else if (status === AuthStatus.LoggedOut) { |
df98563e | 41 | this.isLoggedIn = false |
954605a8 | 42 | this.computeIsUserHasAdminAccess() |
df98563e | 43 | console.log('Logged out.') |
602eb142 | 44 | } else { |
df98563e | 45 | console.error('Unknown auth status: ' + status) |
602eb142 C |
46 | } |
47 | } | |
df98563e | 48 | ) |
602eb142 C |
49 | } |
50 | ||
291e8d3e | 51 | isRegistrationAllowed () { |
db7af09b | 52 | return this.serverService.getConfig().signup.allowed |
a184c71b C |
53 | } |
54 | ||
954605a8 C |
55 | getFirstAdminRightAvailable () { |
56 | const user = this.authService.getUser() | |
57 | if (!user) return undefined | |
58 | ||
59 | const adminRights = [ | |
60 | UserRight.MANAGE_USERS, | |
51548b31 | 61 | UserRight.MANAGE_APPLICATION_FOLLOW, |
954605a8 C |
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] | |
602eb142 C |
79 | } |
80 | ||
df98563e C |
81 | logout () { |
82 | this.authService.logout() | |
602eb142 | 83 | // Redirect to home page |
df98563e | 84 | this.router.navigate(['/videos/list']) |
602eb142 | 85 | } |
954605a8 C |
86 | |
87 | private computeIsUserHasAdminAccess () { | |
88 | const right = this.getFirstAdminRightAvailable() | |
89 | ||
90 | this.userHasAdminAccess = right !== undefined | |
91 | } | |
602eb142 | 92 | } |