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'
12 selector: 'my-user-moderation-dropdown',
13 templateUrl: './user-moderation-dropdown.component.html',
14 styleUrls: [ './user-moderation-dropdown.component.scss' ]
16 export class UserModerationDropdownComponent implements OnInit {
17 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
20 @Output() userChanged = new EventEmitter()
22 userActions: DropdownAction<User>[] = []
24 private openedModal: NgbModalRef
27 private authService: AuthService,
28 private notificationsService: NotificationsService,
29 private confirmService: ConfirmService,
30 private userService: UserService,
37 if (this.authService.isLoggedIn()) {
38 const authUser = this.authService.getUser()
40 if (authUser.hasRight(UserRight.MANAGE_USERS)) {
41 this.userActions = this.userActions.concat([
43 label: this.i18n('Edit'),
44 linkBuilder: this.getRouterUserEditLink
47 label: this.i18n('Delete'),
48 handler: user => this.removeUser(user)
51 label: this.i18n('Ban'),
52 handler: user => this.openBanUserModal(user),
53 isDisplayed: user => !user.blocked
56 label: this.i18n('Unban'),
57 handler: user => this.unbanUser(user),
58 isDisplayed: user => user.blocked
66 this.openedModal.close()
69 openBanUserModal (user: User) {
70 if (user.username === 'root') {
71 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
75 this.userBanModal.openModal(user)
79 this.userChanged.emit()
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
87 this.userService.unbanUser(user)
90 this.notificationsService.success(
92 this.i18n('User {{username}} unbanned.', { username: user.username })
95 this.userChanged.emit()
98 err => this.notificationsService.error(this.i18n('Error'), err.message)
102 async removeUser (user: User) {
103 if (user.username === 'root') {
104 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
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
112 this.userService.removeUser(user).subscribe(
114 this.notificationsService.success(
115 this.i18n('Success'),
116 this.i18n('User {{username}} deleted.', { username: user.username })
118 this.userChanged.emit()
121 err => this.notificationsService.error(this.i18n('Error'), err.message)
125 getRouterUserEditLink (user: User) {
126 return [ '/admin', 'users', 'update', user.id ]