]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit, ViewChild } from '@angular/core' | |
2 | import { UserRight } from '../../../../shared/models/users/user-right.enum' | |
3 | import { AuthService, AuthStatus, RedirectService, ServerService, ThemeService } from '../core' | |
4 | import { User } from '../shared/users/user.model' | |
5 | import { LanguageChooserComponent } from '@app/menu/language-chooser.component' | |
6 | import { HotkeysService } from 'angular2-hotkeys' | |
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 | @ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent | |
15 | ||
16 | user: User | |
17 | isLoggedIn: boolean | |
18 | userHasAdminAccess = false | |
19 | helpVisible = false | |
20 | ||
21 | private routesPerRight: { [ role in UserRight ]?: string } = { | |
22 | [UserRight.MANAGE_USERS]: '/admin/users', | |
23 | [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends', | |
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' | |
28 | } | |
29 | ||
30 | constructor ( | |
31 | private authService: AuthService, | |
32 | private serverService: ServerService, | |
33 | private redirectService: RedirectService, | |
34 | private themeService: ThemeService, | |
35 | private hotkeysService: HotkeysService | |
36 | ) {} | |
37 | ||
38 | ngOnInit () { | |
39 | this.isLoggedIn = this.authService.isLoggedIn() | |
40 | if (this.isLoggedIn === true) this.user = this.authService.getUser() | |
41 | this.computeIsUserHasAdminAccess() | |
42 | ||
43 | this.authService.loginChangedSource.subscribe( | |
44 | status => { | |
45 | if (status === AuthStatus.LoggedIn) { | |
46 | this.isLoggedIn = true | |
47 | this.user = this.authService.getUser() | |
48 | this.computeIsUserHasAdminAccess() | |
49 | console.log('Logged in.') | |
50 | } else if (status === AuthStatus.LoggedOut) { | |
51 | this.isLoggedIn = false | |
52 | this.user = undefined | |
53 | this.computeIsUserHasAdminAccess() | |
54 | console.log('Logged out.') | |
55 | } else { | |
56 | console.error('Unknown auth status: ' + status) | |
57 | } | |
58 | } | |
59 | ) | |
60 | ||
61 | this.hotkeysService.cheatSheetToggle.subscribe(isOpen => { | |
62 | this.helpVisible = isOpen | |
63 | }) | |
64 | } | |
65 | ||
66 | isRegistrationAllowed () { | |
67 | return this.serverService.getConfig().signup.allowed && | |
68 | this.serverService.getConfig().signup.allowedForCurrentIP | |
69 | } | |
70 | ||
71 | getFirstAdminRightAvailable () { | |
72 | const user = this.authService.getUser() | |
73 | if (!user) return undefined | |
74 | ||
75 | const adminRights = [ | |
76 | UserRight.MANAGE_USERS, | |
77 | UserRight.MANAGE_SERVER_FOLLOW, | |
78 | UserRight.MANAGE_VIDEO_ABUSES, | |
79 | UserRight.MANAGE_VIDEO_BLACKLIST, | |
80 | UserRight.MANAGE_JOBS, | |
81 | UserRight.MANAGE_CONFIGURATION | |
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] | |
97 | } | |
98 | ||
99 | logout (event: Event) { | |
100 | event.preventDefault() | |
101 | ||
102 | this.authService.logout() | |
103 | // Redirect to home page | |
104 | this.redirectService.redirectToHomepage() | |
105 | } | |
106 | ||
107 | openLanguageChooser () { | |
108 | this.languageChooserModal.show() | |
109 | } | |
110 | ||
111 | openHotkeysCheatSheet () { | |
112 | this.hotkeysService.cheatSheetToggle.next(!this.helpVisible) | |
113 | } | |
114 | ||
115 | toggleDarkTheme () { | |
116 | this.themeService.toggleDarkTheme() | |
117 | } | |
118 | ||
119 | private computeIsUserHasAdminAccess () { | |
120 | const right = this.getFirstAdminRightAvailable() | |
121 | ||
122 | this.userHasAdminAccess = right !== undefined | |
123 | } | |
124 | } |