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 { FormReactiveService } from '@app/shared/shared-forms'
13 import { TwoFactorService, 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 formReactiveService: FormReactiveService,
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 twoFactorService: TwoFactorService,
38 private userAdminService: UserAdminService
42 this.buildQuotaOptions()
48 const defaultValues = {
49 role: UserRole.USER.toString(),
55 email: USER_EMAIL_VALIDATOR,
56 role: USER_ROLE_VALIDATOR,
57 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
58 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR,
59 byPassAutoBlock: null,
63 this.paramsSub = this.route.params.subscribe(routeParams => {
64 const userId = routeParams['id']
65 this.userService.getUser(userId, true)
67 next: user => this.onUserFetched(user),
70 this.error = err.message
77 this.paramsSub.unsubscribe()
81 this.error = undefined
83 const userUpdate: UserUpdate = this.form.value
84 userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
86 // A select in HTML is always mapped as a string, we convert it to number
87 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
88 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
90 if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null
92 this.userAdminService.updateUser(this.user.id, userUpdate)
95 this.notifier.success($localize`User ${this.user.username} updated.`)
96 this.router.navigate([ '/admin/users/list' ])
100 this.error = err.message
109 isPasswordOptional () {
113 getFormButtonTitle () {
114 return $localize`Update user`
118 this.userService.askResetPassword(this.user.email)
121 this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`)
124 error: err => this.notifier.error(err.message)
128 disableTwoFactorAuth () {
129 this.twoFactorService.disableTwoFactor({ userId: this.user.id })
132 this.user.twoFactorEnabled = false
134 this.notifier.success($localize`Two factor authentication of ${this.user.username} disabled.`)
137 error: err => this.notifier.error(err.message)
142 private onUserFetched (userJson: UserType) {
143 this.user = new User(userJson)
145 this.form.patchValue({
146 email: userJson.email,
147 role: userJson.role.id.toString(),
148 videoQuota: userJson.videoQuota,
149 videoQuotaDaily: userJson.videoQuotaDaily,
150 pluginAuth: userJson.pluginAuth,
151 byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST