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