1 import { Subscription } from 'rxjs'
2 import { Component, OnDestroy, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { ConfigService } from '@app/+admin/config/shared/config.service'
5 import { AuthService, Notifier, ScreenService, ServerService, User, UserService } from '@app/core'
6 import { FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models'
9 import { UserEdit } from './user-edit'
12 selector: 'my-user-update',
13 templateUrl: './user-edit.component.html',
14 styleUrls: [ './user-edit.component.scss' ]
16 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
19 private paramsSub: Subscription
22 protected formValidatorService: FormValidatorService,
23 protected serverService: ServerService,
24 protected configService: ConfigService,
25 protected screenService: ScreenService,
26 protected auth: AuthService,
27 private userValidatorsService: UserValidatorsService,
28 private route: ActivatedRoute,
29 private router: Router,
30 private notifier: Notifier,
31 private userService: UserService,
36 this.buildQuotaOptions()
42 const defaultValues = {
43 role: UserRole.USER.toString(),
49 email: this.userValidatorsService.USER_EMAIL,
50 role: this.userValidatorsService.USER_ROLE,
51 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
52 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
56 this.paramsSub = this.route.params.subscribe(routeParams => {
57 const userId = routeParams['id']
58 this.userService.getUser(userId, true).subscribe(
59 user => this.onUserFetched(user),
61 err => this.error = err.message
67 this.paramsSub.unsubscribe()
71 this.error = undefined
73 const userUpdate: UserUpdate = this.form.value
74 userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
76 // A select in HTML is always mapped as a string, we convert it to number
77 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
78 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
80 this.userService.updateUser(this.user.id, userUpdate).subscribe(
82 this.notifier.success(this.i18n('User {{username}} updated.', { username: this.user.username }))
83 this.router.navigate([ '/admin/users/list' ])
86 err => this.error = err.message
94 isPasswordOptional () {
98 getFormButtonTitle () {
99 return this.i18n('Update user')
103 this.userService.askResetPassword(this.user.email).subscribe(
105 this.notifier.success(
106 this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.user.username })
110 err => this.error = err.message
114 private onUserFetched (userJson: UserType) {
115 this.user = new User(userJson)
117 this.form.patchValue({
118 email: userJson.email,
119 role: userJson.role.toString(),
120 videoQuota: userJson.videoQuota,
121 videoQuotaDaily: userJson.videoQuotaDaily,
122 byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST