2017-06-16 14:32:15 +02:00
|
|
|
import { Component, OnInit } from '@angular/core'
|
|
|
|
import { Router } from '@angular/router'
|
2017-12-01 09:20:19 +01:00
|
|
|
import { UserRight } from '../../../../shared/models/users/user-right.enum'
|
|
|
|
import { AuthService, AuthStatus, ServerService } from '../core'
|
|
|
|
import { User } from '../shared/users/user.model'
|
2016-08-12 16:52:10 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-menu',
|
2017-04-21 11:06:33 +02:00
|
|
|
templateUrl: './menu.component.html',
|
2017-05-01 18:05:28 +02:00
|
|
|
styleUrls: [ './menu.component.scss' ]
|
2016-08-12 16:52:10 +02:00
|
|
|
})
|
|
|
|
export class MenuComponent implements OnInit {
|
2017-12-01 09:20:19 +01:00
|
|
|
user: User
|
2017-06-16 14:32:15 +02:00
|
|
|
isLoggedIn: boolean
|
2017-10-27 16:55:03 +02:00
|
|
|
userHasAdminAccess = false
|
|
|
|
|
|
|
|
private routesPerRight = {
|
|
|
|
[UserRight.MANAGE_USERS]: '/admin/users',
|
2017-11-16 17:16:42 +01:00
|
|
|
[UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
|
2017-10-27 16:55:03 +02:00
|
|
|
[UserRight.MANAGE_VIDEO_ABUSES]: '/admin/video-abuses',
|
|
|
|
[UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist'
|
|
|
|
}
|
2016-08-12 16:52:10 +02:00
|
|
|
|
|
|
|
constructor (
|
|
|
|
private authService: AuthService,
|
2017-10-09 19:12:40 +02:00
|
|
|
private serverService: ServerService,
|
2016-08-12 16:52:10 +02:00
|
|
|
private router: Router
|
|
|
|
) {}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
ngOnInit () {
|
|
|
|
this.isLoggedIn = this.authService.isLoggedIn()
|
2017-12-01 09:20:19 +01:00
|
|
|
if (this.isLoggedIn === true) this.user = this.authService.getUser()
|
2017-10-27 16:55:03 +02:00
|
|
|
this.computeIsUserHasAdminAccess()
|
2016-08-12 16:52:10 +02:00
|
|
|
|
|
|
|
this.authService.loginChangedSource.subscribe(
|
|
|
|
status => {
|
|
|
|
if (status === AuthStatus.LoggedIn) {
|
2017-06-16 14:32:15 +02:00
|
|
|
this.isLoggedIn = true
|
2017-12-01 09:20:19 +01:00
|
|
|
this.user = this.authService.getUser()
|
2017-10-27 16:55:03 +02:00
|
|
|
this.computeIsUserHasAdminAccess()
|
2017-06-16 14:32:15 +02:00
|
|
|
console.log('Logged in.')
|
2016-08-12 16:52:10 +02:00
|
|
|
} else if (status === AuthStatus.LoggedOut) {
|
2017-06-16 14:32:15 +02:00
|
|
|
this.isLoggedIn = false
|
2017-12-01 09:20:19 +01:00
|
|
|
this.user = undefined
|
2017-10-27 16:55:03 +02:00
|
|
|
this.computeIsUserHasAdminAccess()
|
2017-06-16 14:32:15 +02:00
|
|
|
console.log('Logged out.')
|
2016-08-12 16:52:10 +02:00
|
|
|
} else {
|
2017-06-16 14:32:15 +02:00
|
|
|
console.error('Unknown auth status: ' + status)
|
2016-08-12 16:52:10 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-16 14:32:15 +02:00
|
|
|
)
|
2016-08-12 16:52:10 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 20:17:28 +02:00
|
|
|
isRegistrationAllowed () {
|
2018-05-22 19:43:13 +02:00
|
|
|
return this.serverService.getConfig().signup.allowed &&
|
|
|
|
this.serverService.getConfig().signup.allowedForCurrentIP
|
2017-04-10 20:29:33 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 16:55:03 +02:00
|
|
|
getFirstAdminRightAvailable () {
|
|
|
|
const user = this.authService.getUser()
|
|
|
|
if (!user) return undefined
|
|
|
|
|
|
|
|
const adminRights = [
|
|
|
|
UserRight.MANAGE_USERS,
|
2017-11-16 17:16:42 +01:00
|
|
|
UserRight.MANAGE_SERVER_FOLLOW,
|
2017-10-27 16:55:03 +02:00
|
|
|
UserRight.MANAGE_VIDEO_ABUSES,
|
|
|
|
UserRight.MANAGE_VIDEO_BLACKLIST
|
|
|
|
]
|
|
|
|
|
|
|
|
for (const adminRight of adminRights) {
|
|
|
|
if (user.hasRight(adminRight)) {
|
|
|
|
return adminRight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
getFirstAdminRouteAvailable () {
|
|
|
|
const right = this.getFirstAdminRightAvailable()
|
|
|
|
|
|
|
|
return this.routesPerRight[right]
|
2016-08-12 16:52:10 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 09:20:19 +01:00
|
|
|
logout (event: Event) {
|
|
|
|
event.preventDefault()
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
this.authService.logout()
|
2016-08-12 16:52:10 +02:00
|
|
|
// Redirect to home page
|
2017-06-16 14:32:15 +02:00
|
|
|
this.router.navigate(['/videos/list'])
|
2016-08-12 16:52:10 +02:00
|
|
|
}
|
2017-10-27 16:55:03 +02:00
|
|
|
|
|
|
|
private computeIsUserHasAdminAccess () {
|
|
|
|
const right = this.getFirstAdminRightAvailable()
|
|
|
|
|
|
|
|
this.userHasAdminAccess = right !== undefined
|
|
|
|
}
|
2016-08-12 16:52:10 +02:00
|
|
|
}
|