0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Added members related db changes and models

refs #10213
This commit is contained in:
Fabien O'Carroll 2018-12-10 16:20:54 +07:00
parent 38c631eaf7
commit 0bf5dd212a
8 changed files with 112 additions and 6 deletions

View file

@ -0,0 +1,35 @@
const common = require('../../../../lib/common');
const commands = require('../../../schema').commands;
const table = 'members';
const message1 = 'Adding table: ' + table;
const message2 = 'Dropping table: ' + table;
module.exports.up = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (exists) {
common.logging.warn(message1);
return;
}
common.logging.info(message1);
return commands.createTable(table, connection);
});
};
module.exports.down = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
common.logging.warn(message2);
return;
}
common.logging.info(message2);
return commands.deleteTable(table, connection);
});
};

View file

@ -118,5 +118,16 @@
"public_hash": { "public_hash": {
"defaultValue": null "defaultValue": null
} }
},
"members": {
"members_public_key": {
"defaultValue": null
},
"members_private_key": {
"defaultValue": null
},
"members_session_secret": {
"defaultValue": null
}
} }
} }

View file

@ -142,7 +142,7 @@ module.exports = {
maxlength: 50, maxlength: 50,
nullable: false, nullable: false,
defaultTo: 'core', defaultTo: 'core',
validations: {isIn: [['core', 'blog', 'theme', 'app', 'plugin', 'private']]} validations: {isIn: [['core', 'blog', 'theme', 'app', 'plugin', 'private', 'members']]}
}, },
created_at: {type: 'dateTime', nullable: false}, created_at: {type: 'dateTime', nullable: false},
created_by: {type: 'string', maxlength: 24, nullable: false}, created_by: {type: 'string', maxlength: 24, nullable: false},
@ -368,5 +368,15 @@ module.exports = {
mobiledoc: {type: 'text', maxlength: 1000000000, fieldtype: 'long', nullable: true}, mobiledoc: {type: 'text', maxlength: 1000000000, fieldtype: 'long', nullable: true},
created_at_ts: {type: 'bigInteger', nullable: false}, created_at_ts: {type: 'bigInteger', nullable: false},
created_at: {type: 'dateTime', nullable: false} created_at: {type: 'dateTime', nullable: false}
},
members: {
id: {type: 'string', maxlength: 24, nullable: false, primary: true},
email: {type: 'string', maxlength: 191, nullable: false, unique: true, validations: {isEmail: true}},
name: {type: 'string', maxlength: 191, nullable: false},
password: {type: 'string', maxlength: 60, nullable: true},
created_at: {type: 'dateTime', nullable: false},
created_by: {type: 'string', maxlength: 24, nullable: false},
updated_at: {type: 'dateTime', nullable: true},
updated_by: {type: 'string', maxlength: 24, nullable: true}
} }
}; };

View file

@ -34,7 +34,8 @@ models = [
'webhook', 'webhook',
'integration', 'integration',
'api-key', 'api-key',
'mobiledoc-revision' 'mobiledoc-revision',
'member'
]; ];
function init() { function init() {

View file

@ -0,0 +1,40 @@
const ghostBookshelf = require('./base');
const security = require('../lib/security');
const Member = ghostBookshelf.Model.extend({
tableName: 'members',
onSaving() {
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
if (this.hasChanged('password')) {
return security.password.hash(String(this.get('password')))
.then((hash) => {
this.set('password', hash);
});
}
},
comparePassword(rawPassword) {
return security.password.compare(rawPassword, this.get('password'));
},
toJSON(unfilteredOptions) {
var options = Member.filterOptions(unfilteredOptions, 'toJSON'),
attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options);
// remove password hash and tokens for security reasons
delete attrs.password;
return attrs;
}
});
const Members = ghostBookshelf.Collection.extend({
model: Member
});
module.exports = {
Member: ghostBookshelf.model('Member', Member),
Members: ghostBookshelf.collection('Members', Members)
};

View file

@ -2,6 +2,7 @@ const Promise = require('bluebird'),
_ = require('lodash'), _ = require('lodash'),
uuid = require('uuid'), uuid = require('uuid'),
crypto = require('crypto'), crypto = require('crypto'),
keypair = require('keypair'),
ghostBookshelf = require('./base'), ghostBookshelf = require('./base'),
common = require('../lib/common'), common = require('../lib/common'),
validation = require('../data/validation'), validation = require('../data/validation'),
@ -18,9 +19,17 @@ function parseDefaultSettings() {
dynamicDefault = { dynamicDefault = {
db_hash: uuid.v4(), db_hash: uuid.v4(),
public_hash: crypto.randomBytes(15).toString('hex'), public_hash: crypto.randomBytes(15).toString('hex'),
session_secret: crypto.randomBytes(32).toString('hex') session_secret: crypto.randomBytes(32).toString('hex'),
members_session_secret: crypto.randomBytes(32).toString('hex')
}; };
const membersKeypair = keypair({
bits: 1024
});
dynamicDefault.members_public_key = membersKeypair.public;
dynamicDefault.members_private_key = membersKeypair.private;
_.each(defaultSettingsInCategories, function each(settings, categoryName) { _.each(defaultSettingsInCategories, function each(settings, categoryName) {
_.each(settings, function each(setting, settingName) { _.each(settings, function each(setting, settingName) {
setting.type = categoryName; setting.type = categoryName;

View file

@ -87,7 +87,7 @@ describe('DB API', function () {
var jsonResponse = res.body; var jsonResponse = res.body;
should.exist(jsonResponse.db); should.exist(jsonResponse.db);
jsonResponse.db.should.have.length(1); jsonResponse.db.should.have.length(1);
Object.keys(jsonResponse.db[0].data).length.should.eql(24); Object.keys(jsonResponse.db[0].data).length.should.eql(25);
done(); done();
}); });
}); });
@ -105,7 +105,7 @@ describe('DB API', function () {
const jsonResponse = res.body; const jsonResponse = res.body;
should.exist(jsonResponse.db); should.exist(jsonResponse.db);
jsonResponse.db.should.have.length(1); jsonResponse.db.should.have.length(1);
Object.keys(jsonResponse.db[0].data).length.should.eql(26); Object.keys(jsonResponse.db[0].data).length.should.eql(27);
done(); done();
}); });
}); });

View file

@ -19,7 +19,7 @@ var should = require('should'),
*/ */
describe('DB version integrity', function () { describe('DB version integrity', function () {
// Only these variables should need updating // Only these variables should need updating
const currentSchemaHash = '92cb4391c426520d2e3e80c46f6ae100'; const currentSchemaHash = 'b865478398cd2b0a1e5eaffebccdb88c';
const currentFixturesHash = '8b36d1e72c29b7f9073612142b5a8783'; const currentFixturesHash = '8b36d1e72c29b7f9073612142b5a8783';
// If this test is failing, then it is likely a change has been made that requires a DB version bump, // If this test is failing, then it is likely a change has been made that requires a DB version bump,