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