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 { UserBanModalComponent } from '@app/shared/moderation/user-ban-modal.component'
6 import { UserService } from '@app/shared/users'
7 import { AuthService, ConfirmService } from '@app/core'
8 import { User, UserRight } from '../../../../../shared/models/users'
11 selector: 'my-user-moderation-dropdown',
12 templateUrl: './user-moderation-dropdown.component.html',
13 styleUrls: [ './user-moderation-dropdown.component.scss' ]
15 export class UserModerationDropdownComponent implements OnInit {
16 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
19 @Input() buttonSize: 'normal' | 'small' = 'normal'
20 @Input() placement = 'left'
22 @Output() userChanged = new EventEmitter()
23 @Output() userDeleted = new EventEmitter()
25 userActions: DropdownAction<User>[] = []
28 private authService: AuthService,
29 private notificationsService: NotificationsService,
30 private confirmService: ConfirmService,
31 private userService: UserService,
39 openBanUserModal (user: User) {
40 if (user.username === 'root') {
41 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
45 this.userBanModal.openModal(user)
49 this.userChanged.emit()
52 async unbanUser (user: User) {
53 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
54 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
55 if (res === false) return
57 this.userService.unbanUsers(user)
60 this.notificationsService.success(
62 this.i18n('User {{username}} unbanned.', { username: user.username })
65 this.userChanged.emit()
68 err => this.notificationsService.error(this.i18n('Error'), err.message)
72 async removeUser (user: User) {
73 if (user.username === 'root') {
74 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
78 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
79 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
80 if (res === false) return
82 this.userService.removeUser(user).subscribe(
84 this.notificationsService.success(
86 this.i18n('User {{username}} deleted.', { username: user.username })
88 this.userDeleted.emit()
91 err => this.notificationsService.error(this.i18n('Error'), err.message)
95 getRouterUserEditLink (user: User) {
96 return [ '/admin', 'users', 'update', user.id ]
99 private buildActions () {
100 this.userActions = []
102 if (this.authService.isLoggedIn()) {
103 const authUser = this.authService.getUser()
105 if (authUser.hasRight(UserRight.MANAGE_USERS)) {
106 this.userActions = this.userActions.concat([
108 label: this.i18n('Edit'),
109 linkBuilder: this.getRouterUserEditLink
112 label: this.i18n('Delete'),
113 handler: user => this.removeUser(user)
116 label: this.i18n('Ban'),
117 handler: user => this.openBanUserModal(user),
118 isDisplayed: user => !user.blocked
121 label: this.i18n('Unban'),
122 handler: user => this.unbanUser(user),
123 isDisplayed: user => user.blocked