]>
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' | |
67ed6552 C |
13 | import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models' |
14 | import { 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 | }) |
21 | export 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'] | |
76314386 | 62 | this.userService.getUser(userId, true).subscribe( |
8094a898 C |
63 | user => this.onUserFetched(user), |
64 | ||
f7354483 | 65 | err => this.error = err.message |
8094a898 C |
66 | ) |
67 | }) | |
68 | } | |
69 | ||
70 | ngOnDestroy () { | |
71 | this.paramsSub.unsubscribe() | |
72 | } | |
73 | ||
74 | formValidated () { | |
75 | this.error = undefined | |
76 | ||
77 | const userUpdate: UserUpdate = this.form.value | |
1eddc9a7 | 78 | userUpdate.adminFlags = this.buildAdminFlags(this.form.value) |
8094a898 C |
79 | |
80 | // A select in HTML is always mapped as a string, we convert it to number | |
81 | userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | |
bee0abff | 82 | userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) |
8094a898 | 83 | |
d11eae7e C |
84 | if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null |
85 | ||
76314386 | 86 | this.userService.updateUser(this.user.id, userUpdate).subscribe( |
8094a898 | 87 | () => { |
66357162 | 88 | this.notifier.success($localize`User ${this.user.username} updated.`) |
8094a898 C |
89 | this.router.navigate([ '/admin/users/list' ]) |
90 | }, | |
91 | ||
f7354483 | 92 | err => this.error = err.message |
8094a898 C |
93 | ) |
94 | } | |
95 | ||
96 | isCreation () { | |
97 | return false | |
98 | } | |
99 | ||
45f1bd72 JL |
100 | isPasswordOptional () { |
101 | return false | |
102 | } | |
103 | ||
8094a898 | 104 | getFormButtonTitle () { |
66357162 | 105 | return $localize`Update user` |
8094a898 C |
106 | } |
107 | ||
328c78bc | 108 | resetPassword () { |
76314386 | 109 | this.userService.askResetPassword(this.user.email).subscribe( |
328c78bc | 110 | () => { |
66357162 | 111 | this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`) |
328c78bc RK |
112 | }, |
113 | ||
114 | err => this.error = err.message | |
115 | ) | |
116 | } | |
117 | ||
76314386 RK |
118 | private onUserFetched (userJson: UserType) { |
119 | this.user = new User(userJson) | |
8094a898 C |
120 | |
121 | this.form.patchValue({ | |
122 | email: userJson.email, | |
76314386 | 123 | role: userJson.role.toString(), |
bee0abff | 124 | videoQuota: userJson.videoQuota, |
1eddc9a7 | 125 | videoQuotaDaily: userJson.videoQuotaDaily, |
6d989edc | 126 | pluginAuth: userJson.pluginAuth, |
3487330d | 127 | byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST |
8094a898 C |
128 | }) |
129 | } | |
130 | } |