Skip to content

Commit 1654573

Browse files
authored
Disallowing the use of API Model.create and Model.remove (#12)
These two methods can't be protected by this plugin, so it's best to disallow them entirely. By doing so, it makes it safe to wrap your mongoose models with an Restify wrapper. Also, this diff introduces Ava and the new testing engine. Very similar syntax as before, just a newer, modern, maintained library.
1 parent 982ddac commit 1654573

7 files changed

Lines changed: 3003 additions & 731 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 2.0.0
2+
3+
- Removing the ability to call Model.remove() and Model.create() since those aren't compatible with how this library works.
4+
-
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const test = require('ava');
2+
const mongoose = require('mongoose');
3+
const authz = require('../');
4+
const IncompatibleMethodError = require('../lib/IncompatibleMethodError');
5+
6+
test.before((t) => {
7+
const schema = new mongoose.Schema({ friend: String });
8+
schema.plugin(authz);
9+
t.context.MyModel = mongoose.model('MyModel', schema);
10+
});
11+
12+
test('Model.create should not be callable', (t) => {
13+
const { MyModel } = t.context;
14+
t.throws(
15+
() => MyModel.create({ friend: 'bar' }),
16+
IncompatibleMethodError,
17+
);
18+
});
19+
20+
test('Model.remove should not be callable', (t) => {
21+
const { MyModel } = t.context;
22+
t.throws(
23+
() => MyModel.remove({}),
24+
IncompatibleMethodError,
25+
);
26+
});
27+

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
} = require('./lib/helpers');
1010

1111
const PermissionDeniedError = require('./lib/PermissionDeniedError');
12+
const IncompatibleMethodError = require('./lib/IncompatibleMethodError');
1213

1314
module.exports = (schema) => {
1415
async function save(doc, options) {
@@ -143,4 +144,12 @@ module.exports = (schema) => {
143144
const authLevels = await resolveAuthLevel(schema, options, {});
144145
return hasPermission(this.schema, authLevels, 'create');
145146
};
147+
148+
schema.statics.create = function cannotCreate() {
149+
throw new IncompatibleMethodError('Model.create');
150+
};
151+
152+
schema.statics.remove = function cannotRemove() {
153+
throw new IncompatibleMethodError('Model.remove');
154+
};
146155
};

lib/IncompatibleMethodError.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = class IncompatibleMethodError extends Error {
2+
constructor(method) {
3+
const message = `[${method}] is not compatable with mongoose-authz. ` +
4+
`Please see https://www.npmjs.com/package/mongoose-authz#${method} for more details.`;
5+
6+
super(message);
7+
this.name = 'IncompatibleMethod';
8+
}
9+
};

0 commit comments

Comments
 (0)