@@ -2,6 +2,8 @@ import * as React from 'react';
2
2
import { Modal , Button , Spinner } from '@patternfly/react-core' ;
3
3
import { ExclamationTriangleIcon } from '@patternfly/react-icons' ;
4
4
import { UserType , UserAPI } from '../../api' ;
5
+ import { mapErrorMessages } from '../../utilities' ;
6
+ import { AppContext } from '../../loaders/app-context' ;
5
7
6
8
interface IState {
7
9
isWaitingForResponse : boolean ;
@@ -11,10 +13,12 @@ interface IProps {
11
13
isOpen : boolean ;
12
14
user ?: UserType ;
13
15
closeModal : ( didDelete : boolean ) => void ;
14
- addAlert : ( message , variant ) => void ;
16
+ addAlert : ( message , variant , description ? ) => void ;
15
17
}
16
18
17
19
export class DeleteUserModal extends React . Component < IProps , IState > {
20
+ static contextType = AppContext ;
21
+
18
22
constructor ( props ) {
19
23
super ( props ) ;
20
24
@@ -47,7 +51,7 @@ export class DeleteUserModal extends React.Component<IProps, IState> {
47
51
}
48
52
actions = { [
49
53
< Button
50
- isDisabled = { isWaitingForResponse }
54
+ isDisabled = { isWaitingForResponse || this . isUserSelfOrAdmin ( user ) }
51
55
key = 'delete'
52
56
variant = 'danger'
53
57
onClick = { ( ) => this . deleteUser ( ) }
@@ -59,23 +63,45 @@ export class DeleteUserModal extends React.Component<IProps, IState> {
59
63
</ Button > ,
60
64
] }
61
65
>
62
- { user . username } will be permanently deleted.
66
+ { this . getActionDescription ( user ) }
63
67
</ Modal >
64
68
) ;
65
69
}
66
70
71
+ private getActionDescription ( user : UserType ) {
72
+ if ( user . is_superuser ) {
73
+ return 'Deleting super users is not allowed.' ;
74
+ } else if ( user . id === this . context . user . id ) {
75
+ return 'Deleting yourself is not allowed.' ;
76
+ }
77
+
78
+ return `${ user . username } will be permanently deleted.` ;
79
+ }
80
+
81
+ private isUserSelfOrAdmin = ( user : UserType ) : boolean => {
82
+ return user . is_superuser || user . id === this . context . user . id ;
83
+ } ;
84
+
67
85
private deleteUser = ( ) => {
68
86
this . setState ( { isWaitingForResponse : true } , ( ) =>
69
87
UserAPI . delete ( this . props . user . id )
70
- . then ( this . waitForDeleteConfirm ( this . props . user . id ) )
71
- . catch ( ( ) => this . props . addAlert ( 'Error deleting user.' , 'danger' ) ) ,
88
+ . then ( ( ) => this . waitForDeleteConfirm ( this . props . user . id ) )
89
+ . catch ( err => {
90
+ this . props . addAlert (
91
+ 'Error deleting user.' ,
92
+ 'danger' ,
93
+ mapErrorMessages ( err ) [ '__nofield' ] ,
94
+ ) ;
95
+ this . props . closeModal ( false ) ;
96
+ } )
97
+ . finally ( ( ) => this . setState ( { isWaitingForResponse : false } ) ) ,
72
98
) ;
73
99
} ;
74
100
75
101
// Wait for the user to actually get removed from the database before closing the
76
102
// modal
77
103
private waitForDeleteConfirm ( user ) {
78
- UserAPI . get ( user . id )
104
+ UserAPI . get ( user )
79
105
. then ( async result => {
80
106
// wait half a second
81
107
await new Promise ( r => setTimeout ( r , 500 ) ) ;
0 commit comments