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 { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models'
8 import { UserEdit } from './user-edit'
11 selector: 'my-user-update',
12 templateUrl: './user-edit.component.html',
13 styleUrls: [ './user-edit.component.scss' ]
15 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
18 private paramsSub: Subscription
21 protected formValidatorService: FormValidatorService,
22 protected serverService: ServerService,
23 protected configService: ConfigService,
24 protected screenService: ScreenService,
25 protected auth: AuthService,
26 private userValidatorsService: UserValidatorsService,
27 private route: ActivatedRoute,
28 private router: Router,
29 private notifier: Notifier,
30 private userService: UserService
34 this.buildQuotaOptions()
40 const defaultValues = {
41 role: UserRole.USER.toString(),
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,
54 this.paramsSub = this.route.params.subscribe(routeParams => {
55 const userId = routeParams['id']
56 this.userService.getUser(userId, true).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.user.id, userUpdate).subscribe(
80 this.notifier.success($localize`User ${this.user.username} updated.`)
81 this.router.navigate([ '/admin/users/list' ])
84 err => this.error = err.message
92 isPasswordOptional () {
96 getFormButtonTitle () {
97 return $localize`Update user`
101 this.userService.askResetPassword(this.user.email).subscribe(
103 this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`)
106 err => this.error = err.message
110 private onUserFetched (userJson: UserType) {
111 this.user = new User(userJson)
113 this.form.patchValue({
114 email: userJson.email,
115 role: userJson.role.toString(),
116 videoQuota: userJson.videoQuota,
117 videoQuotaDaily: userJson.videoQuotaDaily,
118 byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST