Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow permissions to be checked from Meteor publish functions. #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Meteor.ManagedUsers.availablePermissions = function() {

Testing for Permissions
------------------------
` Meteor.ManagedUsers.hasPermission(permissionName) ` accepts a string of the permission's name, and then returns a boolean if the current user has that permission.
` Meteor.ManagedUsers.hasPermission(permissionName [, userId]) ` accepts a string of the permission's name, and then returns a boolean if the user (defaulting to the current user) has that permission.


To Do
Expand Down
25 changes: 19 additions & 6 deletions managedUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ Meteor.ManagedUsers = {
return {};
},

user: function(userId) {
if (userId === undefined)
return Meteor.user();
else if (typeof userId === "string")
return Meteor.users.findOne(userId);
else
return null;
},

// Input Validation
isAdmin: function() {
return (Meteor.user() && (Meteor.user().username === "admin"));
isAdmin: function(userId) {
var user = Meteor.ManagedUsers.user(userId);
return (user && user.username === "admin");
},

checkUsername: function(username, userId) {
Expand All @@ -32,9 +42,12 @@ Meteor.ManagedUsers = {
}
},

hasPermission: function(permission) {
if(Meteor.user() && ((Meteor.user().username === "admin") || (Meteor.user().permissions && Meteor.user().permissions[permission] == true)))
return true;
hasPermission: function(permission, userId) {
var user = Meteor.ManagedUsers.user(userId);
return (user &&
(user.username === "admin" ||
(user.permissions && user.permissions[permission] == true))
);
}
}

Expand Down Expand Up @@ -146,4 +159,4 @@ if(Meteor.isClient) {
Handlebars.registerHelper('hasPermission', function(permission) {
return Meteor.ManagedUsers.hasPermission(permission);
});
}
}
4 changes: 3 additions & 1 deletion managedUsers_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ if(Meteor.isClient) {
logoutStep,
function(test, expect) {
test.isFalse(Meteor.ManagedUsers.isAdmin());
test.isFalse(Meteor.ManagedUsers.isAdmin(Meteor.userId()));

Accounts.createUser({username: "someone", password: "abc123", profile: {name: "Some One"}}, function(error) {
test.equal(error.reason, "Signups forbidden");
Expand All @@ -62,6 +63,7 @@ if(Meteor.isClient) {
test.equal(error, undefined);
test.equal(Meteor.user().username, "admin");
test.isTrue(Meteor.ManagedUsers.isAdmin());
test.isTrue(Meteor.ManagedUsers.isAdmin(Meteor.userId()));
}));
},

Expand Down Expand Up @@ -106,4 +108,4 @@ if(Meteor.isClient) {

logoutStep,
]);
}
}
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ Package.on_use(function(api) {
});

Package.on_test(function(api) {
api.use(['managedUsers', 'tinytest', 'test-helpers'], ['client', 'server']);
api.use(['managedUsers', 'tinytest', 'test-helpers', 'accounts-password'], ['client', 'server']);
api.add_files('managedUsers_tests.js', ['client', 'server']);
});