includePlainOnCreate
serializer option and use it for serverExpirationDate
This commit is contained in:
parent
095ba3a2eb
commit
0ad6026715
3 changed files with 43 additions and 19 deletions
|
@ -145,10 +145,6 @@ export default Ember.Controller.extend({
|
|||
}
|
||||
}.observes('model.isDateTime', 'model.isFindADate'),
|
||||
|
||||
syncExpirationDate: function() {
|
||||
this.set('model.serverExpirationDate', this.get('model.expirationDate'));
|
||||
}.observes('model.expirationDate'),
|
||||
|
||||
/*
|
||||
* validate if a given time string is in valid format
|
||||
*/
|
||||
|
|
|
@ -26,9 +26,10 @@ export default DS.Model.extend({
|
|||
defaultValue: ''
|
||||
}),
|
||||
|
||||
// encrypted date when poll expires
|
||||
// must be same as the unencrypted serverExipirationDate
|
||||
expirationDate : DS.attr('string'),
|
||||
// ISO 8601 date + time string in UTC
|
||||
expirationDate : DS.attr('string', {
|
||||
includePlainOnCreate: 'serverExpirationDate'
|
||||
}),
|
||||
|
||||
// Must all options been answered?
|
||||
forceAnswer : DS.attr('boolean'),
|
||||
|
@ -42,12 +43,6 @@ export default DS.Model.extend({
|
|||
// FindADate or MakeAPoll
|
||||
pollType : DS.attr('string'),
|
||||
|
||||
// unencrypted expiration date
|
||||
// is set by client on create but never retrieved back from server
|
||||
serverExpirationDate : DS.attr('string', {
|
||||
encrypted: false
|
||||
}),
|
||||
|
||||
// timezone poll got created in (like "Europe/Berlin")
|
||||
timezone : DS.attr('string'),
|
||||
|
||||
|
|
|
@ -1,12 +1,31 @@
|
|||
import DS from "ember-data";
|
||||
import Ember from "ember";
|
||||
/* global sjcl */
|
||||
|
||||
/*
|
||||
* extends DS.RESTSerializer to implement encryption
|
||||
*
|
||||
* By default every attribute hash is encrypted using SJCL.
|
||||
* This is configurable by options parameter of DS.attr().
|
||||
*
|
||||
* Options:
|
||||
* - encrypted (boolean)
|
||||
* If false the attribute won't be encrypted.
|
||||
* - includePlainOnCreate (string)
|
||||
* If set the attribute will be included plain (not encrypted) when
|
||||
* recorde is created. Value is the attributes name used.
|
||||
*/
|
||||
export default DS.RESTSerializer.extend({
|
||||
/*
|
||||
* implement decryption
|
||||
*/
|
||||
normalize: function(modelClass, resourceHash, prop) {
|
||||
// decrypt before unserialize
|
||||
// work-a-round: get encryption key from dummy record
|
||||
var dummyRecord = this.store.createRecord('poll');
|
||||
var decryptionKey = dummyRecord.get('encryption.key');
|
||||
dummyRecord.destroyRecord();
|
||||
|
||||
// run before serialization of attribute hash
|
||||
modelClass.eachAttribute(function(key, attributes) {
|
||||
if (
|
||||
attributes.options.encrypted !== false
|
||||
|
@ -28,14 +47,28 @@ export default DS.RESTSerializer.extend({
|
|||
|
||||
return this._super(modelClass, resourceHash, prop);
|
||||
},
|
||||
|
||||
serializeAttribute: function(snapshot, json, key, attributes) {
|
||||
this._super(snapshot, json, key, attributes);
|
||||
|
||||
// encrypt after serialize
|
||||
/*
|
||||
* implement encryption
|
||||
*/
|
||||
serializeAttribute: function(snapshot, json, key, attribute) {
|
||||
this._super(snapshot, json, key, attribute);
|
||||
|
||||
// get encryption key from snapshot which is model representation
|
||||
var encryptionKey = snapshot.record.get('encryption.key');
|
||||
|
||||
// map includePlainOnCreate after serialization of attribute hash
|
||||
// but before encryption so we can just use the serialized hash
|
||||
if (
|
||||
attributes.options.encrypted !== false
|
||||
!Ember.isEmpty(attribute.options.includePlainOnCreate) &&
|
||||
typeof attribute.options.includePlainOnCreate === 'string'
|
||||
) {
|
||||
json[attribute.options.includePlainOnCreate] = json[key];
|
||||
}
|
||||
|
||||
// encrypt after serialization of attribute hash
|
||||
if (
|
||||
attribute.options.encrypted !== false
|
||||
) {
|
||||
try {
|
||||
json[key] = sjcl.encrypt(encryptionKey, JSON.stringify(json[key]));
|
||||
|
|
Loading…
Reference in a new issue