]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/user-edit/user-update.component.ts
Fix webpack config
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
CommitLineData
67ed6552 1import { Subscription } from 'rxjs'
b426edd4 2import { Component, OnDestroy, OnInit } from '@angular/core'
8094a898 3import { ActivatedRoute, Router } from '@angular/router'
3827c3b3 4import { ConfigService } from '@app/+admin/config/shared/config.service'
67ed6552 5import { AuthService, Notifier, ScreenService, ServerService, User, UserService } from '@app/core'
7ed1edbb
C
6import {
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'
12import { FormValidatorService } from '@app/shared/shared-forms'
67ed6552
C
13import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models'
14import { UserEdit } from './user-edit'
8094a898
C
15
16@Component({
17 selector: 'my-user-update',
6a84aafd
C
18 templateUrl: './user-edit.component.html',
19 styleUrls: [ './user-edit.component.scss' ]
8094a898
C
20})
21export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
22 error: string
8094a898 23
8094a898
C
24 private paramsSub: Subscription
25
26 constructor (
d18d6478 27 protected formValidatorService: FormValidatorService,
6a84aafd 28 protected serverService: ServerService,
3827c3b3 29 protected configService: ConfigService,
76314386 30 protected screenService: ScreenService,
a95a4cc8 31 protected auth: AuthService,
8094a898
C
32 private route: ActivatedRoute,
33 private router: Router,
f8b2c1b4 34 private notifier: Notifier,
66357162
C
35 private userService: UserService
36 ) {
8094a898 37 super()
3827c3b3
C
38
39 this.buildQuotaOptions()
8094a898
C
40 }
41
8094a898 42 ngOnInit () {
ba430d75
C
43 super.ngOnInit()
44
76314386
RK
45 const defaultValues = {
46 role: UserRole.USER.toString(),
47 videoQuota: '-1',
48 videoQuotaDaily: '-1'
49 }
50
d18d6478 51 this.buildForm({
7ed1edbb
C
52 email: USER_EMAIL_VALIDATOR,
53 role: USER_ROLE_VALIDATOR,
54 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
55 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR,
6d989edc
C
56 byPassAutoBlock: null,
57 pluginAuth: null
d18d6478 58 }, defaultValues)
8094a898
C
59
60 this.paramsSub = this.route.params.subscribe(routeParams => {
61 const userId = routeParams['id']
1378c0d3
C
62 this.userService.getUser(userId, true)
63 .subscribe({
64 next: user => this.onUserFetched(user),
8094a898 65
1378c0d3
C
66 error: err => this.error = err.message
67 })
8094a898
C
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
1eddc9a7 79 userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
8094a898
C
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)
bee0abff 83 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
8094a898 84
d11eae7e
C
85 if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null
86
1378c0d3
C
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 },
8094a898 93
1378c0d3
C
94 error: err => this.error = err.message
95 })
8094a898
C
96 }
97
98 isCreation () {
99 return false
100 }
101
45f1bd72
JL
102 isPasswordOptional () {
103 return false
104 }
105
8094a898 106 getFormButtonTitle () {
66357162 107 return $localize`Update user`
8094a898
C
108 }
109
328c78bc 110 resetPassword () {
1378c0d3
C
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 })
328c78bc
RK
119 }
120
76314386
RK
121 private onUserFetched (userJson: UserType) {
122 this.user = new User(userJson)
8094a898
C
123
124 this.form.patchValue({
125 email: userJson.email,
76314386 126 role: userJson.role.toString(),
bee0abff 127 videoQuota: userJson.videoQuota,
1eddc9a7 128 videoQuotaDaily: userJson.videoQuotaDaily,
6d989edc 129 pluginAuth: userJson.pluginAuth,
3487330d 130 byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST
8094a898
C
131 })
132 }
133}