]>
Commit | Line | Data |
---|---|---|
8afc19a6 | 1 | import { Component, OnInit, ViewChild } from '@angular/core' |
b33f657c | 2 | import { UserRight } from '../../../../shared/models/users/user-right.enum' |
1a00c561 | 3 | import { AuthService, AuthStatus, RedirectService, ServerService, ThemeService } from '../core' |
b33f657c | 4 | import { User } from '../shared/users/user.model' |
8afc19a6 | 5 | import { LanguageChooserComponent } from '@app/menu/language-chooser.component' |
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 { | |
8afc19a6 C |
13 | @ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent |
14 | ||
b33f657c | 15 | user: User |
df98563e | 16 | isLoggedIn: boolean |
954605a8 C |
17 | userHasAdminAccess = false |
18 | ||
19 | private routesPerRight = { | |
20 | [UserRight.MANAGE_USERS]: '/admin/users', | |
4610bc5b | 21 | [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends', |
ad76628b C |
22 | [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses', |
23 | [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blacklist', | |
24 | [UserRight.MANAGE_JOBS]: '/admin/jobs', | |
25 | [UserRight.MANAGE_CONFIGURATION]: '/admin/config' | |
954605a8 | 26 | } |
602eb142 C |
27 | |
28 | constructor ( | |
29 | private authService: AuthService, | |
db7af09b | 30 | private serverService: ServerService, |
8c985ef5 | 31 | private redirectService: RedirectService, |
1a00c561 | 32 | private themeService: ThemeService |
602eb142 C |
33 | ) {} |
34 | ||
df98563e C |
35 | ngOnInit () { |
36 | this.isLoggedIn = this.authService.isLoggedIn() | |
b33f657c | 37 | if (this.isLoggedIn === true) this.user = this.authService.getUser() |
954605a8 | 38 | this.computeIsUserHasAdminAccess() |
602eb142 C |
39 | |
40 | this.authService.loginChangedSource.subscribe( | |
41 | status => { | |
42 | if (status === AuthStatus.LoggedIn) { | |
df98563e | 43 | this.isLoggedIn = true |
b33f657c | 44 | this.user = this.authService.getUser() |
954605a8 | 45 | this.computeIsUserHasAdminAccess() |
df98563e | 46 | console.log('Logged in.') |
602eb142 | 47 | } else if (status === AuthStatus.LoggedOut) { |
df98563e | 48 | this.isLoggedIn = false |
b33f657c | 49 | this.user = undefined |
954605a8 | 50 | this.computeIsUserHasAdminAccess() |
df98563e | 51 | console.log('Logged out.') |
602eb142 | 52 | } else { |
df98563e | 53 | console.error('Unknown auth status: ' + status) |
602eb142 C |
54 | } |
55 | } | |
df98563e | 56 | ) |
602eb142 C |
57 | } |
58 | ||
291e8d3e | 59 | isRegistrationAllowed () { |
ff2c1fe8 RK |
60 | return this.serverService.getConfig().signup.allowed && |
61 | this.serverService.getConfig().signup.allowedForCurrentIP | |
a184c71b C |
62 | } |
63 | ||
954605a8 C |
64 | getFirstAdminRightAvailable () { |
65 | const user = this.authService.getUser() | |
66 | if (!user) return undefined | |
67 | ||
68 | const adminRights = [ | |
69 | UserRight.MANAGE_USERS, | |
4610bc5b | 70 | UserRight.MANAGE_SERVER_FOLLOW, |
954605a8 | 71 | UserRight.MANAGE_VIDEO_ABUSES, |
ad76628b C |
72 | UserRight.MANAGE_VIDEO_BLACKLIST, |
73 | UserRight.MANAGE_JOBS, | |
74 | UserRight.MANAGE_CONFIGURATION | |
954605a8 C |
75 | ] |
76 | ||
77 | for (const adminRight of adminRights) { | |
78 | if (user.hasRight(adminRight)) { | |
79 | return adminRight | |
80 | } | |
81 | } | |
82 | ||
83 | return undefined | |
84 | } | |
85 | ||
86 | getFirstAdminRouteAvailable () { | |
87 | const right = this.getFirstAdminRightAvailable() | |
88 | ||
89 | return this.routesPerRight[right] | |
602eb142 C |
90 | } |
91 | ||
b33f657c C |
92 | logout (event: Event) { |
93 | event.preventDefault() | |
94 | ||
df98563e | 95 | this.authService.logout() |
602eb142 | 96 | // Redirect to home page |
b1d40cff | 97 | this.redirectService.redirectToHomepage() |
602eb142 | 98 | } |
954605a8 | 99 | |
8afc19a6 C |
100 | openLanguageChooser () { |
101 | this.languageChooserModal.show() | |
102 | } | |
103 | ||
9a0fc840 | 104 | toggleDarkTheme () { |
1a00c561 | 105 | this.themeService.toggleDarkTheme() |
9a0fc840 RK |
106 | } |
107 | ||
954605a8 C |
108 | private computeIsUserHasAdminAccess () { |
109 | const right = this.getFirstAdminRightAvailable() | |
110 | ||
111 | this.userHasAdminAccess = right !== undefined | |
112 | } | |
602eb142 | 113 | } |