Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update userpass validations to handle password_hash #26577

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/26577.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
ui: Update userpass user form to allow setting `password_hash` field.
```
29 changes: 19 additions & 10 deletions ui/app/components/generated-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(() => {
Expand Down
24 changes: 14 additions & 10 deletions ui/app/services/path-help.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {});
}
}
Expand Down
7 changes: 1 addition & 6 deletions ui/app/templates/components/generated-item.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Loading