Skip to content

Commit 2a400dc

Browse files
author
Martin Krulis
committed
Updating user profile form to better handle password updates and reflect new force update feature.
1 parent 1474a04 commit 2a400dc

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

src/components/forms/EditUserProfileForm/EditUserProfileForm.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const EditUserProfileForm = ({
2121
invalid,
2222
allowChangePassword,
2323
emptyLocalPassword,
24+
canForceChangePassword,
2425
disabledNameChange,
2526
reset,
2627
}) => (
@@ -135,12 +136,14 @@ const EditUserProfileForm = ({
135136
</p>
136137
</div>
137138
) : (
138-
<Field
139-
name="oldPassword"
140-
component={PasswordField}
141-
autoComplete="off"
142-
label={<FormattedMessage id="app.changePasswordForm.oldPassword" defaultMessage="Old Password:" />}
143-
/>
139+
!canForceChangePassword && (
140+
<Field
141+
name="oldPassword"
142+
component={PasswordField}
143+
autoComplete="off"
144+
label={<FormattedMessage id="app.changePasswordForm.oldPassword" defaultMessage="Old Password:" />}
145+
/>
146+
)
144147
)}
145148

146149
<Field
@@ -181,14 +184,18 @@ EditUserProfileForm.propTypes = {
181184
reset: PropTypes.func,
182185
allowChangePassword: PropTypes.bool.isRequired,
183186
emptyLocalPassword: PropTypes.bool.isRequired,
187+
canForceChangePassword: PropTypes.bool.isRequired,
184188
disabledNameChange: PropTypes.bool,
185189
};
186190

187-
const validate = ({ firstName, lastName, email, oldPassword, password, passwordConfirm }, { allowChangePassword }) => {
191+
const validate = (
192+
{ firstName, lastName, email, oldPassword, password, passwordConfirm },
193+
{ disabledNameChange, allowChangePassword, emptyLocalPassword, canForceChangePassword }
194+
) => {
188195
const errors = {};
189196

190-
if (!firstName) {
191-
errors['firstName'] = (
197+
if (!firstName && !disabledNameChange) {
198+
errors.firstName = (
192199
<FormattedMessage
193200
id="app.editUserProfile.validation.emptyFirstName"
194201
defaultMessage="First name cannot be empty."
@@ -197,22 +204,22 @@ const validate = ({ firstName, lastName, email, oldPassword, password, passwordC
197204
}
198205

199206
if (firstName && firstName.length < 2) {
200-
errors['firstName'] = (
207+
errors.firstName = (
201208
<FormattedMessage
202209
id="app.editUserProfile.validation.shortFirstName"
203210
defaultMessage="First name must contain at least 2 characters."
204211
/>
205212
);
206213
}
207214

208-
if (!lastName) {
209-
errors['lastName'] = (
215+
if (!lastName && !disabledNameChange) {
216+
errors.lastName = (
210217
<FormattedMessage id="app.editUserProfile.validation.emptyLastName" defaultMessage="Last name cannot be empty." />
211218
);
212219
}
213220

214221
if (lastName && lastName.length < 2) {
215-
errors['lastName'] = (
222+
errors.lastName = (
216223
<FormattedMessage
217224
id="app.editUserProfile.validation.shortLastName"
218225
defaultMessage="Last name must contain at least 2 characters."
@@ -221,14 +228,14 @@ const validate = ({ firstName, lastName, email, oldPassword, password, passwordC
221228
}
222229

223230
if (email && isEmail(email) === false) {
224-
errors['email'] = (
231+
errors.email = (
225232
<FormattedMessage
226233
id="app.editUserProfile.validation.emailNotValid"
227234
defaultMessage="E-mail address is not valid."
228235
/>
229236
);
230237
} else if (!email) {
231-
errors['email'] = (
238+
errors.email = (
232239
<FormattedMessage
233240
id="app.editUserProfile.validation.emptyEmail"
234241
defaultMessage="E-mail address cannot be empty."
@@ -238,8 +245,17 @@ const validate = ({ firstName, lastName, email, oldPassword, password, passwordC
238245

239246
if (allowChangePassword) {
240247
if (oldPassword || password || passwordConfirm) {
248+
if (!oldPassword && !emptyLocalPassword && !canForceChangePassword) {
249+
errors.oldPassword = (
250+
<FormattedMessage
251+
id="app.editUserProfile.validation.emptyOldPassword"
252+
defaultMessage="Current password has to be verified before it can be changed."
253+
/>
254+
);
255+
}
256+
241257
if (!password || password.length === 0) {
242-
errors['password'] = (
258+
errors.password = (
243259
<FormattedMessage
244260
id="app.editUserProfile.validation.emptyNewPassword"
245261
defaultMessage="New password cannot be empty if you want to change your password."
@@ -248,7 +264,7 @@ const validate = ({ firstName, lastName, email, oldPassword, password, passwordC
248264
}
249265

250266
if (password !== passwordConfirm) {
251-
errors['passwordConfirm'] = (
267+
errors.passwordConfirm = (
252268
<FormattedMessage
253269
id="app.editUserProfile.validation.passwordsDontMatch"
254270
defaultMessage="Passwords do not match."
@@ -257,7 +273,7 @@ const validate = ({ firstName, lastName, email, oldPassword, password, passwordC
257273
}
258274

259275
if (password && password.length > 0 && oldPassword && oldPassword.length > 0 && password === oldPassword) {
260-
errors['password'] = (
276+
errors.password = (
261277
<FormattedMessage
262278
id="app.editUserProfile.validation.samePasswords"
263279
defaultMessage="Changing your password to the same password does not make any sense."

src/locales/cs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@
467467
"app.editUserProfile.validation.emptyFirstName": "Jméno nemůže být prázdné.",
468468
"app.editUserProfile.validation.emptyLastName": "Příjmení nemůže být prázdné.",
469469
"app.editUserProfile.validation.emptyNewPassword": "Nové heslo nemůže být prázdné pokud si měníte heslo.",
470+
"app.editUserProfile.validation.emptyOldPassword": "Je potřeba ověřit aktuální heslo, aby mohlo být změněno.",
470471
"app.editUserProfile.validation.passwordsDontMatch": "Hesla se neshodují.",
471472
"app.editUserProfile.validation.samePasswords": "Změnit Vaše heslo na stejné nedává žádný smysl.",
472473
"app.editUserProfile.validation.shortFirstName": "Jméno musí obsahovat alespoň 2 znaky.",

src/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@
467467
"app.editUserProfile.validation.emptyFirstName": "First name cannot be empty.",
468468
"app.editUserProfile.validation.emptyLastName": "Last name cannot be empty.",
469469
"app.editUserProfile.validation.emptyNewPassword": "New password cannot be empty if you want to change your password.",
470+
"app.editUserProfile.validation.emptyOldPassword": "Current password has to be verified before it can be changed.",
470471
"app.editUserProfile.validation.passwordsDontMatch": "Passwords do not match.",
471472
"app.editUserProfile.validation.samePasswords": "Changing your password to the same password does not make any sense.",
472473
"app.editUserProfile.validation.shortFirstName": "First name must contain at least 2 characters.",

src/locales/whitelist_en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@
467467
"app.editUserProfile.validation.emptyFirstName",
468468
"app.editUserProfile.validation.emptyLastName",
469469
"app.editUserProfile.validation.emptyNewPassword",
470+
"app.editUserProfile.validation.emptyOldPassword",
470471
"app.editUserProfile.validation.passwordsDontMatch",
471472
"app.editUserProfile.validation.samePasswords",
472473
"app.editUserProfile.validation.shortFirstName",

src/pages/EditUser/EditUser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class EditUser extends Component {
173173
}}
174174
allowChangePassword={data.privateData.isLocal}
175175
emptyLocalPassword={data.privateData.emptyLocalPassword}
176+
canForceChangePassword={isSuperAdmin && data.id !== loggedUserId}
176177
disabledNameChange={data.privateData.isExternal && !isSuperAdmin}
177178
/>
178179
</Col>

0 commit comments

Comments
 (0)