2019-02-11 09:30:29 +01:00
|
|
|
import { Component, OnDestroy, OnInit } from '@angular/core'
|
2017-09-05 21:29:39 +02:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router'
|
2018-05-15 11:55:51 +02:00
|
|
|
import { Subscription } from 'rxjs'
|
2019-07-30 09:59:19 +02:00
|
|
|
import { AuthService, Notifier } from '@app/core'
|
2017-10-19 17:33:32 +02:00
|
|
|
import { ServerService } from '../../../core'
|
2017-09-05 21:29:39 +02:00
|
|
|
import { UserEdit } from './user-edit'
|
2018-09-26 14:46:54 +02:00
|
|
|
import { User, UserUpdate } from '../../../../../../shared'
|
2018-06-04 16:21:17 +02:00
|
|
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
2018-06-05 10:58:45 +02:00
|
|
|
import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
|
2018-06-05 15:01:45 +02:00
|
|
|
import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
|
2018-09-26 14:46:54 +02:00
|
|
|
import { ConfigService } from '@app/+admin/config/shared/config.service'
|
2018-10-05 15:24:29 +02:00
|
|
|
import { UserService } from '@app/shared'
|
2019-04-15 10:49:46 +02:00
|
|
|
import { UserAdminFlag } from '@shared/models/users/user-flag.model'
|
2017-09-05 21:29:39 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-user-update',
|
2017-10-19 17:33:32 +02:00
|
|
|
templateUrl: './user-edit.component.html',
|
|
|
|
styleUrls: [ './user-edit.component.scss' ]
|
2017-09-05 21:29:39 +02:00
|
|
|
})
|
|
|
|
export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
|
|
|
|
error: string
|
|
|
|
userId: number
|
2018-10-06 13:54:00 +02:00
|
|
|
userEmail: string
|
2017-09-05 21:29:39 +02:00
|
|
|
username: string
|
|
|
|
|
|
|
|
private paramsSub: Subscription
|
|
|
|
|
|
|
|
constructor (
|
2018-06-05 10:58:45 +02:00
|
|
|
protected formValidatorService: FormValidatorService,
|
2017-10-19 17:33:32 +02:00
|
|
|
protected serverService: ServerService,
|
2018-09-26 14:46:54 +02:00
|
|
|
protected configService: ConfigService,
|
2019-07-30 09:59:19 +02:00
|
|
|
protected auth: AuthService,
|
2018-06-05 15:01:45 +02:00
|
|
|
private userValidatorsService: UserValidatorsService,
|
2017-09-05 21:29:39 +02:00
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
2018-12-19 16:04:34 +01:00
|
|
|
private notifier: Notifier,
|
2018-06-04 16:21:17 +02:00
|
|
|
private userService: UserService,
|
|
|
|
private i18n: I18n
|
2017-09-05 21:29:39 +02:00
|
|
|
) {
|
|
|
|
super()
|
2018-09-26 14:46:54 +02:00
|
|
|
|
|
|
|
this.buildQuotaOptions()
|
2017-09-05 21:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit () {
|
2019-12-18 15:31:54 +01:00
|
|
|
super.ngOnInit()
|
|
|
|
|
2018-08-28 09:01:35 +02:00
|
|
|
const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
|
2018-06-05 10:58:45 +02:00
|
|
|
this.buildForm({
|
2018-06-05 15:01:45 +02:00
|
|
|
email: this.userValidatorsService.USER_EMAIL,
|
|
|
|
role: this.userValidatorsService.USER_ROLE,
|
2018-08-28 09:01:35 +02:00
|
|
|
videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
|
2019-04-15 10:49:46 +02:00
|
|
|
videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
|
|
|
|
byPassAutoBlacklist: null
|
2018-06-05 10:58:45 +02:00
|
|
|
}, defaultValues)
|
2017-09-05 21:29:39 +02:00
|
|
|
|
|
|
|
this.paramsSub = this.route.params.subscribe(routeParams => {
|
|
|
|
const userId = routeParams['id']
|
|
|
|
this.userService.getUser(userId).subscribe(
|
|
|
|
user => this.onUserFetched(user),
|
|
|
|
|
2017-11-04 18:20:13 +01:00
|
|
|
err => this.error = err.message
|
2017-09-05 21:29:39 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy () {
|
|
|
|
this.paramsSub.unsubscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
formValidated () {
|
|
|
|
this.error = undefined
|
|
|
|
|
|
|
|
const userUpdate: UserUpdate = this.form.value
|
2019-04-15 10:49:46 +02:00
|
|
|
userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
|
2017-09-05 21:29:39 +02:00
|
|
|
|
|
|
|
// A select in HTML is always mapped as a string, we convert it to number
|
|
|
|
userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
|
2018-08-28 09:01:35 +02:00
|
|
|
userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
|
2017-09-05 21:29:39 +02:00
|
|
|
|
|
|
|
this.userService.updateUser(this.userId, userUpdate).subscribe(
|
|
|
|
() => {
|
2018-12-19 16:04:34 +01:00
|
|
|
this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username }))
|
2017-09-05 21:29:39 +02:00
|
|
|
this.router.navigate([ '/admin/users/list' ])
|
|
|
|
},
|
|
|
|
|
2017-11-04 18:20:13 +01:00
|
|
|
err => this.error = err.message
|
2017-09-05 21:29:39 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
isCreation () {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-17 10:16:52 +01:00
|
|
|
isPasswordOptional () {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-05 21:29:39 +02:00
|
|
|
getFormButtonTitle () {
|
2018-06-04 16:21:17 +02:00
|
|
|
return this.i18n('Update user')
|
2017-09-05 21:29:39 +02:00
|
|
|
}
|
|
|
|
|
2018-10-06 13:54:00 +02:00
|
|
|
resetPassword () {
|
|
|
|
this.userService.askResetPassword(this.userEmail).subscribe(
|
|
|
|
() => {
|
2019-02-11 09:30:29 +01:00
|
|
|
this.notifier.success(
|
2018-10-06 13:54:00 +02:00
|
|
|
this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username })
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
err => this.error = err.message
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-09-05 21:29:39 +02:00
|
|
|
private onUserFetched (userJson: User) {
|
|
|
|
this.userId = userJson.id
|
|
|
|
this.username = userJson.username
|
2018-10-06 13:54:00 +02:00
|
|
|
this.userEmail = userJson.email
|
2017-09-05 21:29:39 +02:00
|
|
|
|
|
|
|
this.form.patchValue({
|
|
|
|
email: userJson.email,
|
2017-10-27 16:55:03 +02:00
|
|
|
role: userJson.role,
|
2018-08-28 09:01:35 +02:00
|
|
|
videoQuota: userJson.videoQuota,
|
2019-04-15 10:49:46 +02:00
|
|
|
videoQuotaDaily: userJson.videoQuotaDaily,
|
|
|
|
byPassAutoBlacklist: userJson.adminFlags & UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST
|
2017-09-05 21:29:39 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|