diff --git a/addons/api/addon/serializers/account.js b/addons/api/addon/serializers/account.js index 08a6a6028..37fbc9270 100644 --- a/addons/api/addon/serializers/account.js +++ b/addons/api/addon/serializers/account.js @@ -4,6 +4,11 @@ */ import ApplicationSerializer from './application'; +import { + TYPE_AUTH_METHOD_PASSWORD, + TYPE_AUTH_METHOD_OIDC, + TYPE_AUTH_METHOD_LDAP, +} from 'api/models/auth-method'; export default class AccountSerializer extends ApplicationSerializer { // =properties @@ -26,13 +31,31 @@ export default class AccountSerializer extends ApplicationSerializer { */ serialize(snapshot) { switch (snapshot.record.type) { - case 'password': + case TYPE_AUTH_METHOD_PASSWORD: return this.serializePassword(...arguments); - case 'oidc': + case TYPE_AUTH_METHOD_OIDC: return this.serializeOIDC(...arguments); + case TYPE_AUTH_METHOD_LDAP: + return this.serializeLDAP(...arguments); } } + /** + * LDAP serialization limits the `attributes` posted on create and omits + * `attributes` entirely on update. + */ + serializeLDAP(snapshot) { + const { isNew } = snapshot?.record || {}; + let serialized = super.serialize(...arguments); + delete serialized.attributes; + if (isNew) { + serialized.attributes = {}; + const { login_name } = snapshot.record; + if (login_name) serialized.attributes.login_name = login_name; + } + return serialized; + } + /** * OIDC serialization limits the `attributes` posted on create and omits * `attributes` entirely on update. diff --git a/addons/api/tests/unit/serializers/account-test.js b/addons/api/tests/unit/serializers/account-test.js index 326298a26..d630df393 100644 --- a/addons/api/tests/unit/serializers/account-test.js +++ b/addons/api/tests/unit/serializers/account-test.js @@ -8,6 +8,7 @@ import { setupTest } from 'ember-qunit'; import { TYPE_AUTH_METHOD_PASSWORD, TYPE_AUTH_METHOD_OIDC, + TYPE_AUTH_METHOD_LDAP, } from 'api/models/auth-method'; module('Unit | Serializer | account', function (hooks) { @@ -186,4 +187,68 @@ module('Unit | Serializer | account', function (hooks) { version: 1, }); }); + + test('it serializes LDAP-type with only login_name in `attributes` on create', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('account'); + const record = store.createRecord('account', { + type: TYPE_AUTH_METHOD_LDAP, + name: 'Name', + auth_method_id: '1', + description: 'Description', + login_name: 'Login Name', + full_name: 'Full Nname', + email: 'Email', + dn: 'dn', + member_of_groups: ['Group'], + version: 1, + }); + const snapshot = record._createSnapshot(); + const serializedRecord = serializer.serialize(snapshot); + assert.deepEqual(serializedRecord, { + type: TYPE_AUTH_METHOD_LDAP, + name: 'Name', + auth_method_id: '1', + description: 'Description', + version: 1, + attributes: { + login_name: 'Login Name', + }, + }); + }); + + test('it serializes LDAP-type without `attributes`', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('account'); + store.push({ + data: { + id: '1', + type: 'account', + attributes: { + type: TYPE_AUTH_METHOD_LDAP, + name: 'Name', + auth_method_id: '1', + description: 'Description', + login_name: 'Login Name', + full_name: 'Full Nname', + email: 'Email', + dn: 'dn', + member_of_groups: ['Group'], + version: 1, + }, + }, + }); + const record = store.peekRecord('account', '1'); + const snapshot = record._createSnapshot(); + const serializedRecord = serializer.serialize(snapshot); + assert.deepEqual(serializedRecord, { + type: TYPE_AUTH_METHOD_LDAP, + name: 'Name', + auth_method_id: '1', + description: 'Description', + version: 1, + }); + }); });