]>
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 { 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' | |
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 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 | |
39 | ) { | |
40 | super() | |
41 | ||
42 | this.buildQuotaOptions() | |
43 | } | |
44 | ||
45 | ngOnInit () { | |
46 | super.ngOnInit() | |
47 | ||
48 | const defaultValues = { | |
49 | role: UserRole.USER.toString(), | |
50 | videoQuota: '-1', | |
51 | videoQuotaDaily: '-1' | |
52 | } | |
53 | ||
54 | this.buildForm({ | |
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, | |
60 | pluginAuth: null | |
61 | }, defaultValues) | |
62 | ||
63 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
64 | const userId = routeParams['id'] | |
65 | this.userService.getUser(userId, true) | |
66 | .subscribe({ | |
67 | next: user => this.onUserFetched(user), | |
68 | ||
69 | error: err => { | |
70 | this.error = err.message | |
71 | } | |
72 | }) | |
73 | }) | |
74 | } | |
75 | ||
76 | ngOnDestroy () { | |
77 | this.paramsSub.unsubscribe() | |
78 | } | |
79 | ||
80 | formValidated () { | |
81 | this.error = undefined | |
82 | ||
83 | const userUpdate: UserUpdate = this.form.value | |
84 | userUpdate.adminFlags = this.buildAdminFlags(this.form.value) | |
85 | ||
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) | |
89 | ||
90 | if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null | |
91 | ||
92 | this.userAdminService.updateUser(this.user.id, userUpdate) | |
93 | .subscribe({ | |
94 | next: () => { | |
95 | this.notifier.success($localize`User ${this.user.username} updated.`) | |
96 | this.router.navigate([ '/admin/users/list' ]) | |
97 | }, | |
98 | ||
99 | error: err => { | |
100 | this.error = err.message | |
101 | } | |
102 | }) | |
103 | } | |
104 | ||
105 | isCreation () { | |
106 | return false | |
107 | } | |
108 | ||
109 | isPasswordOptional () { | |
110 | return false | |
111 | } | |
112 | ||
113 | getFormButtonTitle () { | |
114 | return $localize`Update user` | |
115 | } | |
116 | ||
117 | resetPassword () { | |
118 | this.userService.askResetPassword(this.user.email) | |
119 | .subscribe({ | |
120 | next: () => { | |
121 | this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`) | |
122 | }, | |
123 | ||
124 | error: err => this.notifier.error(err.message) | |
125 | }) | |
126 | } | |
127 | ||
128 | disableTwoFactorAuth () { | |
129 | this.twoFactorService.disableTwoFactor({ userId: this.user.id }) | |
130 | .subscribe({ | |
131 | next: () => { | |
132 | this.user.twoFactorEnabled = false | |
133 | ||
134 | this.notifier.success($localize`Two factor authentication of ${this.user.username} disabled.`) | |
135 | }, | |
136 | ||
137 | error: err => this.notifier.error(err.message) | |
138 | }) | |
139 | ||
140 | } | |
141 | ||
142 | private onUserFetched (userJson: UserType) { | |
143 | this.user = new User(userJson) | |
144 | ||
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 | |
152 | }) | |
153 | } | |
154 | } |