2018-09-17 21:49:30 +03:00
|
|
|
var crypto = require('crypto'),
|
|
|
|
uuid = require('uuid'),
|
2016-10-11 15:53:52 +03:00
|
|
|
ghostBookshelf = require('./base'),
|
2018-09-17 21:49:30 +03:00
|
|
|
config = require('../config'),
|
2014-06-30 16:58:10 +04:00
|
|
|
Client,
|
|
|
|
Clients;
|
|
|
|
|
|
|
|
Client = ghostBookshelf.Model.extend({
|
2016-02-14 21:44:34 +03:00
|
|
|
|
2015-08-18 16:05:25 +03:00
|
|
|
tableName: 'clients',
|
2016-02-14 21:44:34 +03:00
|
|
|
|
|
|
|
defaults: function defaults() {
|
2017-02-03 21:25:39 +03:00
|
|
|
// @TODO: we cannot delete this ugly check here, because ALL routing tests rely on a static client secret
|
2016-10-11 15:53:52 +03:00
|
|
|
var env = config.get('env'),
|
2016-02-14 21:44:34 +03:00
|
|
|
secret = env.indexOf('testing') !== 0 ? crypto.randomBytes(6).toString('hex') : 'not_available';
|
|
|
|
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
|
|
|
secret: secret,
|
|
|
|
status: 'development',
|
|
|
|
type: 'ua'
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-08-18 16:05:25 +03:00
|
|
|
trustedDomains: function trustedDomains() {
|
|
|
|
return this.hasMany('ClientTrustedDomain', 'client_id');
|
|
|
|
}
|
2015-10-22 16:28:47 +03:00
|
|
|
}, {
|
|
|
|
/**
|
|
|
|
* Returns an array of keys permitted in a method's `options` hash, depending on the current method.
|
|
|
|
* @param {String} methodName The name of the method to check valid options for.
|
|
|
|
* @return {Array} Keys allowed in the `options` hash of the model's method.
|
|
|
|
*/
|
|
|
|
permittedOptions: function permittedOptions(methodName) {
|
2018-09-20 17:23:39 +03:00
|
|
|
var options = ghostBookshelf.Model.permittedOptions.call(this, methodName),
|
2015-10-22 16:28:47 +03:00
|
|
|
|
|
|
|
// whitelists for the `options` hash argument on methods, by method name.
|
|
|
|
// these are the only options that can be passed to Bookshelf / Knex.
|
|
|
|
validOptions = {
|
2016-05-19 14:49:22 +03:00
|
|
|
findOne: ['columns', 'withRelated']
|
2015-10-22 16:28:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if (validOptions[methodName]) {
|
|
|
|
options = options.concat(validOptions[methodName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
2014-06-30 16:58:10 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
Clients = ghostBookshelf.Collection.extend({
|
|
|
|
model: Client
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
2014-07-13 15:17:18 +04:00
|
|
|
Client: ghostBookshelf.model('Client', Client),
|
|
|
|
Clients: ghostBookshelf.collection('Clients', Clients)
|
2014-09-10 08:06:24 +04:00
|
|
|
};
|