2018-06-28 07:59:48 -04:00
|
|
|
import { Component, OnInit, ViewChild } from '@angular/core'
|
2017-12-01 03:20:19 -05:00
|
|
|
import { UserRight } from '../../../../shared/models/users/user-right.enum'
|
2018-06-04 10:21:17 -04:00
|
|
|
import { AuthService, AuthStatus, RedirectService, ServerService } from '../core'
|
2017-12-01 03:20:19 -05:00
|
|
|
import { User } from '../shared/users/user.model'
|
2018-06-28 07:59:48 -04:00
|
|
|
import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
|
2016-08-12 10:52:10 -04:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-menu',
|
2017-04-21 05:06:33 -04:00
|
|
|
templateUrl: './menu.component.html',
|
2017-05-01 12:05:28 -04:00
|
|
|
styleUrls: [ './menu.component.scss' ]
|
2016-08-12 10:52:10 -04:00
|
|
|
})
|
|
|
|
export class MenuComponent implements OnInit {
|
2018-06-28 07:59:48 -04:00
|
|
|
@ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent
|
|
|
|
|
2017-12-01 03:20:19 -05:00
|
|
|
user: User
|
2017-06-16 08:32:15 -04:00
|
|
|
isLoggedIn: boolean
|
2017-10-27 10:55:03 -04:00
|
|
|
userHasAdminAccess = false
|
|
|
|
|
|
|
|
private routesPerRight = {
|
|
|
|
[UserRight.MANAGE_USERS]: '/admin/users',
|
2017-11-16 11:16:42 -05:00
|
|
|
[UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
|
2017-10-27 10:55:03 -04:00
|
|
|
[UserRight.MANAGE_VIDEO_ABUSES]: '/admin/video-abuses',
|
|
|
|
[UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist'
|
|
|
|
}
|
2018-09-03 19:28:04 -04:00
|
|
|
private theme = document.querySelector('body')
|
|
|
|
private previousTheme = { }
|
2016-08-12 10:52:10 -04:00
|
|
|
|
|
|
|
constructor (
|
|
|
|
private authService: AuthService,
|
2017-10-09 13:12:40 -04:00
|
|
|
private serverService: ServerService,
|
2018-06-04 10:21:17 -04:00
|
|
|
private redirectService: RedirectService
|
2016-08-12 10:52:10 -04:00
|
|
|
) {}
|
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
ngOnInit () {
|
|
|
|
this.isLoggedIn = this.authService.isLoggedIn()
|
2017-12-01 03:20:19 -05:00
|
|
|
if (this.isLoggedIn === true) this.user = this.authService.getUser()
|
2017-10-27 10:55:03 -04:00
|
|
|
this.computeIsUserHasAdminAccess()
|
2016-08-12 10:52:10 -04:00
|
|
|
|
|
|
|
this.authService.loginChangedSource.subscribe(
|
|
|
|
status => {
|
|
|
|
if (status === AuthStatus.LoggedIn) {
|
2017-06-16 08:32:15 -04:00
|
|
|
this.isLoggedIn = true
|
2017-12-01 03:20:19 -05:00
|
|
|
this.user = this.authService.getUser()
|
2017-10-27 10:55:03 -04:00
|
|
|
this.computeIsUserHasAdminAccess()
|
2017-06-16 08:32:15 -04:00
|
|
|
console.log('Logged in.')
|
2016-08-12 10:52:10 -04:00
|
|
|
} else if (status === AuthStatus.LoggedOut) {
|
2017-06-16 08:32:15 -04:00
|
|
|
this.isLoggedIn = false
|
2017-12-01 03:20:19 -05:00
|
|
|
this.user = undefined
|
2017-10-27 10:55:03 -04:00
|
|
|
this.computeIsUserHasAdminAccess()
|
2017-06-16 08:32:15 -04:00
|
|
|
console.log('Logged out.')
|
2016-08-12 10:52:10 -04:00
|
|
|
} else {
|
2017-06-16 08:32:15 -04:00
|
|
|
console.error('Unknown auth status: ' + status)
|
2016-08-12 10:52:10 -04:00
|
|
|
}
|
|
|
|
}
|
2017-06-16 08:32:15 -04:00
|
|
|
)
|
2018-09-03 19:28:04 -04:00
|
|
|
|
|
|
|
// initialise the alternative theme with dark theme colors
|
|
|
|
this.previousTheme['mainBackgroundColor'] = '#111111'
|
|
|
|
this.previousTheme['mainForegroundColor'] = '#fff'
|
|
|
|
this.previousTheme['submenuColor'] = 'rgb(32,32,32)'
|
|
|
|
this.previousTheme['inputColor'] = 'gray'
|
|
|
|
this.previousTheme['inputPlaceholderColor'] = '#fff'
|
2016-08-12 10:52:10 -04:00
|
|
|
}
|
|
|
|
|
2017-07-25 14:17:28 -04:00
|
|
|
isRegistrationAllowed () {
|
2018-05-22 13:43:13 -04:00
|
|
|
return this.serverService.getConfig().signup.allowed &&
|
|
|
|
this.serverService.getConfig().signup.allowedForCurrentIP
|
2017-04-10 14:29:33 -04:00
|
|
|
}
|
|
|
|
|
2017-10-27 10:55:03 -04:00
|
|
|
getFirstAdminRightAvailable () {
|
|
|
|
const user = this.authService.getUser()
|
|
|
|
if (!user) return undefined
|
|
|
|
|
|
|
|
const adminRights = [
|
|
|
|
UserRight.MANAGE_USERS,
|
2017-11-16 11:16:42 -05:00
|
|
|
UserRight.MANAGE_SERVER_FOLLOW,
|
2017-10-27 10:55:03 -04: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 10:52:10 -04:00
|
|
|
}
|
|
|
|
|
2017-12-01 03:20:19 -05:00
|
|
|
logout (event: Event) {
|
|
|
|
event.preventDefault()
|
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
this.authService.logout()
|
2016-08-12 10:52:10 -04:00
|
|
|
// Redirect to home page
|
2018-06-04 10:21:17 -04:00
|
|
|
this.redirectService.redirectToHomepage()
|
2016-08-12 10:52:10 -04:00
|
|
|
}
|
2017-10-27 10:55:03 -04:00
|
|
|
|
2018-06-28 07:59:48 -04:00
|
|
|
openLanguageChooser () {
|
|
|
|
this.languageChooserModal.show()
|
|
|
|
}
|
|
|
|
|
2018-09-03 19:28:04 -04:00
|
|
|
toggleDarkTheme () {
|
|
|
|
// switch properties
|
|
|
|
this.switchProperty('mainBackgroundColor')
|
|
|
|
this.switchProperty('mainForegroundColor')
|
|
|
|
this.switchProperty('submenuColor')
|
|
|
|
this.switchProperty('inputColor')
|
|
|
|
this.switchProperty('inputPlaceholderColor')
|
|
|
|
}
|
|
|
|
|
|
|
|
private switchProperty (property, newValue?) {
|
|
|
|
const propertyOldvalue = window.getComputedStyle(this.theme).getPropertyValue('--' + property)
|
|
|
|
this.theme.style.setProperty('--' + property, (newValue) ? newValue : this.previousTheme[property])
|
|
|
|
this.previousTheme[property] = propertyOldvalue
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:55:03 -04:00
|
|
|
private computeIsUserHasAdminAccess () {
|
|
|
|
const right = this.getFirstAdminRightAvailable()
|
|
|
|
|
|
|
|
this.userHasAdminAccess = right !== undefined
|
|
|
|
}
|
2016-08-12 10:52:10 -04:00
|
|
|
}
|