]>
Commit | Line | Data |
---|---|---|
e724fa93 C |
1 | import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' |
2 | import { NotificationsService } from 'angular2-notifications' | |
3 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
4 | import { DropdownAction } from '@app/shared/buttons/action-dropdown.component' | |
5 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
6 | import { UserBanModalComponent } from '@app/shared/moderation/user-ban-modal.component' | |
7 | import { User, UserService } from '@app/shared/users' | |
8 | import { AuthService, ConfirmService } from '@app/core' | |
9 | import { UserRight } from '../../../../../shared/models/users' | |
10 | ||
11 | @Component({ | |
12 | selector: 'my-user-moderation-dropdown', | |
13 | templateUrl: './user-moderation-dropdown.component.html', | |
14 | styleUrls: [ './user-moderation-dropdown.component.scss' ] | |
15 | }) | |
16 | export class UserModerationDropdownComponent implements OnInit { | |
17 | @ViewChild('userBanModal') userBanModal: UserBanModalComponent | |
18 | ||
19 | @Input() user: User | |
20 | @Output() userChanged = new EventEmitter() | |
21 | ||
22 | userActions: DropdownAction<User>[] = [] | |
23 | ||
24 | private openedModal: NgbModalRef | |
25 | ||
26 | constructor ( | |
27 | private authService: AuthService, | |
28 | private notificationsService: NotificationsService, | |
29 | private confirmService: ConfirmService, | |
30 | private userService: UserService, | |
31 | private i18n: I18n | |
32 | ) { } | |
33 | ||
34 | ngOnInit () { | |
35 | this.userActions = [] | |
36 | ||
37 | if (this.authService.isLoggedIn()) { | |
38 | const authUser = this.authService.getUser() | |
39 | ||
40 | if (authUser.hasRight(UserRight.MANAGE_USERS)) { | |
41 | this.userActions = this.userActions.concat([ | |
42 | { | |
43 | label: this.i18n('Edit'), | |
44 | linkBuilder: this.getRouterUserEditLink | |
45 | }, | |
46 | { | |
47 | label: this.i18n('Delete'), | |
48 | handler: user => this.removeUser(user) | |
49 | }, | |
50 | { | |
51 | label: this.i18n('Ban'), | |
52 | handler: user => this.openBanUserModal(user), | |
53 | isDisplayed: user => !user.blocked | |
54 | }, | |
55 | { | |
56 | label: this.i18n('Unban'), | |
57 | handler: user => this.unbanUser(user), | |
58 | isDisplayed: user => user.blocked | |
59 | } | |
60 | ]) | |
61 | } | |
62 | } | |
63 | } | |
64 | ||
65 | hideBanUserModal () { | |
66 | this.openedModal.close() | |
67 | } | |
68 | ||
69 | openBanUserModal (user: User) { | |
70 | if (user.username === 'root') { | |
71 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.')) | |
72 | return | |
73 | } | |
74 | ||
75 | this.userBanModal.openModal(user) | |
76 | } | |
77 | ||
78 | onUserBanned () { | |
79 | this.userChanged.emit() | |
80 | } | |
81 | ||
82 | async unbanUser (user: User) { | |
83 | const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username }) | |
84 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
85 | if (res === false) return | |
86 | ||
87 | this.userService.unbanUser(user) | |
88 | .subscribe( | |
89 | () => { | |
90 | this.notificationsService.success( | |
91 | this.i18n('Success'), | |
92 | this.i18n('User {{username}} unbanned.', { username: user.username }) | |
93 | ) | |
94 | ||
95 | this.userChanged.emit() | |
96 | }, | |
97 | ||
98 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
99 | ) | |
100 | } | |
101 | ||
102 | async removeUser (user: User) { | |
103 | if (user.username === 'root') { | |
104 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.')) | |
105 | return | |
106 | } | |
107 | ||
108 | const message = this.i18n('If you remove this user, you will not be able to create another with the same username!') | |
109 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
110 | if (res === false) return | |
111 | ||
112 | this.userService.removeUser(user).subscribe( | |
113 | () => { | |
114 | this.notificationsService.success( | |
115 | this.i18n('Success'), | |
116 | this.i18n('User {{username}} deleted.', { username: user.username }) | |
117 | ) | |
118 | this.userChanged.emit() | |
119 | }, | |
120 | ||
121 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
122 | ) | |
123 | } | |
124 | ||
125 | getRouterUserEditLink (user: User) { | |
126 | return [ '/admin', 'users', 'update', user.id ] | |
127 | } | |
128 | } |