1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Subscription } from 'rxjs'
4 import { AuthService, Notifier } from '@app/core'
5 import { ServerService } from '../../../core'
6 import { UserEdit } from './user-edit'
7 import { User, UserUpdate } from '../../../../../../shared'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10 import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
11 import { ConfigService } from '@app/+admin/config/shared/config.service'
12 import { UserService } from '@app/shared'
13 import { UserAdminFlag } from '@shared/models/users/user-flag.model'
16 selector: 'my-user-update',
17 templateUrl: './user-edit.component.html',
18 styleUrls: [ './user-edit.component.scss' ]
20 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
26 private paramsSub: Subscription
29 protected formValidatorService: FormValidatorService,
30 protected serverService: ServerService,
31 protected configService: ConfigService,
32 protected auth: AuthService,
33 private userValidatorsService: UserValidatorsService,
34 private route: ActivatedRoute,
35 private router: Router,
36 private notifier: Notifier,
37 private userService: UserService,
42 this.buildQuotaOptions()
46 const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
48 email: this.userValidatorsService.USER_EMAIL,
49 role: this.userValidatorsService.USER_ROLE,
50 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
51 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
52 byPassAutoBlacklist: null
55 this.paramsSub = this.route.params.subscribe(routeParams => {
56 const userId = routeParams['id']
57 this.userService.getUser(userId).subscribe(
58 user => this.onUserFetched(user),
60 err => this.error = err.message
66 this.paramsSub.unsubscribe()
70 this.error = undefined
72 const userUpdate: UserUpdate = this.form.value
73 userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
75 // A select in HTML is always mapped as a string, we convert it to number
76 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
77 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
79 this.userService.updateUser(this.userId, userUpdate).subscribe(
81 this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username }))
82 this.router.navigate([ '/admin/users/list' ])
85 err => this.error = err.message
93 getFormButtonTitle () {
94 return this.i18n('Update user')
98 this.userService.askResetPassword(this.userEmail).subscribe(
100 this.notifier.success(
101 this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username })
105 err => this.error = err.message
109 private onUserFetched (userJson: User) {
110 this.userId = userJson.id
111 this.username = userJson.username
112 this.userEmail = userJson.email
114 this.form.patchValue({
115 email: userJson.email,
117 videoQuota: userJson.videoQuota,
118 videoQuotaDaily: userJson.videoQuotaDaily,
119 byPassAutoBlacklist: userJson.adminFlags & UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST