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