]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/moderation/user-moderation-dropdown.component.ts
174e9f024c82119f8ef71a7bc7a7e63a4017b32b
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / moderation / user-moderation-dropdown.component.ts
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'
9
10 @Component({
11 selector: 'my-user-moderation-dropdown',
12 templateUrl: './user-moderation-dropdown.component.html',
13 styleUrls: [ './user-moderation-dropdown.component.scss' ]
14 })
15 export class UserModerationDropdownComponent implements OnInit {
16 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
17
18 @Input() user: User
19 @Input() buttonSize: 'normal' | 'small' = 'normal'
20
21 @Output() userChanged = new EventEmitter()
22 @Output() userDeleted = new EventEmitter()
23
24 userActions: DropdownAction<User>[] = []
25
26 constructor (
27 private authService: AuthService,
28 private notificationsService: NotificationsService,
29 private confirmService: ConfirmService,
30 private userService: UserService,
31 private i18n: I18n
32 ) { }
33
34 ngOnInit () {
35 this.buildActions()
36 }
37
38 openBanUserModal (user: User) {
39 if (user.username === 'root') {
40 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
41 return
42 }
43
44 this.userBanModal.openModal(user)
45 }
46
47 onUserBanned () {
48 this.userChanged.emit()
49 }
50
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
55
56 this.userService.unbanUsers(user)
57 .subscribe(
58 () => {
59 this.notificationsService.success(
60 this.i18n('Success'),
61 this.i18n('User {{username}} unbanned.', { username: user.username })
62 )
63
64 this.userChanged.emit()
65 },
66
67 err => this.notificationsService.error(this.i18n('Error'), err.message)
68 )
69 }
70
71 async removeUser (user: User) {
72 if (user.username === 'root') {
73 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
74 return
75 }
76
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
80
81 this.userService.removeUser(user).subscribe(
82 () => {
83 this.notificationsService.success(
84 this.i18n('Success'),
85 this.i18n('User {{username}} deleted.', { username: user.username })
86 )
87 this.userDeleted.emit()
88 },
89
90 err => this.notificationsService.error(this.i18n('Error'), err.message)
91 )
92 }
93
94 getRouterUserEditLink (user: User) {
95 return [ '/admin', 'users', 'update', user.id ]
96 }
97
98 private buildActions () {
99 this.userActions = []
100
101 if (this.authService.isLoggedIn()) {
102 const authUser = this.authService.getUser()
103
104 if (authUser.hasRight(UserRight.MANAGE_USERS)) {
105 this.userActions = this.userActions.concat([
106 {
107 label: this.i18n('Edit'),
108 linkBuilder: this.getRouterUserEditLink
109 },
110 {
111 label: this.i18n('Delete'),
112 handler: user => this.removeUser(user)
113 },
114 {
115 label: this.i18n('Ban'),
116 handler: user => this.openBanUserModal(user),
117 isDisplayed: user => !user.blocked
118 },
119 {
120 label: this.i18n('Unban'),
121 handler: user => this.unbanUser(user),
122 isDisplayed: user => user.blocked
123 }
124 ])
125 }
126 }
127 }
128 }