2014-08-23 21:59:36 +04:00
|
|
|
var Promise = require('bluebird'),
|
|
|
|
ghostBookshelf = require('./base'),
|
|
|
|
errors = require('../errors'),
|
2014-02-19 17:57:26 +04:00
|
|
|
|
2014-07-14 16:29:45 +04:00
|
|
|
Basetoken;
|
2013-11-24 18:29:36 +04:00
|
|
|
|
2014-07-14 16:29:45 +04:00
|
|
|
Basetoken = ghostBookshelf.Model.extend({
|
2013-11-24 18:29:36 +04:00
|
|
|
|
2014-07-14 16:29:45 +04:00
|
|
|
user: function () {
|
|
|
|
return this.belongsTo('User');
|
|
|
|
},
|
|
|
|
|
|
|
|
client: function () {
|
|
|
|
return this.belongsTo('Client');
|
|
|
|
},
|
2014-04-03 17:03:09 +04:00
|
|
|
|
|
|
|
// override for base function since we don't have
|
|
|
|
// a created_by field for sessions
|
|
|
|
creating: function (newObj, attr, options) {
|
|
|
|
/*jshint unused:false*/
|
|
|
|
},
|
|
|
|
|
|
|
|
// override for base function since we don't have
|
|
|
|
// a updated_by field for sessions
|
|
|
|
saving: function (newObj, attr, options) {
|
|
|
|
/*jshint unused:false*/
|
|
|
|
// Remove any properties which don't belong on the model
|
|
|
|
this.attributes = this.pick(this.permittedAttributes());
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
}
|
2013-11-24 18:29:36 +04:00
|
|
|
|
|
|
|
}, {
|
2014-07-14 16:29:45 +04:00
|
|
|
destroyAllExpired: function (options) {
|
2014-05-06 05:45:08 +04:00
|
|
|
options = this.filterOptions(options, 'destroyAll');
|
2014-07-14 16:29:45 +04:00
|
|
|
return ghostBookshelf.Collection.forge([], {model: this})
|
|
|
|
.query('where', 'expires', '<', Date.now())
|
2014-08-23 21:59:36 +04:00
|
|
|
.fetch(options)
|
2014-02-27 06:44:09 +04:00
|
|
|
.then(function (collection) {
|
2013-11-24 18:29:36 +04:00
|
|
|
collection.invokeThen('destroy', options);
|
|
|
|
});
|
2014-08-23 21:59:36 +04:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* ### destroyByUser
|
|
|
|
* @param {[type]} options has context and id. Context is the user doing the destroy, id is the user to destroy
|
|
|
|
*/
|
|
|
|
destroyByUser: function (options) {
|
|
|
|
var userId = options.id;
|
|
|
|
|
|
|
|
options = this.filterOptions(options, 'destroyByUser');
|
|
|
|
|
|
|
|
if (userId) {
|
|
|
|
return ghostBookshelf.Collection.forge([], {model: this})
|
|
|
|
.query('where', 'user_id', '=', userId)
|
|
|
|
.fetch(options)
|
|
|
|
.then(function (collection) {
|
|
|
|
collection.invokeThen('destroy', options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(new errors.NotFoundError('No user found'));
|
2014-09-01 22:02:46 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### destroyByToken
|
|
|
|
* @param {[type]} options has token where token is the token to destroy
|
|
|
|
*/
|
|
|
|
destroyByToken: function (options) {
|
|
|
|
var token = options.token;
|
|
|
|
|
|
|
|
options = this.filterOptions(options, 'destroyByUser');
|
2014-08-23 21:59:36 +04:00
|
|
|
|
2014-09-01 22:02:46 +04:00
|
|
|
if (token) {
|
|
|
|
return ghostBookshelf.Collection.forge([], {model: this})
|
|
|
|
.query('where', 'token', '=', token)
|
|
|
|
.fetch(options)
|
|
|
|
.then(function (collection) {
|
|
|
|
collection.invokeThen('destroy', options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(new errors.NotFoundError('Token not found'));
|
2014-09-10 08:06:24 +04:00
|
|
|
}
|
2013-11-24 18:29:36 +04:00
|
|
|
});
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
module.exports = Basetoken;
|