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 as UserType, UserUpdate, UserRole } 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'
14 import { User } from '@app/shared/users/user.model'
15 import { ScreenService } from '@app/shared/misc/screen.service'
18 selector: 'my-user-update',
19 templateUrl: './user-edit.component.html',
20 styleUrls: [ './user-edit.component.scss' ]
22 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
25 private paramsSub: Subscription
28 protected formValidatorService: FormValidatorService,
29 protected serverService: ServerService,
30 protected configService: ConfigService,
31 protected screenService: ScreenService,
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()
48 const defaultValues = {
49 role: UserRole.USER.toString(),
55 email: this.userValidatorsService.USER_EMAIL,
56 role: this.userValidatorsService.USER_ROLE,
57 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
58 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
59 byPassAutoBlacklist: null
62 this.paramsSub = this.route.params.subscribe(routeParams => {
63 const userId = routeParams['id']
64 this.userService.getUser(userId, true).subscribe(
65 user => this.onUserFetched(user),
67 err => this.error = err.message
73 this.paramsSub.unsubscribe()
77 this.error = undefined
79 const userUpdate: UserUpdate = this.form.value
80 userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
82 // A select in HTML is always mapped as a string, we convert it to number
83 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
84 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
86 this.userService.updateUser(this.user.id, userUpdate).subscribe(
88 this.notifier.success(this.i18n('User {{username}} updated.', { username: this.user.username }))
89 this.router.navigate([ '/admin/users/list' ])
92 err => this.error = err.message
100 isPasswordOptional () {
104 getFormButtonTitle () {
105 return this.i18n('Update user')
109 this.userService.askResetPassword(this.user.email).subscribe(
111 this.notifier.success(
112 this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.user.username })
116 err => this.error = err.message
120 private onUserFetched (userJson: UserType) {
121 this.user = new User(userJson)
123 this.form.patchValue({
124 email: userJson.email,
125 role: userJson.role.toString(),
126 videoQuota: userJson.videoQuota,
127 videoQuotaDaily: userJson.videoQuotaDaily,
128 byPassAutoBlacklist: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLOCK