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