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'
21 @Output() userChanged = new EventEmitter()
22 @Output() userDeleted = new EventEmitter()
24 userActions: DropdownAction<User>[] = []
27 private authService: AuthService,
28 private notificationsService: NotificationsService,
29 private confirmService: ConfirmService,
30 private userService: UserService,
38 openBanUserModal (user: User) {
39 if (user.username === 'root') {
40 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
44 this.userBanModal.openModal(user)
48 this.userChanged.emit()
51 async unbanUser (user: User) {
52 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
53 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
54 if (res === false) return
56 this.userService.unbanUsers(user)
59 this.notificationsService.success(
61 this.i18n('User {{username}} unbanned.', { username: user.username })
64 this.userChanged.emit()
67 err => this.notificationsService.error(this.i18n('Error'), err.message)
71 async removeUser (user: User) {
72 if (user.username === 'root') {
73 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
77 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
78 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
79 if (res === false) return
81 this.userService.removeUser(user).subscribe(
83 this.notificationsService.success(
85 this.i18n('User {{username}} deleted.', { username: user.username })
87 this.userDeleted.emit()
90 err => this.notificationsService.error(this.i18n('Error'), err.message)
94 getRouterUserEditLink (user: User) {
95 return [ '/admin', 'users', 'update', user.id ]
98 private buildActions () {
101 if (this.authService.isLoggedIn()) {
102 const authUser = this.authService.getUser()
104 if (authUser.hasRight(UserRight.MANAGE_USERS)) {
105 this.userActions = this.userActions.concat([
107 label: this.i18n('Edit'),
108 linkBuilder: this.getRouterUserEditLink
111 label: this.i18n('Delete'),
112 handler: user => this.removeUser(user)
115 label: this.i18n('Ban'),
116 handler: user => this.openBanUserModal(user),
117 isDisplayed: user => !user.blocked
120 label: this.i18n('Unban'),
121 handler: user => this.unbanUser(user),
122 isDisplayed: user => user.blocked