diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-12-11 11:06:32 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-12-11 11:06:32 +0100 |
commit | fada8d75550dc7365f7e18ee1569b9406251d660 (patch) | |
tree | db9dc01c18693824f83fce5020f4c1f3ae7c0865 /client/src/app/menu/menu.component.ts | |
parent | 492fd28167f770d79a430fc57451b5a9e075d8e7 (diff) | |
parent | c2830fa8f84f61462098bf36add824f89436dfa9 (diff) | |
download | PeerTube-fada8d75550dc7365f7e18ee1569b9406251d660.tar.gz PeerTube-fada8d75550dc7365f7e18ee1569b9406251d660.tar.zst PeerTube-fada8d75550dc7365f7e18ee1569b9406251d660.zip |
Merge branch 'feature/design' into develop
Diffstat (limited to 'client/src/app/menu/menu.component.ts')
-rw-r--r-- | client/src/app/menu/menu.component.ts | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/client/src/app/menu/menu.component.ts b/client/src/app/menu/menu.component.ts new file mode 100644 index 000000000..8b8b714a8 --- /dev/null +++ b/client/src/app/menu/menu.component.ts | |||
@@ -0,0 +1,101 @@ | |||
1 | import { Component, OnInit } from '@angular/core' | ||
2 | import { Router } from '@angular/router' | ||
3 | import { UserRight } from '../../../../shared/models/users/user-right.enum' | ||
4 | import { AuthService, AuthStatus, ServerService } from '../core' | ||
5 | import { User } from '../shared/users/user.model' | ||
6 | |||
7 | @Component({ | ||
8 | selector: 'my-menu', | ||
9 | templateUrl: './menu.component.html', | ||
10 | styleUrls: [ './menu.component.scss' ] | ||
11 | }) | ||
12 | export class MenuComponent implements OnInit { | ||
13 | user: User | ||
14 | isLoggedIn: boolean | ||
15 | userHasAdminAccess = false | ||
16 | |||
17 | private routesPerRight = { | ||
18 | [UserRight.MANAGE_USERS]: '/admin/users', | ||
19 | [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends', | ||
20 | [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/video-abuses', | ||
21 | [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist' | ||
22 | } | ||
23 | |||
24 | constructor ( | ||
25 | private authService: AuthService, | ||
26 | private serverService: ServerService, | ||
27 | private router: Router | ||
28 | ) {} | ||
29 | |||
30 | ngOnInit () { | ||
31 | this.isLoggedIn = this.authService.isLoggedIn() | ||
32 | if (this.isLoggedIn === true) this.user = this.authService.getUser() | ||
33 | this.computeIsUserHasAdminAccess() | ||
34 | |||
35 | this.authService.loginChangedSource.subscribe( | ||
36 | status => { | ||
37 | if (status === AuthStatus.LoggedIn) { | ||
38 | this.isLoggedIn = true | ||
39 | this.user = this.authService.getUser() | ||
40 | this.computeIsUserHasAdminAccess() | ||
41 | console.log('Logged in.') | ||
42 | } else if (status === AuthStatus.LoggedOut) { | ||
43 | this.isLoggedIn = false | ||
44 | this.user = undefined | ||
45 | this.computeIsUserHasAdminAccess() | ||
46 | console.log('Logged out.') | ||
47 | } else { | ||
48 | console.error('Unknown auth status: ' + status) | ||
49 | } | ||
50 | } | ||
51 | ) | ||
52 | } | ||
53 | |||
54 | getUserAvatarPath () { | ||
55 | return this.user.getAvatarPath() | ||
56 | } | ||
57 | |||
58 | isRegistrationAllowed () { | ||
59 | return this.serverService.getConfig().signup.allowed | ||
60 | } | ||
61 | |||
62 | getFirstAdminRightAvailable () { | ||
63 | const user = this.authService.getUser() | ||
64 | if (!user) return undefined | ||
65 | |||
66 | const adminRights = [ | ||
67 | UserRight.MANAGE_USERS, | ||
68 | UserRight.MANAGE_SERVER_FOLLOW, | ||
69 | UserRight.MANAGE_VIDEO_ABUSES, | ||
70 | UserRight.MANAGE_VIDEO_BLACKLIST | ||
71 | ] | ||
72 | |||
73 | for (const adminRight of adminRights) { | ||
74 | if (user.hasRight(adminRight)) { | ||
75 | return adminRight | ||
76 | } | ||
77 | } | ||
78 | |||
79 | return undefined | ||
80 | } | ||
81 | |||
82 | getFirstAdminRouteAvailable () { | ||
83 | const right = this.getFirstAdminRightAvailable() | ||
84 | |||
85 | return this.routesPerRight[right] | ||
86 | } | ||
87 | |||
88 | logout (event: Event) { | ||
89 | event.preventDefault() | ||
90 | |||
91 | this.authService.logout() | ||
92 | // Redirect to home page | ||
93 | this.router.navigate(['/videos/list']) | ||
94 | } | ||
95 | |||
96 | private computeIsUserHasAdminAccess () { | ||
97 | const right = this.getFirstAdminRightAvailable() | ||
98 | |||
99 | this.userHasAdminAccess = right !== undefined | ||
100 | } | ||
101 | } | ||