]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/moderation/user-moderation-dropdown.component.ts
enable email verification by admin (#1348)
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / moderation / user-moderation-dropdown.component.ts
1 import { Component, EventEmitter, Input, OnChanges, 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, ServerService } from '@app/core'
8 import { User, UserRight } from '../../../../../shared/models/users'
9 import { Account } from '@app/shared/account/account.model'
10 import { BlocklistService } from '@app/shared/blocklist'
11
12 @Component({
13 selector: 'my-user-moderation-dropdown',
14 templateUrl: './user-moderation-dropdown.component.html',
15 styleUrls: [ './user-moderation-dropdown.component.scss' ]
16 })
17 export class UserModerationDropdownComponent implements OnChanges {
18 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
19
20 @Input() user: User
21 @Input() account: Account
22
23 @Input() buttonSize: 'normal' | 'small' = 'normal'
24 @Input() placement = 'left'
25
26 @Output() userChanged = new EventEmitter()
27 @Output() userDeleted = new EventEmitter()
28
29 userActions: DropdownAction<{ user: User, account: Account }>[] = []
30
31 constructor (
32 private authService: AuthService,
33 private notificationsService: NotificationsService,
34 private confirmService: ConfirmService,
35 private serverService: ServerService,
36 private userService: UserService,
37 private blocklistService: BlocklistService,
38 private i18n: I18n
39 ) { }
40
41 get requiresEmailVerification () {
42 return this.serverService.getConfig().signup.requiresEmailVerification
43 }
44
45 ngOnChanges () {
46 this.buildActions()
47 }
48
49 openBanUserModal (user: User) {
50 if (user.username === 'root') {
51 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
52 return
53 }
54
55 this.userBanModal.openModal(user)
56 }
57
58 onUserBanned () {
59 this.userChanged.emit()
60 }
61
62 async unbanUser (user: User) {
63 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
64 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
65 if (res === false) return
66
67 this.userService.unbanUsers(user)
68 .subscribe(
69 () => {
70 this.notificationsService.success(
71 this.i18n('Success'),
72 this.i18n('User {{username}} unbanned.', { username: user.username })
73 )
74
75 this.userChanged.emit()
76 },
77
78 err => this.notificationsService.error(this.i18n('Error'), err.message)
79 )
80 }
81
82 async removeUser (user: User) {
83 if (user.username === 'root') {
84 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
85 return
86 }
87
88 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
89 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
90 if (res === false) return
91
92 this.userService.removeUser(user).subscribe(
93 () => {
94 this.notificationsService.success(
95 this.i18n('Success'),
96 this.i18n('User {{username}} deleted.', { username: user.username })
97 )
98 this.userDeleted.emit()
99 },
100
101 err => this.notificationsService.error(this.i18n('Error'), err.message)
102 )
103 }
104
105 setEmailAsVerified (user: User) {
106 this.userService.updateUser(user.id, { emailVerified: true }).subscribe(
107 () => {
108 this.notificationsService.success(
109 this.i18n('Success'),
110 this.i18n('User {{username}} email set as verified', { username: user.username })
111 )
112 },
113
114 err => this.notificationsService.error(this.i18n('Error'), err.message)
115 )
116 }
117
118 blockAccountByUser (account: Account) {
119 this.blocklistService.blockAccountByUser(account)
120 .subscribe(
121 () => {
122 this.notificationsService.success(
123 this.i18n('Success'),
124 this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost })
125 )
126
127 this.account.mutedByUser = true
128 this.userChanged.emit()
129 },
130
131 err => this.notificationsService.error(this.i18n('Error'), err.message)
132 )
133 }
134
135 unblockAccountByUser (account: Account) {
136 this.blocklistService.unblockAccountByUser(account)
137 .subscribe(
138 () => {
139 this.notificationsService.success(
140 this.i18n('Success'),
141 this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost })
142 )
143
144 this.account.mutedByUser = false
145 this.userChanged.emit()
146 },
147
148 err => this.notificationsService.error(this.i18n('Error'), err.message)
149 )
150 }
151
152 blockServerByUser (host: string) {
153 this.blocklistService.blockServerByUser(host)
154 .subscribe(
155 () => {
156 this.notificationsService.success(
157 this.i18n('Success'),
158 this.i18n('Instance {{host}} muted.', { host })
159 )
160
161 this.account.mutedServerByUser = true
162 this.userChanged.emit()
163 },
164
165 err => this.notificationsService.error(this.i18n('Error'), err.message)
166 )
167 }
168
169 unblockServerByUser (host: string) {
170 this.blocklistService.unblockServerByUser(host)
171 .subscribe(
172 () => {
173 this.notificationsService.success(
174 this.i18n('Success'),
175 this.i18n('Instance {{host}} unmuted.', { host })
176 )
177
178 this.account.mutedServerByUser = false
179 this.userChanged.emit()
180 },
181
182 err => this.notificationsService.error(this.i18n('Error'), err.message)
183 )
184 }
185
186 blockAccountByInstance (account: Account) {
187 this.blocklistService.blockAccountByInstance(account)
188 .subscribe(
189 () => {
190 this.notificationsService.success(
191 this.i18n('Success'),
192 this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost })
193 )
194
195 this.account.mutedByInstance = true
196 this.userChanged.emit()
197 },
198
199 err => this.notificationsService.error(this.i18n('Error'), err.message)
200 )
201 }
202
203 unblockAccountByInstance (account: Account) {
204 this.blocklistService.unblockAccountByInstance(account)
205 .subscribe(
206 () => {
207 this.notificationsService.success(
208 this.i18n('Success'),
209 this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost })
210 )
211
212 this.account.mutedByInstance = false
213 this.userChanged.emit()
214 },
215
216 err => this.notificationsService.error(this.i18n('Error'), err.message)
217 )
218 }
219
220 blockServerByInstance (host: string) {
221 this.blocklistService.blockServerByInstance(host)
222 .subscribe(
223 () => {
224 this.notificationsService.success(
225 this.i18n('Success'),
226 this.i18n('Instance {{host}} muted by the instance.', { host })
227 )
228
229 this.account.mutedServerByInstance = true
230 this.userChanged.emit()
231 },
232
233 err => this.notificationsService.error(this.i18n('Error'), err.message)
234 )
235 }
236
237 unblockServerByInstance (host: string) {
238 this.blocklistService.unblockServerByInstance(host)
239 .subscribe(
240 () => {
241 this.notificationsService.success(
242 this.i18n('Success'),
243 this.i18n('Instance {{host}} unmuted by the instance.', { host })
244 )
245
246 this.account.mutedServerByInstance = false
247 this.userChanged.emit()
248 },
249
250 err => this.notificationsService.error(this.i18n('Error'), err.message)
251 )
252 }
253
254 getRouterUserEditLink (user: User) {
255 return [ '/admin', 'users', 'update', user.id ]
256 }
257
258 private buildActions () {
259 this.userActions = []
260
261 if (this.authService.isLoggedIn()) {
262 const authUser = this.authService.getUser()
263
264 if (this.user && authUser.id === this.user.id) return
265
266 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS)) {
267 this.userActions = this.userActions.concat([
268 {
269 label: this.i18n('Edit'),
270 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
271 },
272 {
273 label: this.i18n('Delete'),
274 handler: ({ user }) => this.removeUser(user)
275 },
276 {
277 label: this.i18n('Ban'),
278 handler: ({ user }: { user: User }) => this.openBanUserModal(user),
279 isDisplayed: ({ user }: { user: User }) => !user.blocked
280 },
281 {
282 label: this.i18n('Unban'),
283 handler: ({ user }: { user: User }) => this.unbanUser(user),
284 isDisplayed: ({ user }: { user: User }) => user.blocked
285 },
286 {
287 label: this.i18n('Set Email as Verified'),
288 handler: ({ user }: { user: User }) => this.setEmailAsVerified(user),
289 isDisplayed: ({ user }: { user: User }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
290 }
291 ])
292 }
293
294 // Actions on accounts/servers
295 if (this.account) {
296 // User actions
297 this.userActions = this.userActions.concat([
298 {
299 label: this.i18n('Mute this account'),
300 isDisplayed: ({ account }: { account: Account }) => account.mutedByUser === false,
301 handler: ({ account }: { account: Account }) => this.blockAccountByUser(account)
302 },
303 {
304 label: this.i18n('Unmute this account'),
305 isDisplayed: ({ account }: { account: Account }) => account.mutedByUser === true,
306 handler: ({ account }: { account: Account }) => this.unblockAccountByUser(account)
307 },
308 {
309 label: this.i18n('Mute the instance'),
310 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === false,
311 handler: ({ account }: { account: Account }) => this.blockServerByUser(account.host)
312 },
313 {
314 label: this.i18n('Unmute the instance'),
315 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === true,
316 handler: ({ account }: { account: Account }) => this.unblockServerByUser(account.host)
317 }
318 ])
319
320 // Instance actions
321 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
322 this.userActions = this.userActions.concat([
323 {
324 label: this.i18n('Mute this account by your instance'),
325 isDisplayed: ({ account }: { account: Account }) => account.mutedByInstance === false,
326 handler: ({ account }: { account: Account }) => this.blockAccountByInstance(account)
327 },
328 {
329 label: this.i18n('Unmute this account by your instance'),
330 isDisplayed: ({ account }: { account: Account }) => account.mutedByInstance === true,
331 handler: ({ account }: { account: Account }) => this.unblockAccountByInstance(account)
332 }
333 ])
334 }
335
336 // Instance actions
337 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
338 this.userActions = this.userActions.concat([
339 {
340 label: this.i18n('Mute the instance by your instance'),
341 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === false,
342 handler: ({ account }: { account: Account }) => this.blockServerByInstance(account.host)
343 },
344 {
345 label: this.i18n('Unmute the instance by your instance'),
346 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === true,
347 handler: ({ account }: { account: Account }) => this.unblockServerByInstance(account.host)
348 }
349 ])
350 }
351 }
352 }
353 }
354 }