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 { UserService } from '@app/shared/users'
8 import { AuthService, ConfirmService } from '@app/core'
9 import { User, 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 @Input() buttonSize: 'normal' | 'small' = 'normal'
22 @Output() userChanged = new EventEmitter()
23 @Output() userDeleted = new EventEmitter()
25 userActions: DropdownAction<User>[] = []
27 private openedModal: NgbModalRef
30 private authService: AuthService,
31 private notificationsService: NotificationsService,
32 private confirmService: ConfirmService,
33 private userService: UserService,
42 this.openedModal.close()
45 openBanUserModal (user: User) {
46 if (user.username === 'root') {
47 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
51 this.userBanModal.openModal(user)
55 this.userChanged.emit()
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
63 this.userService.unbanUser(user)
66 this.notificationsService.success(
68 this.i18n('User {{username}} unbanned.', { username: user.username })
71 this.userChanged.emit()
74 err => this.notificationsService.error(this.i18n('Error'), err.message)
78 async removeUser (user: User) {
79 if (user.username === 'root') {
80 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
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
88 this.userService.removeUser(user).subscribe(
90 this.notificationsService.success(
92 this.i18n('User {{username}} deleted.', { username: user.username })
94 this.userDeleted.emit()
97 err => this.notificationsService.error(this.i18n('Error'), err.message)
101 getRouterUserEditLink (user: User) {
102 return [ '/admin', 'users', 'update', user.id ]
105 private buildActions () {
106 this.userActions = []
108 if (this.authService.isLoggedIn()) {
109 const authUser = this.authService.getUser()
111 if (authUser.hasRight(UserRight.MANAGE_USERS)) {
112 this.userActions = this.userActions.concat([
114 label: this.i18n('Edit'),
115 linkBuilder: this.getRouterUserEditLink
118 label: this.i18n('Delete'),
119 handler: user => this.removeUser(user)
122 label: this.i18n('Ban'),
123 handler: user => this.openBanUserModal(user),
124 isDisplayed: user => !user.blocked
127 label: this.i18n('Unban'),
128 handler: user => this.unbanUser(user),
129 isDisplayed: user => user.blocked