]>
Commit | Line | Data |
---|---|---|
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 { UserAdminService } from '@app/shared/shared-users' | |
14 | import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models' | |
15 | import { UserEdit } from './user-edit' | |
16 | ||
17 | @Component({ | |
18 | selector: 'my-user-update', | |
19 | templateUrl: './user-edit.component.html', | |
20 | styleUrls: [ './user-edit.component.scss' ] | |
21 | }) | |
22 | export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy { | |
23 | error: string | |
24 | ||
25 | private paramsSub: Subscription | |
26 | ||
27 | constructor ( | |
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 | |
38 | ) { | |
39 | super() | |
40 | ||
41 | this.buildQuotaOptions() | |
42 | } | |
43 | ||
44 | ngOnInit () { | |
45 | super.ngOnInit() | |
46 | ||
47 | const defaultValues = { | |
48 | role: UserRole.USER.toString(), | |
49 | videoQuota: '-1', | |
50 | videoQuotaDaily: '-1' | |
51 | } | |
52 | ||
53 | this.buildForm({ | |
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, | |
59 | pluginAuth: null | |
60 | }, defaultValues) | |
61 | ||
62 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
63 | const userId = routeParams['id'] | |
64 | this.userService.getUser(userId, true) | |
65 | .subscribe({ | |
66 | next: user => this.onUserFetched(user), | |
67 | ||
68 | error: err => { | |
69 | this.error = err.message | |
70 | } | |
71 | }) | |
72 | }) | |
73 | } | |
74 | ||
75 | ngOnDestroy () { | |
76 | this.paramsSub.unsubscribe() | |
77 | } | |
78 | ||
79 | formValidated () { | |
80 | this.error = undefined | |
81 | ||
82 | const userUpdate: UserUpdate = this.form.value | |
83 | userUpdate.adminFlags = this.buildAdminFlags(this.form.value) | |
84 | ||
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) | |
88 | ||
89 | if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null | |
90 | ||
91 | this.userAdminService.updateUser(this.user.id, userUpdate) | |
92 | .subscribe({ | |
93 | next: () => { | |
94 | this.notifier.success($localize`User ${this.user.username} updated.`) | |
95 | this.router.navigate([ '/admin/users/list' ]) | |
96 | }, | |
97 | ||
98 | error: err => { | |
99 | this.error = err.message | |
100 | } | |
101 | }) | |
102 | } | |
103 | ||
104 | isCreation () { | |
105 | return false | |
106 | } | |
107 | ||
108 | isPasswordOptional () { | |
109 | return false | |
110 | } | |
111 | ||
112 | getFormButtonTitle () { | |
113 | return $localize`Update user` | |
114 | } | |
115 | ||
116 | resetPassword () { | |
117 | this.userService.askResetPassword(this.user.email) | |
118 | .subscribe({ | |
119 | next: () => { | |
120 | this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`) | |
121 | }, | |
122 | ||
123 | error: err => { | |
124 | this.error = err.message | |
125 | } | |
126 | }) | |
127 | } | |
128 | ||
129 | private onUserFetched (userJson: UserType) { | |
130 | this.user = new User(userJson) | |
131 | ||
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 | |
139 | }) | |
140 | } | |
141 | } |