]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Subscription } from 'rxjs' |
b426edd4 | 2 | import { Component, OnDestroy, OnInit } from '@angular/core' |
8094a898 | 3 | import { ActivatedRoute, Router } from '@angular/router' |
3827c3b3 | 4 | import { ConfigService } from '@app/+admin/config/shared/config.service' |
67ed6552 | 5 | import { AuthService, Notifier, ScreenService, ServerService, User, UserService } from '@app/core' |
7ed1edbb C |
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' | |
5c5bcea2 | 12 | import { FormReactiveService } from '@app/shared/shared-forms' |
2166c058 | 13 | import { TwoFactorService, UserAdminService } from '@app/shared/shared-users' |
67ed6552 C |
14 | import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models' |
15 | import { UserEdit } from './user-edit' | |
8094a898 C |
16 | |
17 | @Component({ | |
18 | selector: 'my-user-update', | |
6a84aafd C |
19 | templateUrl: './user-edit.component.html', |
20 | styleUrls: [ './user-edit.component.scss' ] | |
8094a898 C |
21 | }) |
22 | export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy { | |
23 | error: string | |
8094a898 | 24 | |
8094a898 C |
25 | private paramsSub: Subscription |
26 | ||
27 | constructor ( | |
5c5bcea2 | 28 | protected formReactiveService: FormReactiveService, |
6a84aafd | 29 | protected serverService: ServerService, |
3827c3b3 | 30 | protected configService: ConfigService, |
76314386 | 31 | protected screenService: ScreenService, |
a95a4cc8 | 32 | protected auth: AuthService, |
8094a898 C |
33 | private route: ActivatedRoute, |
34 | private router: Router, | |
f8b2c1b4 | 35 | private notifier: Notifier, |
d92d070c | 36 | private userService: UserService, |
2166c058 | 37 | private twoFactorService: TwoFactorService, |
d92d070c | 38 | private userAdminService: UserAdminService |
9df52d66 | 39 | ) { |
8094a898 | 40 | super() |
3827c3b3 C |
41 | |
42 | this.buildQuotaOptions() | |
8094a898 C |
43 | } |
44 | ||
8094a898 | 45 | ngOnInit () { |
ba430d75 C |
46 | super.ngOnInit() |
47 | ||
76314386 RK |
48 | const defaultValues = { |
49 | role: UserRole.USER.toString(), | |
50 | videoQuota: '-1', | |
51 | videoQuotaDaily: '-1' | |
52 | } | |
53 | ||
d18d6478 | 54 | this.buildForm({ |
7ed1edbb C |
55 | email: USER_EMAIL_VALIDATOR, |
56 | role: USER_ROLE_VALIDATOR, | |
57 | videoQuota: USER_VIDEO_QUOTA_VALIDATOR, | |
58 | videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR, | |
6d989edc C |
59 | byPassAutoBlock: null, |
60 | pluginAuth: null | |
d18d6478 | 61 | }, defaultValues) |
8094a898 C |
62 | |
63 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
64 | const userId = routeParams['id'] | |
1378c0d3 C |
65 | this.userService.getUser(userId, true) |
66 | .subscribe({ | |
67 | next: user => this.onUserFetched(user), | |
8094a898 | 68 | |
9df52d66 C |
69 | error: err => { |
70 | this.error = err.message | |
71 | } | |
1378c0d3 | 72 | }) |
8094a898 C |
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 | |
1eddc9a7 | 84 | userUpdate.adminFlags = this.buildAdminFlags(this.form.value) |
8094a898 C |
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) | |
bee0abff | 88 | userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) |
8094a898 | 89 | |
d11eae7e C |
90 | if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null |
91 | ||
d92d070c | 92 | this.userAdminService.updateUser(this.user.id, userUpdate) |
1378c0d3 C |
93 | .subscribe({ |
94 | next: () => { | |
95 | this.notifier.success($localize`User ${this.user.username} updated.`) | |
96 | this.router.navigate([ '/admin/users/list' ]) | |
97 | }, | |
8094a898 | 98 | |
9df52d66 C |
99 | error: err => { |
100 | this.error = err.message | |
101 | } | |
1378c0d3 | 102 | }) |
8094a898 C |
103 | } |
104 | ||
105 | isCreation () { | |
106 | return false | |
107 | } | |
108 | ||
45f1bd72 JL |
109 | isPasswordOptional () { |
110 | return false | |
111 | } | |
112 | ||
8094a898 | 113 | getFormButtonTitle () { |
66357162 | 114 | return $localize`Update user` |
8094a898 C |
115 | } |
116 | ||
328c78bc | 117 | resetPassword () { |
1378c0d3 C |
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 | ||
2166c058 C |
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) | |
1378c0d3 | 138 | }) |
2166c058 | 139 | |
328c78bc RK |
140 | } |
141 | ||
76314386 RK |
142 | private onUserFetched (userJson: UserType) { |
143 | this.user = new User(userJson) | |
8094a898 C |
144 | |
145 | this.form.patchValue({ | |
146 | email: userJson.email, | |
9e5cf66b | 147 | role: userJson.role.id.toString(), |
bee0abff | 148 | videoQuota: userJson.videoQuota, |
1eddc9a7 | 149 | videoQuotaDaily: userJson.videoQuotaDaily, |
6d989edc | 150 | pluginAuth: userJson.pluginAuth, |
3487330d | 151 | byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST |
8094a898 C |
152 | }) |
153 | } | |
154 | } |