Update userpass validations to handle password_hash (#26577)

* Update userpass validations to handle password_hash

* Add changelog
This commit is contained in:
Chelsea Shaw
2024-04-22 12:13:53 -05:00
committed by GitHub
parent 8ada0cedaf
commit 0a505f9651
4 changed files with 37 additions and 26 deletions

3
changelog/26577.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:improvement
ui: Update userpass user form to allow setting `password_hash` field.
```

View File

@@ -35,8 +35,27 @@ export default Component.extend({
props: computed('model', function () {
return this.model.serialize();
}),
validateForm() {
// Only validate on new models because blank passwords will not be updated
// in practice this only happens for userpass users
if (this.model.validate && this.model.isNew) {
const { isValid, state } = this.model.validate();
this.setProperties({
modelValidations: state,
isFormInvalid: !isValid,
});
return isValid;
} else {
this.set('isFormInvalid', false);
return true;
}
},
saveModel: task(
waitFor(function* () {
const isValid = this.validateForm();
if (!isValid) {
return;
}
try {
yield this.model.save();
} catch (err) {
@@ -71,16 +90,6 @@ export default Component.extend({
actions: {
onKeyUp(name, value) {
this.model.set(name, value);
if (this.model.validate) {
// Set validation error message for updated attribute
const { isValid, state } = this.model.validate();
this.setProperties({
modelValidations: state,
isFormInvalid: !isValid,
});
} else {
this.set('isFormInvalid', false);
}
},
deleteItem() {
this.model.destroyRecord().then(() => {

View File

@@ -27,6 +27,7 @@ import {
pathToHelpUrlSegment,
reducePathsByPathName,
} from 'vault/utils/openapi-helpers';
import { isPresent } from '@ember/utils';
export default class PathHelpService extends Service {
@service store;
@@ -272,16 +273,19 @@ export default class PathHelpService extends Service {
// Build and add validations on model
// NOTE: For initial phase, initialize validations only for user pass auth
if (backend === 'userpass') {
const validations = fieldGroups.reduce((obj, element) => {
if (element.default) {
element.default.forEach((v) => {
const key = v.options.fieldValue || v.name;
obj[key] = [{ type: 'presence', message: `${v.name} can't be blank` }];
});
}
return obj;
}, {});
const validations = {
password: [
{
validator(model) {
return (
!(isPresent(model.password) && isPresent(model.passwordHash)) &&
(isPresent(model.password) || isPresent(model.passwordHash))
);
},
message: 'You must provide either password or password hash, but not both.',
},
],
};
newModel = withModelValidations(validations)(class GeneratedItemModel extends newModel {});
}
}

View File

@@ -59,12 +59,7 @@
<div class="box is-sideless is-fullwidth is-marginless">
<NamespaceReminder @mode="save" @noun={{this.itemType}} />
<MessageError @model={{this.model}} />
<FormFieldGroups
@model={{this.model}}
@mode={{this.mode}}
@onKeyUp={{action "onKeyUp"}}
@modelValidations={{this.modelValidations}}
/>
<FormFieldGroups @model={{this.model}} @mode={{this.mode}} @modelValidations={{this.modelValidations}} />
</div>
<div class="field is-grouped-split box is-fullwidth is-bottomless">
<Hds::ButtonSet>