fix: Remove password attribute from account model
This commit is contained in:
5 files changed
+126
-9
No files matched your search
@@ -9,4 +9,29 @@ 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}
|
||||
*/
|
||||
savePassword(password, options={ adapterOptions: {} }) {
|
||||
const defaultAdapterOptions = {
|
||||
method: 'set-password',
|
||||
serializeWithNewPassword: true,
|
||||
password
|
||||
};
|
||||
return this.save({
|
||||
...options,
|
||||
adapterOptions: {
|
||||
...defaultAdapterOptions,
|
||||
...options.adapterOptions
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,4 @@ export default class FragmentAccountAttributesModel extends Fragment {
|
||||
description: 'The account login name'
|
||||
}) login_name;
|
||||
|
||||
@attr('string', {
|
||||
description: 'The account password'
|
||||
}) password;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import ApplicationSerializer from './application';
|
||||
import { get } from '@ember/object';
|
||||
|
||||
export default class AccountSerializer extends ApplicationSerializer {
|
||||
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* If `adapterOptions.serializeWithNewPassword` is true, the serialization should
|
||||
* include **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 = get(snapshot, 'adapterOptions.password');
|
||||
const serializeWithNewPassword = get(snapshot, 'adapterOptions.serializeWithNewPassword');
|
||||
let serialized = super.serialize(...arguments);
|
||||
if (password) serialized.attributes.password = password;
|
||||
if (serializeWithNewPassword) serialized = this.serializeWithNewPassword(snapshot, password)
|
||||
return serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a payload containing only new password.
|
||||
* @param {Snapshot} snapshot
|
||||
* @param {string} password
|
||||
* @return {object}
|
||||
*/
|
||||
serializeWithNewPassword(snapshot, password) {
|
||||
return {
|
||||
version: snapshot.attr('version'),
|
||||
password,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from 'api/serializers/account';
|
||||
@@ -4,19 +4,18 @@ import { setupTest } from 'ember-qunit';
|
||||
module('Unit | Serializer | account', function(hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
test('it serializes account', function (assert) {
|
||||
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',
|
||||
password: 'Password',
|
||||
},
|
||||
auth_method_id: '1',
|
||||
version: 1
|
||||
});
|
||||
const snapshot = record._createSnapshot();
|
||||
@@ -25,14 +24,70 @@ module('Unit | Serializer | account', function(hooks) {
|
||||
assert.deepEqual(serializedRecord, {
|
||||
type: 'password',
|
||||
name: 'Name',
|
||||
auth_method_id: '1',
|
||||
description: 'Description',
|
||||
attributes: {
|
||||
login_name: 'Login Name',
|
||||
password: 'Password',
|
||||
login_name: 'Login Name'
|
||||
},
|
||||
auth_method_id: '1',
|
||||
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 serializes only password when `adapterOptions.serializeWithNewPassword` 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',
|
||||
description: 'Description',
|
||||
attributes: {
|
||||
login_name: 'Login Name'
|
||||
},
|
||||
auth_method_id: '1',
|
||||
version: 1
|
||||
});
|
||||
const snapshot = record._createSnapshot();
|
||||
snapshot.adapterOptions = {
|
||||
serializeWithNewPassword: true,
|
||||
password: 'Password'
|
||||
};
|
||||
const serializedRecord = serializer.serialize(snapshot);
|
||||
assert.deepEqual(serializedRecord, {
|
||||
password: 'Password',
|
||||
version: 1
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user