2014-08-23 21:59:36 +04:00
|
|
|
var Promise = require('bluebird'),
|
2015-06-14 18:58:49 +03:00
|
|
|
ghostBookshelf = require('./index'),
|
|
|
|
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
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
user: function user() {
|
2014-07-14 16:29:45 +04:00
|
|
|
return this.belongsTo('User');
|
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
client: function client() {
|
2014-07-14 16:29:45 +04:00
|
|
|
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
|
2015-06-14 18:58:49 +03:00
|
|
|
creating: function creating(newObj, attr, options) {
|
2014-04-03 17:03:09 +04:00
|
|
|
/*jshint unused:false*/
|
|
|
|
},
|
|
|
|
|
|
|
|
// override for base function since we don't have
|
|
|
|
// a updated_by field for sessions
|
2015-06-14 18:58:49 +03:00
|
|
|
saving: function saving(newObj, attr, options) {
|
2014-04-03 17:03:09 +04:00
|
|
|
/*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
|
|
|
|
|
|
|
}, {
|
2015-06-14 18:58:49 +03:00
|
|
|
destroyAllExpired: function destroyAllExpired(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)
|
2015-06-14 18:58:49 +03:00
|
|
|
.then(function then(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
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
destroyByUser: function destroyByUser(options) {
|
2014-08-23 21:59:36 +04:00
|
|
|
var userId = options.id;
|
|
|
|
|
|
|
|
options = this.filterOptions(options, 'destroyByUser');
|
|
|
|
|
|
|
|
if (userId) {
|
|
|
|
return ghostBookshelf.Collection.forge([], {model: this})
|
|
|
|
.query('where', 'user_id', '=', userId)
|
|
|
|
.fetch(options)
|
2015-06-14 18:58:49 +03:00
|
|
|
.then(function then(collection) {
|
2014-08-23 21:59:36 +04:00
|
|
|
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
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
destroyByToken: function destroyByToken(options) {
|
2014-09-01 22:02:46 +04:00
|
|
|
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)
|
2015-06-14 18:58:49 +03:00
|
|
|
.then(function then(collection) {
|
2014-09-01 22:02:46 +04:00
|
|
|
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;
|