wolas:restrict-mixin

v0.0.5Published 2 years ago

RestrictMixin

A mixin for mdg:validated-method to throw errors on specified conditions before the run function.

Install

meteor add ziarno:restrict-mixin

Usage

As a mixin:

1SomeCollection.methods.someMethod = new ValidateMethod({
2    name,
3    mixins: [RestrictMixin],
4    restrictions: [
5        {
6          condition: function (args) {
7            return !this.userId;
8          },
9          error: function (args) {
10            return new Meteor.Error(this.name + '.unauthorized', 'You must be logged in');
11          }
12        }
13    ],
14    validate,
15    run: function (args) {}
16});

You can also create mixins:

1var isLoggedInMixin = RestrictMixin.createMixin({
2    condition: function (args) {
3        return !this.userId;
4    },
5    error: function (args) {
6        return new Meteor.Error(this.name + '.unauthorized', 'You must be logged in');
7    }
8});
9
10SomeCollection.methods.someMethod = new ValidateMethod({
11    name,
12    mixins: [isLoggedInMixin],
13    validate,
14    run: function (args) {
15      //will not run if !this.userId
16    }
17});

Notes:

  • restrictions can be an array or object (also as value passed to createMethod)
  • condition and error can be functions or objects
  • condition and error functions are called in the correct context (this is the same as in the run method) and get passed the same arguments (which is args passed to the method)

Environment

If you want to run a restriction only in the simulation or only on the server, use the env field:

1SomeCollection.methods.someMethod = new ValidateMethod({
2    name,
3    mixins: [RestrictMixin],
4    restrictions: [
5        {
6          condition: function (args) {
7            return !Meteor.users.find(args.someUserId);
8          },
9          error: function (args) {
10            return new Meteor.Error(this.name + '.notFound', 'User not found');
11          },
12          env: 'server-only' //this restricion will *not* run if this.isSimulation
13        }
14    ],
15    validate,
16    run: function (args) {}
17});
  • Possible env values:
    • 'server-only'
    • 'simulation-only'
  • Runs on both environments if env is not specified

Extra: usage with ProvideMixin

There might be situations where you want to find an object in the DB and use it for error triggering - but you shouldn't make a DB query for each error. That's where ProvideMixin comes in handy

  • it adds additional arguments to the condition(), error(), and run() functions.
1SomeCollection.methods.someMethod = new ValidateMethod({
2    name,
3    mixins: [RestrictMixin, ProvideMixin],
4    provide: function (args) {
5      return SomeOtherCollection.findOne(args.someOtherCollectionId);
6    },
7    restrictions: [
8      {
9        condition: function (args, someOtherCollectionItem) {
10          return someOtherCollectionItem.owner !== this.userId;
11        },
12        error: function (args, someOtherCollectionItem) {
13          return new Meteor.Error(this.name + '.unauthorized', 'Only owners can do stuff');
14        },
15      }
16    ],
17    validate,
18    run: function (args, someOtherCollectionItem) {
19      //...
20    }
21});