]>
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' | |
12 | import { FormValidatorService } from '@app/shared/shared-forms' | |
d92d070c | 13 | import { 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 ( | |
d18d6478 | 28 | protected formValidatorService: FormValidatorService, |
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 C |
36 | private userService: UserService, |
37 | private userAdminService: UserAdminService | |
9df52d66 | 38 | ) { |
8094a898 | 39 | super() |
3827c3b3 C |
40 | |
41 | this.buildQuotaOptions() | |
8094a898 C |
42 | } |
43 | ||
8094a898 | 44 | ngOnInit () { |
ba430d75 C |
45 | super.ngOnInit() |
46 | ||
76314386 RK |
47 | const defaultValues = { |
48 | role: UserRole.USER.toString(), | |
49 | videoQuota: '-1', | |
50 | videoQuotaDaily: '-1' | |
51 | } | |
52 | ||
d18d6478 | 53 | this.buildForm({ |
7ed1edbb C |
54 | email: USER_EMAIL_VALIDATOR, |
55 | role: USER_ROLE_VALIDATOR, | |
56 | videoQuota: USER_VIDEO_QUOTA_VALIDATOR, | |
57 | videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR, | |
6d989edc C |
58 | byPassAutoBlock: null, |
59 | pluginAuth: null | |
d18d6478 | 60 | }, defaultValues) |
8094a898 C |
61 | |
62 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
63 | const userId = routeParams['id'] | |
1378c0d3 C |
64 | this.userService.getUser(userId, true) |
65 | .subscribe({ | |
66 | next: user => this.onUserFetched(user), | |
8094a898 | 67 | |
9df52d66 C |
68 | error: err => { |
69 | this.error = err.message | |
70 | } | |
1378c0d3 | 71 | }) |
8094a898 C |
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 | |
1eddc9a7 | 83 | userUpdate.adminFlags = this.buildAdminFlags(this.form.value) |
8094a898 C |
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) | |
bee0abff | 87 | userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) |
8094a898 | 88 | |
d11eae7e C |
89 | if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null |
90 | ||
d92d070c | 91 | this.userAdminService.updateUser(this.user.id, userUpdate) |
1378c0d3 C |
92 | .subscribe({ |
93 | next: () => { | |
94 | this.notifier.success($localize`User ${this.user.username} updated.`) | |
95 | this.router.navigate([ '/admin/users/list' ]) | |
96 | }, | |
8094a898 | 97 | |
9df52d66 C |
98 | error: err => { |
99 | this.error = err.message | |
100 | } | |
1378c0d3 | 101 | }) |
8094a898 C |
102 | } |
103 | ||
104 | isCreation () { | |
105 | return false | |
106 | } | |
107 | ||
45f1bd72 JL |
108 | isPasswordOptional () { |
109 | return false | |
110 | } | |
111 | ||
8094a898 | 112 | getFormButtonTitle () { |
66357162 | 113 | return $localize`Update user` |
8094a898 C |
114 | } |
115 | ||
328c78bc | 116 | resetPassword () { |
1378c0d3 C |
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 | ||
9df52d66 C |
123 | error: err => { |
124 | this.error = err.message | |
125 | } | |
1378c0d3 | 126 | }) |
328c78bc RK |
127 | } |
128 | ||
76314386 RK |
129 | private onUserFetched (userJson: UserType) { |
130 | this.user = new User(userJson) | |
8094a898 C |
131 | |
132 | this.form.patchValue({ | |
133 | email: userJson.email, | |
76314386 | 134 | role: userJson.role.toString(), |
bee0abff | 135 | videoQuota: userJson.videoQuota, |
1eddc9a7 | 136 | videoQuotaDaily: userJson.videoQuotaDaily, |
6d989edc | 137 | pluginAuth: userJson.pluginAuth, |
3487330d | 138 | byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST |
8094a898 C |
139 | }) |
140 | } | |
141 | } |