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'
9 USER_VIDEO_QUOTA_DAILY_VALIDATOR,
10 USER_VIDEO_QUOTA_VALIDATOR
11 } from '@app/shared/form-validators/user-validators'
12 import { FormValidatorService } from '@app/shared/shared-forms'
13 import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models'
14 import { UserEdit } from './user-edit'
17 selector: 'my-user-update',
18 templateUrl: './user-edit.component.html',
19 styleUrls: [ './user-edit.component.scss' ]
21 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
24 private paramsSub: Subscription
27 protected formValidatorService: FormValidatorService,
28 protected serverService: ServerService,
29 protected configService: ConfigService,
30 protected screenService: ScreenService,
31 protected auth: AuthService,
32 private route: ActivatedRoute,
33 private router: Router,
34 private notifier: Notifier,
35 private userService: UserService
39 this.buildQuotaOptions()
45 const defaultValues = {
46 role: UserRole.USER.toString(),
52 email: USER_EMAIL_VALIDATOR,
53 role: USER_ROLE_VALIDATOR,
54 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
55 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR,
56 byPassAutoBlock: null,
60 this.paramsSub = this.route.params.subscribe(routeParams => {
61 const userId = routeParams['id']
62 this.userService.getUser(userId, true).subscribe(
63 user => this.onUserFetched(user),
65 err => this.error = err.message
71 this.paramsSub.unsubscribe()
75 this.error = undefined
77 const userUpdate: UserUpdate = this.form.value
78 userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
80 // A select in HTML is always mapped as a string, we convert it to number
81 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
82 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
84 if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null
86 this.userService.updateUser(this.user.id, userUpdate).subscribe(
88 this.notifier.success($localize`User ${this.user.username} updated.`)
89 this.router.navigate([ '/admin/users/list' ])
92 err => this.error = err.message
100 isPasswordOptional () {
104 getFormButtonTitle () {
105 return $localize`Update user`
109 this.userService.askResetPassword(this.user.email).subscribe(
111 this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`)
114 err => this.error = err.message
118 private onUserFetched (userJson: UserType) {
119 this.user = new User(userJson)
121 this.form.patchValue({
122 email: userJson.email,
123 role: userJson.role.toString(),
124 videoQuota: userJson.videoQuota,
125 videoQuotaDaily: userJson.videoQuotaDaily,
126 pluginAuth: userJson.pluginAuth,
127 byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST