]>
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' | |
79bd2632 | 7 | import { UserService } from '@app/shared/users' |
e724fa93 | 8 | import { AuthService, ConfirmService } from '@app/core' |
79bd2632 | 9 | import { User, UserRight } from '../../../../../shared/models/users' |
e724fa93 C |
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 | |
79bd2632 C |
20 | @Input() buttonSize: 'normal' | 'small' = 'normal' |
21 | ||
e724fa93 | 22 | @Output() userChanged = new EventEmitter() |
79bd2632 | 23 | @Output() userDeleted = new EventEmitter() |
e724fa93 C |
24 | |
25 | userActions: DropdownAction<User>[] = [] | |
26 | ||
27 | private openedModal: NgbModalRef | |
28 | ||
29 | constructor ( | |
30 | private authService: AuthService, | |
31 | private notificationsService: NotificationsService, | |
32 | private confirmService: ConfirmService, | |
33 | private userService: UserService, | |
34 | private i18n: I18n | |
35 | ) { } | |
36 | ||
37 | ngOnInit () { | |
79bd2632 | 38 | this.buildActions() |
e724fa93 C |
39 | } |
40 | ||
41 | hideBanUserModal () { | |
42 | this.openedModal.close() | |
43 | } | |
44 | ||
45 | openBanUserModal (user: User) { | |
46 | if (user.username === 'root') { | |
47 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.')) | |
48 | return | |
49 | } | |
50 | ||
51 | this.userBanModal.openModal(user) | |
52 | } | |
53 | ||
54 | onUserBanned () { | |
55 | this.userChanged.emit() | |
56 | } | |
57 | ||
58 | async unbanUser (user: User) { | |
59 | const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username }) | |
60 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
61 | if (res === false) return | |
62 | ||
63 | this.userService.unbanUser(user) | |
64 | .subscribe( | |
65 | () => { | |
66 | this.notificationsService.success( | |
67 | this.i18n('Success'), | |
68 | this.i18n('User {{username}} unbanned.', { username: user.username }) | |
69 | ) | |
70 | ||
71 | this.userChanged.emit() | |
72 | }, | |
73 | ||
74 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
75 | ) | |
76 | } | |
77 | ||
78 | async removeUser (user: User) { | |
79 | if (user.username === 'root') { | |
80 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.')) | |
81 | return | |
82 | } | |
83 | ||
84 | const message = this.i18n('If you remove this user, you will not be able to create another with the same username!') | |
85 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
86 | if (res === false) return | |
87 | ||
88 | this.userService.removeUser(user).subscribe( | |
89 | () => { | |
90 | this.notificationsService.success( | |
91 | this.i18n('Success'), | |
92 | this.i18n('User {{username}} deleted.', { username: user.username }) | |
93 | ) | |
79bd2632 | 94 | this.userDeleted.emit() |
e724fa93 C |
95 | }, |
96 | ||
97 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
98 | ) | |
99 | } | |
100 | ||
101 | getRouterUserEditLink (user: User) { | |
102 | return [ '/admin', 'users', 'update', user.id ] | |
103 | } | |
79bd2632 C |
104 | |
105 | private buildActions () { | |
106 | this.userActions = [] | |
107 | ||
108 | if (this.authService.isLoggedIn()) { | |
109 | const authUser = this.authService.getUser() | |
110 | ||
111 | if (authUser.hasRight(UserRight.MANAGE_USERS)) { | |
112 | this.userActions = this.userActions.concat([ | |
113 | { | |
114 | label: this.i18n('Edit'), | |
115 | linkBuilder: this.getRouterUserEditLink | |
116 | }, | |
117 | { | |
118 | label: this.i18n('Delete'), | |
119 | handler: user => this.removeUser(user) | |
120 | }, | |
121 | { | |
122 | label: this.i18n('Ban'), | |
123 | handler: user => this.openBanUserModal(user), | |
124 | isDisplayed: user => !user.blocked | |
125 | }, | |
126 | { | |
127 | label: this.i18n('Unban'), | |
128 | handler: user => this.unbanUser(user), | |
129 | isDisplayed: user => user.blocked | |
130 | } | |
131 | ]) | |
132 | } | |
133 | } | |
134 | } | |
e724fa93 | 135 | } |