]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-edit/user-update.component.ts
Fix client lint
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
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 {
7 USER_EMAIL_VALIDATOR,
8 USER_ROLE_VALIDATOR,
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'
15
16 @Component({
17 selector: 'my-user-update',
18 templateUrl: './user-edit.component.html',
19 styleUrls: [ './user-edit.component.scss' ]
20 })
21 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
22 error: string
23
24 private paramsSub: Subscription
25
26 constructor (
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
36 ) {
37 super()
38
39 this.buildQuotaOptions()
40 }
41
42 ngOnInit () {
43 super.ngOnInit()
44
45 const defaultValues = {
46 role: UserRole.USER.toString(),
47 videoQuota: '-1',
48 videoQuotaDaily: '-1'
49 }
50
51 this.buildForm({
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,
57 pluginAuth: null
58 }, defaultValues)
59
60 this.paramsSub = this.route.params.subscribe(routeParams => {
61 const userId = routeParams['id']
62 this.userService.getUser(userId, true)
63 .subscribe({
64 next: user => this.onUserFetched(user),
65
66 error: err => this.error = err.message
67 })
68 })
69 }
70
71 ngOnDestroy () {
72 this.paramsSub.unsubscribe()
73 }
74
75 formValidated () {
76 this.error = undefined
77
78 const userUpdate: UserUpdate = this.form.value
79 userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
80
81 // A select in HTML is always mapped as a string, we convert it to number
82 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
83 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
84
85 if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null
86
87 this.userService.updateUser(this.user.id, userUpdate)
88 .subscribe({
89 next: () => {
90 this.notifier.success($localize`User ${this.user.username} updated.`)
91 this.router.navigate([ '/admin/users/list' ])
92 },
93
94 error: err => this.error = err.message
95 })
96 }
97
98 isCreation () {
99 return false
100 }
101
102 isPasswordOptional () {
103 return false
104 }
105
106 getFormButtonTitle () {
107 return $localize`Update user`
108 }
109
110 resetPassword () {
111 this.userService.askResetPassword(this.user.email)
112 .subscribe({
113 next: () => {
114 this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`)
115 },
116
117 error: err => this.error = err.message
118 })
119 }
120
121 private onUserFetched (userJson: UserType) {
122 this.user = new User(userJson)
123
124 this.form.patchValue({
125 email: userJson.email,
126 role: userJson.role.toString(),
127 videoQuota: userJson.videoQuota,
128 videoQuotaDaily: userJson.videoQuotaDaily,
129 pluginAuth: userJson.pluginAuth,
130 byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST
131 })
132 }
133 }