diff --git a/addons/api/addon/models/account.js b/addons/api/addon/models/account.js index 76321d86d..4c52347f5 100644 --- a/addons/api/addon/models/account.js +++ b/addons/api/addon/models/account.js @@ -9,4 +9,28 @@ export default class AccountModel extends GeneratedAccountModel { */ @fragment('fragment-account-attributes', { defaultValue: {} }) attributes; + // =methods + + /** + * Save account password via the `set-password` method. + * See serializer and adapter for more information. + * @param {string} password + * @param {object} options + * @param {object} options.adapterOptions + * @return {Promise} + */ + setPassword(password, options={ adapterOptions: {} }) { + const defaultAdapterOptions = { + method: 'set-password', + password + }; + return this.save({ + ...options, + adapterOptions: { + ...defaultAdapterOptions, + ...options.adapterOptions + } + }); + } + } diff --git a/addons/api/addon/models/fragment-account-attributes.js b/addons/api/addon/models/fragment-account-attributes.js index ee2281f18..76ee82487 100644 --- a/addons/api/addon/models/fragment-account-attributes.js +++ b/addons/api/addon/models/fragment-account-attributes.js @@ -7,7 +7,4 @@ export default class FragmentAccountAttributesModel extends Fragment { description: 'The account login name' }) login_name; - @attr('string', { - description: 'The account password' - }) password; } diff --git a/addons/api/addon/models/fragment-target-attributes.js b/addons/api/addon/models/fragment-target-attributes.js new file mode 100644 index 000000000..1ab994d9d --- /dev/null +++ b/addons/api/addon/models/fragment-target-attributes.js @@ -0,0 +1,10 @@ +import Fragment from 'ember-data-model-fragments/fragment'; +import { attr } from '@ember-data/model'; + +export default class FragmentTargetAttributesModel extends Fragment { + + @attr('number', { + description: 'The default port a target should use if present.' + }) default_port; + +} diff --git a/addons/api/addon/models/target.js b/addons/api/addon/models/target.js index 03a3ad5d7..57b87ff29 100644 --- a/addons/api/addon/models/target.js +++ b/addons/api/addon/models/target.js @@ -1,11 +1,17 @@ import GeneratedTargetModel from '../generated/models/target'; -import { fragmentArray } from 'ember-data-model-fragments/attributes'; +import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes'; import { computed } from '@ember/object'; export default class TargetModel extends GeneratedTargetModel { // =attributes + /** + * Attributes of this resource, if any, represented as a JSON fragment. + * @type {FragmentTargetAttributesModel} + */ + @fragment('fragment-target-attributes', { defaultValue: {} }) attributes; + /** * @type {[FragmentHostSetModel]} */ diff --git a/addons/api/addon/serializers/account.js b/addons/api/addon/serializers/account.js new file mode 100644 index 000000000..c014c2361 --- /dev/null +++ b/addons/api/addon/serializers/account.js @@ -0,0 +1,39 @@ +import ApplicationSerializer from './application'; + +export default class AccountSerializer extends ApplicationSerializer { + + // =methods + + /** + * If `adapterOptions.method` is `set-password`, the serialization should + * include only **password** and the version. + * If `adapterOptions.password` is set, the serialization should + * include **password** in it's `attributes`. + * @override + * @param {Snapshot} snapshot + * @return {object} + */ + serialize(snapshot) { + const password = snapshot?.adapterOptions?.password; + let serialized = super.serialize(...arguments); + if (password && snapshot?.record?.isNew) serialized.attributes.password = password; + if (snapshot?.adapterOptions?.method === 'set-password') { + serialized = this.serializeForSetPassword(snapshot, password); + } + return serialized; + } + + /** + * Returns a payload containing only new password. + * @param {Snapshot} snapshot + * @param {string} password + * @return {object} + */ + serializeForSetPassword(snapshot, password) { + return { + version: snapshot.attr('version'), + password, + }; + } + +} diff --git a/addons/api/app/models/fragment-target-attributes.js b/addons/api/app/models/fragment-target-attributes.js new file mode 100644 index 000000000..41eb7d88f --- /dev/null +++ b/addons/api/app/models/fragment-target-attributes.js @@ -0,0 +1 @@ +export { default } from 'api/models/fragment-target-attributes'; diff --git a/addons/api/app/serializers/account.js b/addons/api/app/serializers/account.js new file mode 100644 index 000000000..af9b2cc1c --- /dev/null +++ b/addons/api/app/serializers/account.js @@ -0,0 +1 @@ +export { default } from 'api/serializers/account'; diff --git a/addons/api/tests/unit/models/account-test.js b/addons/api/tests/unit/models/account-test.js index 4237ae6b6..dcd3fe8bb 100644 --- a/addons/api/tests/unit/models/account-test.js +++ b/addons/api/tests/unit/models/account-test.js @@ -4,21 +4,10 @@ import { setupTest } from 'ember-qunit'; module('Unit | Model | account', function(hooks) { setupTest(hooks); - // Replace this with your real tests. test('it exists', function(assert) { let store = this.owner.lookup('service:store'); let model = store.createRecord('account', {}); assert.ok(model); }); - test('it contains attributes', function(assert) { - let store = this.owner.lookup('service:store'); - let model = store.createRecord('account', { - type: 'password', - attributes: { - login_name: 'loginname' - } - }); - assert.equal(model.attributes.login_name, 'loginname'); - }); }); diff --git a/addons/api/tests/unit/serializers/account-test.js b/addons/api/tests/unit/serializers/account-test.js new file mode 100644 index 000000000..85f9b655f --- /dev/null +++ b/addons/api/tests/unit/serializers/account-test.js @@ -0,0 +1,97 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Serializer | account', function(hooks) { + setupTest(hooks); + + test('it serializes normally', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('account'); + const record = store.createRecord('account', { + type: 'password', + name: 'Name', + auth_method_id: '1', + description: 'Description', + attributes: { + login_name: 'Login Name', + }, + version: 1 + }); + const snapshot = record._createSnapshot(); + snapshot.adapterOptions = {}; + const serializedRecord = serializer.serialize(snapshot); + assert.deepEqual(serializedRecord, { + type: 'password', + name: 'Name', + auth_method_id: '1', + description: 'Description', + attributes: { + login_name: 'Login Name' + }, + version: 1 + }); + }); + + test('it serializes password attribute when `adapterOptions.password` is true', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('account'); + const record = store.createRecord('account', { + type: 'password', + name: 'Name', + auth_method_id: '1', + description: 'Description', + attributes: { + login_name: 'Login Name', + }, + version: 1 + }); + const snapshot = record._createSnapshot(); + snapshot.adapterOptions = { + password: 'Password' + }; + const serializedRecord = serializer.serialize(snapshot); + assert.deepEqual(serializedRecord, { + type: 'password', + name: 'Name', + auth_method_id: '1', + description: 'Description', + attributes: { + login_name: 'Login Name', + password: 'Password' + }, + version: 1 + }); + }); + + test('it does not serialize password for non-new records', function (assert) { + assert.expect(0); + }); + + test('it serializes only password when `adapterOptions.method` is set to `set-password`', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('account'); + const record = store.createRecord('account', { + type: 'password', + name: 'Name', + description: 'Description', + attributes: { + login_name: 'Login Name' + }, + auth_method_id: '1', + version: 1 + }); + const snapshot = record._createSnapshot(); + snapshot.adapterOptions = { + method: 'set-password', + password: 'Password' + }; + const serializedRecord = serializer.serialize(snapshot); + assert.deepEqual(serializedRecord, { + password: 'Password', + version: 1 + }); + }); +}); diff --git a/addons/api/tests/unit/serializers/target-test.js b/addons/api/tests/unit/serializers/target-test.js index 94cab05e5..e129331eb 100644 --- a/addons/api/tests/unit/serializers/target-test.js +++ b/addons/api/tests/unit/serializers/target-test.js @@ -30,7 +30,10 @@ module('Unit | Serializer | target', function(hooks) { description: 'Description', version: 1, type: 'tcp', - scope_id: 'org_1' + scope_id: 'org_1', + attributes: { + default_port: null + } }); }); diff --git a/ui/core/app/components/form/target/index.hbs b/ui/core/app/components/form/target/index.hbs index a6e5a204e..5a977d2e4 100644 --- a/ui/core/app/components/form/target/index.hbs +++ b/ui/core/app/components/form/target/index.hbs @@ -14,11 +14,10 @@