1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Subscription } from 'rxjs'
4 import { 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 private userValidatorsService: UserValidatorsService,
33 private route: ActivatedRoute,
34 private router: Router,
35 private notifier: Notifier,
36 private userService: UserService,
41 this.buildQuotaOptions()
45 const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
47 email: this.userValidatorsService.USER_EMAIL,
48 role: this.userValidatorsService.USER_ROLE,
49 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
50 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
51 byPassAutoBlacklist: null
54 this.paramsSub = this.route.params.subscribe(routeParams => {
55 const userId = routeParams['id']
56 this.userService.getUser(userId).subscribe(
57 user => this.onUserFetched(user),
59 err => this.error = err.message
65 this.paramsSub.unsubscribe()
69 this.error = undefined
71 const userUpdate: UserUpdate = this.form.value
72 userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
74 // A select in HTML is always mapped as a string, we convert it to number
75 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
76 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
78 this.userService.updateUser(this.userId, userUpdate).subscribe(
80 this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username }))
81 this.router.navigate([ '/admin/users/list' ])
84 err => this.error = err.message
92 getFormButtonTitle () {
93 return this.i18n('Update user')
97 this.userService.askResetPassword(this.userEmail).subscribe(
99 this.notifier.success(
100 this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username })
104 err => this.error = err.message
108 private onUserFetched (userJson: User) {
109 this.userId = userJson.id
110 this.username = userJson.username
111 this.userEmail = userJson.email
113 this.form.patchValue({
114 email: userJson.email,
116 videoQuota: userJson.videoQuota,
117 videoQuotaDaily: userJson.videoQuotaDaily,
118 byPassAutoBlacklist: userJson.adminFlags & UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST