1
1
import React , { Component } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
- import { Button , InputGroup , InputGroupAddon , Input , FormGroup , Label , FormFeedback } from 'reactstrap' ;
4
- import { loadComponent } from 'lib/Injector' ;
5
- import fetch from 'isomorphic-fetch' ;
6
3
import Config from 'lib/Config' ;
4
+ import i18n from 'i18n' ;
5
+ import SudoModePasswordField from '../../components/SudoModePasswordField/SudoModePasswordField' ;
7
6
8
7
// See SudoModeController::getClientConfig()
9
8
const configSectionKey = 'SilverStripe\\Admin\\SudoModeController' ;
@@ -29,78 +28,7 @@ const withSudoMode = (WrappedComponent) => {
29
28
30
29
this . state = {
31
30
active : Config . getSection ( configSectionKey ) . sudoModeActive || false ,
32
- showVerification : false ,
33
- loading : false ,
34
- errorMessage : null ,
35
31
} ;
36
-
37
- this . handleConfirmNotice = this . handleConfirmNotice . bind ( this ) ;
38
- this . handleVerify = this . handleVerify . bind ( this ) ;
39
- this . handleVerifyInputKeyPress = this . handleVerifyInputKeyPress . bind ( this ) ;
40
-
41
- // React 15 compatible ref callback
42
- this . passwordInput = null ;
43
- this . setPasswordInput = element => {
44
- this . passwordInput = element ;
45
- } ;
46
- }
47
-
48
- /**
49
- * Action called when clicking the button to confirm the sudo mode notice
50
- * and trigger the verification form to be rendered.
51
- */
52
- handleConfirmNotice ( ) {
53
- this . setState ( {
54
- showVerification : true ,
55
- } , ( ) => this . passwordInput && this . passwordInput . focus ( ) ) ;
56
- }
57
-
58
- /**
59
- * Action called when the user has entered their password and requested
60
- * verification of sudo mode state.
61
- */
62
- handleVerify ( ) {
63
- this . setState ( {
64
- loading : true ,
65
- } ) ;
66
-
67
- const payload = new FormData ( ) ;
68
- payload . append ( 'SecurityID' , Config . get ( 'SecurityID' ) ) ;
69
- payload . append ( 'Password' , this . passwordInput . value ) ;
70
-
71
- // Validate the request
72
- fetch ( Config . getSection ( configSectionKey ) . endpoints . activate , {
73
- method : 'POST' ,
74
- body : payload ,
75
- } ) . then ( response => response . json ( ) . then ( result => {
76
- // Happy path, send the user to the wrapped component
77
- if ( result . result ) {
78
- return this . setState ( {
79
- loading : false ,
80
- active : true ,
81
- } ) ;
82
- }
83
-
84
- // Validation error, show them the message
85
- return this . setState ( {
86
- loading : false ,
87
- errorMessage : result . message ,
88
- } , ( ) => this . passwordInput . focus ( ) ) ;
89
- } ) ) ;
90
- }
91
-
92
- /**
93
- * Treat pressing enter on the password field the same as clicking the
94
- * verify button.
95
- *
96
- * @param {object } event
97
- */
98
- handleVerifyInputKeyPress ( event ) {
99
- if ( event . charCode === 13 ) {
100
- event . stopPropagation ( ) ;
101
- event . preventDefault ( ) ;
102
- this . handleVerify ( ) ;
103
- }
104
32
}
105
33
106
34
/**
@@ -112,117 +40,13 @@ const withSudoMode = (WrappedComponent) => {
112
40
return this . state . active === true ;
113
41
}
114
42
115
- /**
116
- * Renders a notice to the user that they will need to verify themself
117
- * to enter sudo mode and continue to use this functionality.
118
- *
119
- * @returns {HTMLElement }
120
- */
121
- renderSudoModeNotice ( ) {
122
- const { i18n } = window ;
123
- const { showVerification } = this . state ;
124
-
125
- const helpLink = Config . getSection ( configSectionKey ) . helpLink || null ;
126
-
127
- return (
128
- < div className = "sudo-mode__notice sudo-mode__notice--required" >
129
- < p className = "sudo-mode__notice-message" >
130
- { i18n . _t ( 'Admin.VERIFY_ITS_YOU' , 'Verify it\'s you first.' ) }
131
- { helpLink && (
132
- < a href = { helpLink } className = "sudo-mode__notice-help" target = "_blank" rel = "noopener noreferrer" >
133
- { i18n . _t ( 'Admin.WHATS_THIS' , 'What is this?' ) }
134
- </ a >
135
- ) }
136
- </ p >
137
- { ! showVerification && (
138
- < Button
139
- className = "sudo-mode__notice-button font-icon-lock"
140
- color = "info"
141
- onClick = { this . handleConfirmNotice }
142
- >
143
- { i18n . _t ( 'Admin.VERIFY_TO_CONTINUE' , 'Verify to continue' ) }
144
- </ Button >
145
- ) }
146
- </ div >
147
- ) ;
148
- }
149
-
150
- /**
151
- * Renders the password verification form to enter sudo mode
152
- *
153
- * @returns {HTMLElement }
154
- */
155
- renderSudoModeVerification ( ) {
156
- const { i18n } = window ;
157
- const { errorMessage } = this . state ;
158
-
159
- const inputProps = {
160
- type : 'password' ,
161
- name : 'sudoModePassword' ,
162
- id : 'sudoModePassword' ,
163
- className : 'no-change-track' ,
164
- onKeyPress : this . handleVerifyInputKeyPress ,
165
- innerRef : this . setPasswordInput ,
166
- } ;
167
- const validationProps = errorMessage ? { valid : false , invalid : true } : { } ;
168
-
169
- return (
170
- < div className = "sudo-mode__verify" >
171
- < FormGroup className = "sudo-mode__verify-form-group" >
172
- < Label for = "sudoModePassword" >
173
- { i18n . _t ( 'Admin.ENTER_PASSWORD' , 'Enter your password' ) }
174
- </ Label >
175
-
176
- < InputGroup >
177
- < Input { ...inputProps } { ...validationProps } />
178
- < InputGroupAddon addonType = "append" >
179
- < Button
180
- className = "sudo-mode__verify-button"
181
- color = "info"
182
- onClick = { this . handleVerify }
183
- >
184
- { i18n . _t ( 'Admin.VERIFY' , 'Verify' ) }
185
- </ Button >
186
- </ InputGroupAddon >
187
- < FormFeedback > { errorMessage } </ FormFeedback >
188
- </ InputGroup >
189
- </ FormGroup >
190
- </ div >
191
- ) ;
192
- }
193
-
194
- /**
195
- * Renders the "sudo mode" notice or verification screen
196
- *
197
- * @returns {HTMLElement }
198
- */
199
- renderSudoMode ( ) {
200
- const { showVerification, loading } = this . state ;
201
-
202
- const LoadingComponent = this . props . LoadingComponent || loadComponent (
203
- 'CircularLoading' ,
204
- 'SudoMode'
205
- ) ;
206
-
207
- if ( loading ) {
208
- return (
209
- < div className = "sudo-mode alert alert-info" >
210
- < LoadingComponent block />
211
- </ div >
212
- ) ;
213
- }
214
-
215
- return (
216
- < div className = "sudo-mode alert alert-info" >
217
- { this . renderSudoModeNotice ( ) }
218
- { showVerification && this . renderSudoModeVerification ( ) }
219
- </ div >
220
- ) ;
221
- }
222
-
223
43
render ( ) {
224
44
if ( ! this . isSudoModeActive ( ) ) {
225
- return this . renderSudoMode ( ) ;
45
+ return < SudoModePasswordField
46
+ verifyMessage = { i18n . _t ( 'Admin.VERIFY_ITS_YOU' , 'Verify it\'s you first.' ) }
47
+ onSuccess = { ( ) => this . setState ( { active : true } ) }
48
+ autocomplete = "off"
49
+ /> ; // this.renderSudoMode();
226
50
}
227
51
return < WrappedComponent { ...this . props } /> ;
228
52
}
0 commit comments