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 { UserAdminService } from '@app/shared/shared-users'
14 import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models'
15 import { UserEdit } from './user-edit'
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 route: ActivatedRoute,
34 private router: Router,
35 private notifier: Notifier,
36 private userService: UserService,
37 private userAdminService: UserAdminService
41 this.buildQuotaOptions()
47 const defaultValues = {
48 role: UserRole.USER.toString(),
54 email: USER_EMAIL_VALIDATOR,
55 role: USER_ROLE_VALIDATOR,
56 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
57 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR,
58 byPassAutoBlock: null,
62 this.paramsSub = this.route.params.subscribe(routeParams => {
63 const userId = routeParams['id']
64 this.userService.getUser(userId, true)
66 next: user => this.onUserFetched(user),
69 this.error = err.message
76 this.paramsSub.unsubscribe()
80 this.error = undefined
82 const userUpdate: UserUpdate = this.form.value
83 userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
85 // A select in HTML is always mapped as a string, we convert it to number
86 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
87 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
89 if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null
91 this.userAdminService.updateUser(this.user.id, userUpdate)
94 this.notifier.success($localize`User ${this.user.username} updated.`)
95 this.router.navigate([ '/admin/users/list' ])
99 this.error = err.message
108 isPasswordOptional () {
112 getFormButtonTitle () {
113 return $localize`Update user`
117 this.userService.askResetPassword(this.user.email)
120 this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`)
124 this.error = err.message
129 private onUserFetched (userJson: UserType) {
130 this.user = new User(userJson)
132 this.form.patchValue({
133 email: userJson.email,
134 role: userJson.role.toString(),
135 videoQuota: userJson.videoQuota,
136 videoQuotaDaily: userJson.videoQuotaDaily,
137 pluginAuth: userJson.pluginAuth,
138 byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST