]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { Component, OnInit } from '@angular/core' |
2 | import { Router } from '@angular/router' | |
b33f657c | 3 | import { UserRight } from '../../../../shared/models/users/user-right.enum' |
b1d40cff | 4 | import { AuthService, AuthStatus, RedirectService, ServerService } from '../core' |
b33f657c | 5 | import { User } from '../shared/users/user.model' |
602eb142 C |
6 | |
7 | @Component({ | |
8 | selector: 'my-menu', | |
383bfc83 | 9 | templateUrl: './menu.component.html', |
3eeeb87f | 10 | styleUrls: [ './menu.component.scss' ] |
602eb142 C |
11 | }) |
12 | export class MenuComponent implements OnInit { | |
b33f657c | 13 | user: User |
df98563e | 14 | isLoggedIn: boolean |
954605a8 C |
15 | userHasAdminAccess = false |
16 | ||
17 | private routesPerRight = { | |
18 | [UserRight.MANAGE_USERS]: '/admin/users', | |
4610bc5b | 19 | [UserRight.MANAGE_SERVER_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, |
b1d40cff | 27 | private redirectService: RedirectService |
602eb142 C |
28 | ) {} |
29 | ||
df98563e C |
30 | ngOnInit () { |
31 | this.isLoggedIn = this.authService.isLoggedIn() | |
b33f657c | 32 | if (this.isLoggedIn === true) this.user = this.authService.getUser() |
954605a8 | 33 | this.computeIsUserHasAdminAccess() |
602eb142 C |
34 | |
35 | this.authService.loginChangedSource.subscribe( | |
36 | status => { | |
37 | if (status === AuthStatus.LoggedIn) { | |
df98563e | 38 | this.isLoggedIn = true |
b33f657c | 39 | this.user = this.authService.getUser() |
954605a8 | 40 | this.computeIsUserHasAdminAccess() |
df98563e | 41 | console.log('Logged in.') |
602eb142 | 42 | } else if (status === AuthStatus.LoggedOut) { |
df98563e | 43 | this.isLoggedIn = false |
b33f657c | 44 | this.user = undefined |
954605a8 | 45 | this.computeIsUserHasAdminAccess() |
df98563e | 46 | console.log('Logged out.') |
602eb142 | 47 | } else { |
df98563e | 48 | console.error('Unknown auth status: ' + status) |
602eb142 C |
49 | } |
50 | } | |
df98563e | 51 | ) |
602eb142 C |
52 | } |
53 | ||
291e8d3e | 54 | isRegistrationAllowed () { |
ff2c1fe8 RK |
55 | return this.serverService.getConfig().signup.allowed && |
56 | this.serverService.getConfig().signup.allowedForCurrentIP | |
a184c71b C |
57 | } |
58 | ||
954605a8 C |
59 | getFirstAdminRightAvailable () { |
60 | const user = this.authService.getUser() | |
61 | if (!user) return undefined | |
62 | ||
63 | const adminRights = [ | |
64 | UserRight.MANAGE_USERS, | |
4610bc5b | 65 | UserRight.MANAGE_SERVER_FOLLOW, |
954605a8 C |
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] | |
602eb142 C |
83 | } |
84 | ||
b33f657c C |
85 | logout (event: Event) { |
86 | event.preventDefault() | |
87 | ||
df98563e | 88 | this.authService.logout() |
602eb142 | 89 | // Redirect to home page |
b1d40cff | 90 | this.redirectService.redirectToHomepage() |
602eb142 | 91 | } |
954605a8 C |
92 | |
93 | private computeIsUserHasAdminAccess () { | |
94 | const right = this.getFirstAdminRightAvailable() | |
95 | ||
96 | this.userHasAdminAccess = right !== undefined | |
97 | } | |
602eb142 | 98 | } |