aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/moderation
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-10-05 15:24:29 +0200
committerChocobozzz <me@florianbigard.com>2018-10-05 17:02:09 +0200
commite724fa93c71d76d709e819a05e5e2904a3c4205b (patch)
treec9bd410253f4ceac8e330541580445ca313583ac /client/src/app/shared/moderation
parent21c54ac5f684f8b72bcde45cd8327ee21f574f22 (diff)
downloadPeerTube-e724fa93c71d76d709e819a05e5e2904a3c4205b.tar.gz
PeerTube-e724fa93c71d76d709e819a05e5e2904a3c4205b.tar.zst
PeerTube-e724fa93c71d76d709e819a05e5e2904a3c4205b.zip
Move user moderation tool in a separate component
Diffstat (limited to 'client/src/app/shared/moderation')
-rw-r--r--client/src/app/shared/moderation/index.ts2
-rw-r--r--client/src/app/shared/moderation/user-ban-modal.component.html32
-rw-r--r--client/src/app/shared/moderation/user-ban-modal.component.scss6
-rw-r--r--client/src/app/shared/moderation/user-ban-modal.component.ts68
-rw-r--r--client/src/app/shared/moderation/user-moderation-dropdown.component.html3
-rw-r--r--client/src/app/shared/moderation/user-moderation-dropdown.component.scss0
-rw-r--r--client/src/app/shared/moderation/user-moderation-dropdown.component.ts128
7 files changed, 239 insertions, 0 deletions
diff --git a/client/src/app/shared/moderation/index.ts b/client/src/app/shared/moderation/index.ts
new file mode 100644
index 000000000..2245294c5
--- /dev/null
+++ b/client/src/app/shared/moderation/index.ts
@@ -0,0 +1,2 @@
1export * from './user-ban-modal.component'
2export * from './user-moderation-dropdown.component' \ No newline at end of file
diff --git a/client/src/app/shared/moderation/user-ban-modal.component.html b/client/src/app/shared/moderation/user-ban-modal.component.html
new file mode 100644
index 000000000..b2958caa4
--- /dev/null
+++ b/client/src/app/shared/moderation/user-ban-modal.component.html
@@ -0,0 +1,32 @@
1<ng-template #modal>
2 <div class="modal-header">
3 <h4 i18n class="modal-title">Ban {{ userToBan.username }}</h4>
4 <span class="close" aria-hidden="true" (click)="hideBanUserModal()"></span>
5 </div>
6
7 <div class="modal-body">
8 <form novalidate [formGroup]="form" (ngSubmit)="banUser()">
9 <div class="form-group">
10 <textarea i18n-placeholder placeholder="Reason..." formControlName="reason" [ngClass]="{ 'input-error': formErrors['reason'] }">
11 </textarea>
12 <div *ngIf="formErrors.reason" class="form-error">
13 {{ formErrors.reason }}
14 </div>
15 </div>
16
17 <div i18n>
18 A banned user will no longer be able to login.
19 </div>
20
21 <div class="form-group inputs">
22 <span i18n class="action-button action-button-cancel" (click)="hideBanUserModal()">Cancel</span>
23
24 <input
25 type="submit" i18n-value value="Ban this user" class="action-button-submit"
26 [disabled]="!form.valid"
27 >
28 </div>
29 </form>
30 </div>
31
32</ng-template> \ No newline at end of file
diff --git a/client/src/app/shared/moderation/user-ban-modal.component.scss b/client/src/app/shared/moderation/user-ban-modal.component.scss
new file mode 100644
index 000000000..84562f15c
--- /dev/null
+++ b/client/src/app/shared/moderation/user-ban-modal.component.scss
@@ -0,0 +1,6 @@
1@import 'variables';
2@import 'mixins';
3
4textarea {
5 @include peertube-textarea(100%, 60px);
6}
diff --git a/client/src/app/shared/moderation/user-ban-modal.component.ts b/client/src/app/shared/moderation/user-ban-modal.component.ts
new file mode 100644
index 000000000..d49783cd2
--- /dev/null
+++ b/client/src/app/shared/moderation/user-ban-modal.component.ts
@@ -0,0 +1,68 @@
1import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2import { NotificationsService } from 'angular2-notifications'
3import { I18n } from '@ngx-translate/i18n-polyfill'
4import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
5import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
6import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
7import { FormReactive, UserValidatorsService } from '@app/shared/forms'
8import { User, UserService } from '@app/shared/users'
9
10@Component({
11 selector: 'my-user-ban-modal',
12 templateUrl: './user-ban-modal.component.html',
13 styleUrls: [ './user-ban-modal.component.scss' ]
14})
15export class UserBanModalComponent extends FormReactive implements OnInit {
16 @ViewChild('modal') modal: NgbModal
17 @Output() userBanned = new EventEmitter<User>()
18
19 private userToBan: User
20 private openedModal: NgbModalRef
21
22 constructor (
23 protected formValidatorService: FormValidatorService,
24 private modalService: NgbModal,
25 private notificationsService: NotificationsService,
26 private userService: UserService,
27 private userValidatorsService: UserValidatorsService,
28 private i18n: I18n
29 ) {
30 super()
31 }
32
33 ngOnInit () {
34 this.buildForm({
35 reason: this.userValidatorsService.USER_BAN_REASON
36 })
37 }
38
39 openModal (user: User) {
40 this.userToBan = user
41 this.openedModal = this.modalService.open(this.modal)
42 }
43
44 hideBanUserModal () {
45 this.userToBan = undefined
46 this.openedModal.close()
47 }
48
49 async banUser () {
50 const reason = this.form.value['reason'] || undefined
51
52 this.userService.banUser(this.userToBan, reason)
53 .subscribe(
54 () => {
55 this.notificationsService.success(
56 this.i18n('Success'),
57 this.i18n('User {{username}} banned.', { username: this.userToBan.username })
58 )
59
60 this.userBanned.emit(this.userToBan)
61 this.hideBanUserModal()
62 },
63
64 err => this.notificationsService.error(this.i18n('Error'), err.message)
65 )
66 }
67
68}
diff --git a/client/src/app/shared/moderation/user-moderation-dropdown.component.html b/client/src/app/shared/moderation/user-moderation-dropdown.component.html
new file mode 100644
index 000000000..ed8a4dc66
--- /dev/null
+++ b/client/src/app/shared/moderation/user-moderation-dropdown.component.html
@@ -0,0 +1,3 @@
1<my-user-ban-modal #userBanModal (userBanned)="onUserBanned()"></my-user-ban-modal>
2
3<my-action-dropdown i18n-label label="Actions" [actions]="userActions" [entry]="user"></my-action-dropdown> \ No newline at end of file
diff --git a/client/src/app/shared/moderation/user-moderation-dropdown.component.scss b/client/src/app/shared/moderation/user-moderation-dropdown.component.scss
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/client/src/app/shared/moderation/user-moderation-dropdown.component.scss
diff --git a/client/src/app/shared/moderation/user-moderation-dropdown.component.ts b/client/src/app/shared/moderation/user-moderation-dropdown.component.ts
new file mode 100644
index 000000000..d92423476
--- /dev/null
+++ b/client/src/app/shared/moderation/user-moderation-dropdown.component.ts
@@ -0,0 +1,128 @@
1import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2import { NotificationsService } from 'angular2-notifications'
3import { I18n } from '@ngx-translate/i18n-polyfill'
4import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
5import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
6import { UserBanModalComponent } from '@app/shared/moderation/user-ban-modal.component'
7import { User, UserService } from '@app/shared/users'
8import { AuthService, ConfirmService } from '@app/core'
9import { UserRight } from '../../../../../shared/models/users'
10
11@Component({
12 selector: 'my-user-moderation-dropdown',
13 templateUrl: './user-moderation-dropdown.component.html',
14 styleUrls: [ './user-moderation-dropdown.component.scss' ]
15})
16export class UserModerationDropdownComponent implements OnInit {
17 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
18
19 @Input() user: User
20 @Output() userChanged = new EventEmitter()
21
22 userActions: DropdownAction<User>[] = []
23
24 private openedModal: NgbModalRef
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.userActions = []
36
37 if (this.authService.isLoggedIn()) {
38 const authUser = this.authService.getUser()
39
40 if (authUser.hasRight(UserRight.MANAGE_USERS)) {
41 this.userActions = this.userActions.concat([
42 {
43 label: this.i18n('Edit'),
44 linkBuilder: this.getRouterUserEditLink
45 },
46 {
47 label: this.i18n('Delete'),
48 handler: user => this.removeUser(user)
49 },
50 {
51 label: this.i18n('Ban'),
52 handler: user => this.openBanUserModal(user),
53 isDisplayed: user => !user.blocked
54 },
55 {
56 label: this.i18n('Unban'),
57 handler: user => this.unbanUser(user),
58 isDisplayed: user => user.blocked
59 }
60 ])
61 }
62 }
63 }
64
65 hideBanUserModal () {
66 this.openedModal.close()
67 }
68
69 openBanUserModal (user: User) {
70 if (user.username === 'root') {
71 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
72 return
73 }
74
75 this.userBanModal.openModal(user)
76 }
77
78 onUserBanned () {
79 this.userChanged.emit()
80 }
81
82 async unbanUser (user: User) {
83 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
84 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
85 if (res === false) return
86
87 this.userService.unbanUser(user)
88 .subscribe(
89 () => {
90 this.notificationsService.success(
91 this.i18n('Success'),
92 this.i18n('User {{username}} unbanned.', { username: user.username })
93 )
94
95 this.userChanged.emit()
96 },
97
98 err => this.notificationsService.error(this.i18n('Error'), err.message)
99 )
100 }
101
102 async removeUser (user: User) {
103 if (user.username === 'root') {
104 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
105 return
106 }
107
108 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
109 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
110 if (res === false) return
111
112 this.userService.removeUser(user).subscribe(
113 () => {
114 this.notificationsService.success(
115 this.i18n('Success'),
116 this.i18n('User {{username}} deleted.', { username: user.username })
117 )
118 this.userChanged.emit()
119 },
120
121 err => this.notificationsService.error(this.i18n('Error'), err.message)
122 )
123 }
124
125 getRouterUserEditLink (user: User) {
126 return [ '/admin', 'users', 'update', user.id ]
127 }
128}