]>
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', | |
19 | [UserRight.MANAGE_PODS]: '/admin/friends', | |
20 | [UserRight.MANAGE_REQUEST_SCHEDULERS]: '/admin/requests/stats', | |
21 | [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/video-abuses', | |
22 | [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist' | |
23 | } | |
602eb142 C |
24 | |
25 | constructor ( | |
26 | private authService: AuthService, | |
db7af09b | 27 | private serverService: ServerService, |
602eb142 C |
28 | private router: Router |
29 | ) {} | |
30 | ||
df98563e C |
31 | ngOnInit () { |
32 | this.isLoggedIn = this.authService.isLoggedIn() | |
954605a8 | 33 | this.computeIsUserHasAdminAccess() |
602eb142 C |
34 | |
35 | this.authService.loginChangedSource.subscribe( | |
36 | status => { | |
37 | if (status === AuthStatus.LoggedIn) { | |
df98563e | 38 | this.isLoggedIn = true |
954605a8 | 39 | this.computeIsUserHasAdminAccess() |
df98563e | 40 | console.log('Logged in.') |
602eb142 | 41 | } else if (status === AuthStatus.LoggedOut) { |
df98563e | 42 | this.isLoggedIn = false |
954605a8 | 43 | this.computeIsUserHasAdminAccess() |
df98563e | 44 | console.log('Logged out.') |
602eb142 | 45 | } else { |
df98563e | 46 | console.error('Unknown auth status: ' + status) |
602eb142 C |
47 | } |
48 | } | |
df98563e | 49 | ) |
602eb142 C |
50 | } |
51 | ||
291e8d3e | 52 | isRegistrationAllowed () { |
db7af09b | 53 | return this.serverService.getConfig().signup.allowed |
a184c71b C |
54 | } |
55 | ||
954605a8 C |
56 | getFirstAdminRightAvailable () { |
57 | const user = this.authService.getUser() | |
58 | if (!user) return undefined | |
59 | ||
60 | const adminRights = [ | |
61 | UserRight.MANAGE_USERS, | |
62 | UserRight.MANAGE_PODS, | |
63 | UserRight.MANAGE_REQUEST_SCHEDULERS, | |
64 | UserRight.MANAGE_VIDEO_ABUSES, | |
65 | UserRight.MANAGE_VIDEO_BLACKLIST | |
66 | ] | |
67 | ||
68 | for (const adminRight of adminRights) { | |
69 | if (user.hasRight(adminRight)) { | |
70 | return adminRight | |
71 | } | |
72 | } | |
73 | ||
74 | return undefined | |
75 | } | |
76 | ||
77 | getFirstAdminRouteAvailable () { | |
78 | const right = this.getFirstAdminRightAvailable() | |
79 | ||
80 | return this.routesPerRight[right] | |
602eb142 C |
81 | } |
82 | ||
df98563e C |
83 | logout () { |
84 | this.authService.logout() | |
602eb142 | 85 | // Redirect to home page |
df98563e | 86 | this.router.navigate(['/videos/list']) |
602eb142 | 87 | } |
954605a8 C |
88 | |
89 | private computeIsUserHasAdminAccess () { | |
90 | const right = this.getFirstAdminRightAvailable() | |
91 | ||
92 | this.userHasAdminAccess = right !== undefined | |
93 | } | |
602eb142 | 94 | } |