]>
Commit | Line | Data |
---|---|---|
b426edd4 | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
8094a898 | 2 | import { ActivatedRoute, Router } from '@angular/router' |
db400f44 | 3 | import { Subscription } from 'rxjs' |
a95a4cc8 | 4 | import { AuthService, Notifier } from '@app/core' |
6a84aafd | 5 | import { ServerService } from '../../../core' |
8094a898 | 6 | import { UserEdit } from './user-edit' |
76314386 | 7 | import { User as UserType, UserUpdate, UserRole } from '../../../../../../shared' |
b1d40cff | 8 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 9 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 10 | import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service' |
3827c3b3 | 11 | import { ConfigService } from '@app/+admin/config/shared/config.service' |
e724fa93 | 12 | import { UserService } from '@app/shared' |
1eddc9a7 | 13 | import { UserAdminFlag } from '@shared/models/users/user-flag.model' |
76314386 RK |
14 | import { User } from '@app/shared/users/user.model' |
15 | import { ScreenService } from '@app/shared/misc/screen.service' | |
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, |
e309822b | 33 | private userValidatorsService: UserValidatorsService, |
8094a898 C |
34 | private route: ActivatedRoute, |
35 | private router: Router, | |
f8b2c1b4 | 36 | private notifier: Notifier, |
b1d40cff C |
37 | private userService: UserService, |
38 | private i18n: I18n | |
8094a898 C |
39 | ) { |
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({ |
e309822b C |
55 | email: this.userValidatorsService.USER_EMAIL, |
56 | role: this.userValidatorsService.USER_ROLE, | |
bee0abff | 57 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA, |
1eddc9a7 C |
58 | videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY, |
59 | byPassAutoBlacklist: null | |
d18d6478 | 60 | }, defaultValues) |
8094a898 C |
61 | |
62 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
63 | const userId = routeParams['id'] | |
76314386 | 64 | this.userService.getUser(userId, true).subscribe( |
8094a898 C |
65 | user => this.onUserFetched(user), |
66 | ||
f7354483 | 67 | err => this.error = err.message |
8094a898 C |
68 | ) |
69 | }) | |
70 | } | |
71 | ||
72 | ngOnDestroy () { | |
73 | this.paramsSub.unsubscribe() | |
74 | } | |
75 | ||
76 | formValidated () { | |
77 | this.error = undefined | |
78 | ||
79 | const userUpdate: UserUpdate = this.form.value | |
1eddc9a7 | 80 | userUpdate.adminFlags = this.buildAdminFlags(this.form.value) |
8094a898 C |
81 | |
82 | // A select in HTML is always mapped as a string, we convert it to number | |
83 | userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | |
bee0abff | 84 | userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) |
8094a898 | 85 | |
76314386 | 86 | this.userService.updateUser(this.user.id, userUpdate).subscribe( |
8094a898 | 87 | () => { |
86521a67 | 88 | this.notifier.success(this.i18n('User {{username}} updated.', { username: this.user.username })) |
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 () { |
b1d40cff | 105 | return this.i18n('Update user') |
8094a898 C |
106 | } |
107 | ||
328c78bc | 108 | resetPassword () { |
76314386 | 109 | this.userService.askResetPassword(this.user.email).subscribe( |
328c78bc | 110 | () => { |
b426edd4 | 111 | this.notifier.success( |
76314386 | 112 | this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.user.username }) |
328c78bc RK |
113 | ) |
114 | }, | |
115 | ||
116 | err => this.error = err.message | |
117 | ) | |
118 | } | |
119 | ||
76314386 RK |
120 | private onUserFetched (userJson: UserType) { |
121 | this.user = new User(userJson) | |
8094a898 C |
122 | |
123 | this.form.patchValue({ | |
124 | email: userJson.email, | |
76314386 | 125 | role: userJson.role.toString(), |
bee0abff | 126 | videoQuota: userJson.videoQuota, |
1eddc9a7 C |
127 | videoQuotaDaily: userJson.videoQuotaDaily, |
128 | byPassAutoBlacklist: userJson.adminFlags & UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST | |
8094a898 C |
129 | }) |
130 | } | |
131 | } |