-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6080 from ErisDS/model-access-rules
Add access rules bookshelf plugin
- Loading branch information
Showing
6 changed files
with
114 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// # Access Rules | ||
// | ||
// Extends Bookshelf.Model.force to take a 'context' option which provides information on how this query should | ||
// be treated in terms of data access rules - currently just detecting public requests | ||
module.exports = function (Bookshelf) { | ||
var model = Bookshelf.Model, | ||
Model; | ||
|
||
Model = Bookshelf.Model.extend({ | ||
/** | ||
* Cached copy of the context setup for this model instance | ||
*/ | ||
_context: null, | ||
/** | ||
* ## Is Public Context? | ||
* A helper to determine if this is a public request or not | ||
* @returns {boolean} | ||
*/ | ||
isPublicContext: function isPublicContext() { | ||
return !!(this._context && this._context.public); | ||
} | ||
}, | ||
{ | ||
/** | ||
* ## Forge | ||
* Ensure that context gets set as part of the forge | ||
* | ||
* @param {object} attributes | ||
* @param {object} options | ||
* @returns {Bookshelf.Model} model | ||
*/ | ||
forge: function forge(attributes, options) { | ||
var self = model.forge.apply(this, arguments); | ||
|
||
if (options && options.context) { | ||
self._context = options.context; | ||
delete options.context; | ||
} | ||
|
||
return self; | ||
} | ||
}); | ||
|
||
Bookshelf.Model = Model; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
module.exports = { | ||
accessRules: require('./access-rules'), | ||
includeCount: require('./include-count'), | ||
pagination: require('./pagination') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*globals describe, it, beforeEach, afterEach */ | ||
/*jshint expr:true*/ | ||
var should = require('should'), | ||
sinon = require('sinon'), | ||
|
||
// Thing we're testing | ||
// accessRules = require('../../../server/models/plugins/access-rules'), | ||
models = require('../../../server/models'), | ||
ghostBookshelf, | ||
|
||
sandbox = sinon.sandbox.create(); | ||
|
||
// To stop jshint complaining | ||
should.equal(true, true); | ||
|
||
describe('Access Rules', function () { | ||
beforeEach(function () { | ||
return models.init().then(function () { | ||
ghostBookshelf = models.Base; | ||
}); | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('Base Model', function () { | ||
it('should assign isPublicContext to prototype', function () { | ||
ghostBookshelf.Model.prototype.isPublicContext.should.be.a.Function; | ||
}); | ||
|
||
it('should get called when a model is forged', function () { | ||
ghostBookshelf.Model.forge(null, {context: 'test'})._context.should.eql('test'); | ||
}); | ||
|
||
describe('isPublicContext', function () { | ||
it('should isPublicContext false if no context is set', function () { | ||
ghostBookshelf.Model.forge().isPublicContext().should.be.false; | ||
}); | ||
|
||
it('should return false if context has no `public` property', function () { | ||
ghostBookshelf.Model.forge(null, {context: 'test'}).isPublicContext().should.be.false; | ||
}); | ||
|
||
it('should return false if context.public is false', function () { | ||
ghostBookshelf.Model.forge(null, {context: {public: false}}).isPublicContext().should.be.false; | ||
}); | ||
|
||
it('should return true if context.public is true', function () { | ||
ghostBookshelf.Model.forge(null, {context: {public: true}}).isPublicContext().should.be.true; | ||
}); | ||
}); | ||
}); | ||
}); |