]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit } from '@angular/core' | |
2 | import { Router } from '@angular/router' | |
3 | ||
4 | import { AuthService, AuthStatus } from '../auth' | |
5 | import { ServerService } from '../server' | |
6 | import { UserRight } from '../../../../../shared/models/users/user-right.enum' | |
7 | ||
8 | @Component({ | |
9 | selector: 'my-menu', | |
10 | templateUrl: './menu.component.html', | |
11 | styleUrls: [ './menu.component.scss' ] | |
12 | }) | |
13 | export class MenuComponent implements OnInit { | |
14 | isLoggedIn: boolean | |
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 | } | |
24 | ||
25 | constructor ( | |
26 | private authService: AuthService, | |
27 | private serverService: ServerService, | |
28 | private router: Router | |
29 | ) {} | |
30 | ||
31 | ngOnInit () { | |
32 | this.isLoggedIn = this.authService.isLoggedIn() | |
33 | this.computeIsUserHasAdminAccess() | |
34 | ||
35 | this.authService.loginChangedSource.subscribe( | |
36 | status => { | |
37 | if (status === AuthStatus.LoggedIn) { | |
38 | this.isLoggedIn = true | |
39 | this.computeIsUserHasAdminAccess() | |
40 | console.log('Logged in.') | |
41 | } else if (status === AuthStatus.LoggedOut) { | |
42 | this.isLoggedIn = false | |
43 | this.computeIsUserHasAdminAccess() | |
44 | console.log('Logged out.') | |
45 | } else { | |
46 | console.error('Unknown auth status: ' + status) | |
47 | } | |
48 | } | |
49 | ) | |
50 | } | |
51 | ||
52 | isRegistrationAllowed () { | |
53 | return this.serverService.getConfig().signup.allowed | |
54 | } | |
55 | ||
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] | |
81 | } | |
82 | ||
83 | logout () { | |
84 | this.authService.logout() | |
85 | // Redirect to home page | |
86 | this.router.navigate(['/videos/list']) | |
87 | } | |
88 | ||
89 | private computeIsUserHasAdminAccess () { | |
90 | const right = this.getFirstAdminRightAvailable() | |
91 | ||
92 | this.userHasAdminAccess = right !== undefined | |
93 | } | |
94 | } |