Skip to content

Commit

Permalink
Updating user profile form to better handle password updates and refl…
Browse files Browse the repository at this point in the history
…ect new force update feature.
  • Loading branch information
Martin Krulis committed Sep 18, 2019
1 parent 1474a04 commit 2a400dc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/components/forms/EditUserProfileForm/EditUserProfileForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const EditUserProfileForm = ({
invalid,
allowChangePassword,
emptyLocalPassword,
canForceChangePassword,
disabledNameChange,
reset,
}) => (
Expand Down Expand Up @@ -135,12 +136,14 @@ const EditUserProfileForm = ({
</p>
</div>
) : (
<Field
name="oldPassword"
component={PasswordField}
autoComplete="off"
label={<FormattedMessage id="app.changePasswordForm.oldPassword" defaultMessage="Old Password:" />}
/>
!canForceChangePassword && (
<Field
name="oldPassword"
component={PasswordField}
autoComplete="off"
label={<FormattedMessage id="app.changePasswordForm.oldPassword" defaultMessage="Old Password:" />}
/>
)
)}

<Field
Expand Down Expand Up @@ -181,14 +184,18 @@ EditUserProfileForm.propTypes = {
reset: PropTypes.func,
allowChangePassword: PropTypes.bool.isRequired,
emptyLocalPassword: PropTypes.bool.isRequired,
canForceChangePassword: PropTypes.bool.isRequired,
disabledNameChange: PropTypes.bool,
};

const validate = ({ firstName, lastName, email, oldPassword, password, passwordConfirm }, { allowChangePassword }) => {
const validate = (
{ firstName, lastName, email, oldPassword, password, passwordConfirm },
{ disabledNameChange, allowChangePassword, emptyLocalPassword, canForceChangePassword }
) => {
const errors = {};

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

if (firstName && firstName.length < 2) {
errors['firstName'] = (
errors.firstName = (
<FormattedMessage
id="app.editUserProfile.validation.shortFirstName"
defaultMessage="First name must contain at least 2 characters."
/>
);
}

if (!lastName) {
errors['lastName'] = (
if (!lastName && !disabledNameChange) {
errors.lastName = (
<FormattedMessage id="app.editUserProfile.validation.emptyLastName" defaultMessage="Last name cannot be empty." />
);
}

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

if (email && isEmail(email) === false) {
errors['email'] = (
errors.email = (
<FormattedMessage
id="app.editUserProfile.validation.emailNotValid"
defaultMessage="E-mail address is not valid."
/>
);
} else if (!email) {
errors['email'] = (
errors.email = (
<FormattedMessage
id="app.editUserProfile.validation.emptyEmail"
defaultMessage="E-mail address cannot be empty."
Expand All @@ -238,8 +245,17 @@ const validate = ({ firstName, lastName, email, oldPassword, password, passwordC

if (allowChangePassword) {
if (oldPassword || password || passwordConfirm) {
if (!oldPassword && !emptyLocalPassword && !canForceChangePassword) {
errors.oldPassword = (
<FormattedMessage
id="app.editUserProfile.validation.emptyOldPassword"
defaultMessage="Current password has to be verified before it can be changed."
/>
);
}

if (!password || password.length === 0) {
errors['password'] = (
errors.password = (
<FormattedMessage
id="app.editUserProfile.validation.emptyNewPassword"
defaultMessage="New password cannot be empty if you want to change your password."
Expand All @@ -248,7 +264,7 @@ const validate = ({ firstName, lastName, email, oldPassword, password, passwordC
}

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

if (password && password.length > 0 && oldPassword && oldPassword.length > 0 && password === oldPassword) {
errors['password'] = (
errors.password = (
<FormattedMessage
id="app.editUserProfile.validation.samePasswords"
defaultMessage="Changing your password to the same password does not make any sense."
Expand Down
1 change: 1 addition & 0 deletions src/locales/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@
"app.editUserProfile.validation.emptyFirstName": "Jméno nemůže být prázdné.",
"app.editUserProfile.validation.emptyLastName": "Příjmení nemůže být prázdné.",
"app.editUserProfile.validation.emptyNewPassword": "Nové heslo nemůže být prázdné pokud si měníte heslo.",
"app.editUserProfile.validation.emptyOldPassword": "Je potřeba ověřit aktuální heslo, aby mohlo být změněno.",
"app.editUserProfile.validation.passwordsDontMatch": "Hesla se neshodují.",
"app.editUserProfile.validation.samePasswords": "Změnit Vaše heslo na stejné nedává žádný smysl.",
"app.editUserProfile.validation.shortFirstName": "Jméno musí obsahovat alespoň 2 znaky.",
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@
"app.editUserProfile.validation.emptyFirstName": "First name cannot be empty.",
"app.editUserProfile.validation.emptyLastName": "Last name cannot be empty.",
"app.editUserProfile.validation.emptyNewPassword": "New password cannot be empty if you want to change your password.",
"app.editUserProfile.validation.emptyOldPassword": "Current password has to be verified before it can be changed.",
"app.editUserProfile.validation.passwordsDontMatch": "Passwords do not match.",
"app.editUserProfile.validation.samePasswords": "Changing your password to the same password does not make any sense.",
"app.editUserProfile.validation.shortFirstName": "First name must contain at least 2 characters.",
Expand Down
1 change: 1 addition & 0 deletions src/locales/whitelist_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@
"app.editUserProfile.validation.emptyFirstName",
"app.editUserProfile.validation.emptyLastName",
"app.editUserProfile.validation.emptyNewPassword",
"app.editUserProfile.validation.emptyOldPassword",
"app.editUserProfile.validation.passwordsDontMatch",
"app.editUserProfile.validation.samePasswords",
"app.editUserProfile.validation.shortFirstName",
Expand Down
1 change: 1 addition & 0 deletions src/pages/EditUser/EditUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class EditUser extends Component {
}}
allowChangePassword={data.privateData.isLocal}
emptyLocalPassword={data.privateData.emptyLocalPassword}
canForceChangePassword={isSuperAdmin && data.id !== loggedUserId}
disabledNameChange={data.privateData.isExternal && !isSuperAdmin}
/>
</Col>
Expand Down

0 comments on commit 2a400dc

Please sign in to comment.