dispatch:run-as-user
Allow server and client to run as user in restricted mode.
API:
Meteor.runAsUser(userId, func)
Meteor.runAsRestrictedUser(userId, func)
Meteor.runRestricted(func)
Meteor.runUnrestricted(func)
Meteor.isRestricted()
Examples
1 Meteor.runAsUser(userId, function() { 2 // Run method as user on both client or server 3 Meteor.call('getUserId'); 4 });
Client cannot set other userId that it's current user
1 foo = new Mongo.Collection('foo-collection'); 2 3 foo.allow({ 4 insert: function(userId) { return !!userId; } 5 }); 6 7 Meteor.runAsUserRestricted(userId, function() { 8 // This will throw errors on both client and server if 9 // not allowed 10 foo.insert({ name: 'foo'}); 11 });
1 Meteor.runAsRestrictedUser(userId, function() { 2 if (Meteor.isRestricted()) { 3 // Something in restricted mode 4 console.log(Meteor.userId()); 5 } else { 6 // Safe mode 7 } 8 });
Restrictions API:
1 Meteor.runRestricted(function() { 2 // Meteor.isRestricted() === true 3 Meteor.runUnrestricted(function() { 4 // Meteor.isRestricted() === false 5 }); 6 });