]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnDestroy, OnInit } from '@angular/core' | |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
3 | import { Subscription } from 'rxjs' | |
4 | import { AuthService, Notifier } from '@app/core' | |
5 | import { ServerService } from '../../../core' | |
6 | import { UserEdit } from './user-edit' | |
7 | import { User as UserType, UserUpdate, UserRole } from '../../../../../../shared' | |
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
9 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
10 | import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service' | |
11 | import { ConfigService } from '@app/+admin/config/shared/config.service' | |
12 | import { UserService } from '@app/shared' | |
13 | import { UserAdminFlag } from '@shared/models/users/user-flag.model' | |
14 | import { User } from '@app/shared/users/user.model' | |
15 | import { ScreenService } from '@app/shared/misc/screen.service' | |
16 | ||
17 | @Component({ | |
18 | selector: 'my-user-update', | |
19 | templateUrl: './user-edit.component.html', | |
20 | styleUrls: [ './user-edit.component.scss' ] | |
21 | }) | |
22 | export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy { | |
23 | error: string | |
24 | ||
25 | private paramsSub: Subscription | |
26 | ||
27 | constructor ( | |
28 | protected formValidatorService: FormValidatorService, | |
29 | protected serverService: ServerService, | |
30 | protected configService: ConfigService, | |
31 | protected screenService: ScreenService, | |
32 | protected auth: AuthService, | |
33 | private userValidatorsService: UserValidatorsService, | |
34 | private route: ActivatedRoute, | |
35 | private router: Router, | |
36 | private notifier: Notifier, | |
37 | private userService: UserService, | |
38 | private i18n: I18n | |
39 | ) { | |
40 | super() | |
41 | ||
42 | this.buildQuotaOptions() | |
43 | } | |
44 | ||
45 | ngOnInit () { | |
46 | super.ngOnInit() | |
47 | ||
48 | const defaultValues = { | |
49 | role: UserRole.USER.toString(), | |
50 | videoQuota: '-1', | |
51 | videoQuotaDaily: '-1' | |
52 | } | |
53 | ||
54 | this.buildForm({ | |
55 | email: this.userValidatorsService.USER_EMAIL, | |
56 | role: this.userValidatorsService.USER_ROLE, | |
57 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA, | |
58 | videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY, | |
59 | byPassAutoBlacklist: null | |
60 | }, defaultValues) | |
61 | ||
62 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
63 | const userId = routeParams['id'] | |
64 | this.userService.getUser(userId, true).subscribe( | |
65 | user => this.onUserFetched(user), | |
66 | ||
67 | err => this.error = err.message | |
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 | |
80 | userUpdate.adminFlags = this.buildAdminFlags(this.form.value) | |
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) | |
84 | userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) | |
85 | ||
86 | this.userService.updateUser(this.user.id, userUpdate).subscribe( | |
87 | () => { | |
88 | this.notifier.success(this.i18n('User {{username}} updated.', { username: this.user.username })) | |
89 | this.router.navigate([ '/admin/users/list' ]) | |
90 | }, | |
91 | ||
92 | err => this.error = err.message | |
93 | ) | |
94 | } | |
95 | ||
96 | isCreation () { | |
97 | return false | |
98 | } | |
99 | ||
100 | isPasswordOptional () { | |
101 | return false | |
102 | } | |
103 | ||
104 | getFormButtonTitle () { | |
105 | return this.i18n('Update user') | |
106 | } | |
107 | ||
108 | resetPassword () { | |
109 | this.userService.askResetPassword(this.user.email).subscribe( | |
110 | () => { | |
111 | this.notifier.success( | |
112 | this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.user.username }) | |
113 | ) | |
114 | }, | |
115 | ||
116 | err => this.error = err.message | |
117 | ) | |
118 | } | |
119 | ||
120 | private onUserFetched (userJson: UserType) { | |
121 | this.user = new User(userJson) | |
122 | ||
123 | this.form.patchValue({ | |
124 | email: userJson.email, | |
125 | role: userJson.role.toString(), | |
126 | videoQuota: userJson.videoQuota, | |
127 | videoQuotaDaily: userJson.videoQuotaDaily, | |
128 | byPassAutoBlacklist: userJson.adminFlags & UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST | |
129 | }) | |
130 | } | |
131 | } |