diff --git a/client/views/admin/settings.html b/client/views/admin/settings.html index ed9a3bb65..83d4e5431 100644 --- a/client/views/admin/settings.html +++ b/client/views/admin/settings.html @@ -7,6 +7,12 @@

No settings yet.

{{/if}} {{#if currentUser.isAdmin}} +
+ +
+ +
+
diff --git a/client/views/admin/settings.js b/client/views/admin/settings.js index 9a291968b..1a1be63c0 100644 --- a/client/views/admin/settings.js +++ b/client/views/admin/settings.js @@ -2,6 +2,7 @@ Template.settings.events = { 'click input[type=submit]': function(e){ e.preventDefault(); if(!Meteor.user()) throw 'You must be logged in.'; + var requireInvite=!!$('#requireInvite').attr('checked'); var title= $('#title').val(); var theme = $('#theme').val(); var footerCode=$("#footerCode").val(); @@ -23,6 +24,7 @@ Template.settings.events = { if(prevSetting){ Settings.update(prevSetting._id,{ $set: { + requireInvite:requireInvite, title: title, theme: theme, footerCode: footerCode, @@ -46,6 +48,7 @@ Template.settings.events = { }); }else{ var settingId = Settings.insert({ + requireInvite:requireInvite, title: title, theme: theme, footerCode: footerCode, diff --git a/client/views/comments/comment_page.html b/client/views/comments/comment_page.html index 18f5eaf00..26da6865b 100644 --- a/client/views/comments/comment_page.html +++ b/client/views/comments/comment_page.html @@ -14,7 +14,7 @@ {{/with}} {{/if}} - {{#if currentUser.approved}} + {{#if canPostComment}} {{> comment_form}} {{/if}}
diff --git a/client/views/comments/comment_page.js b/client/views/comments/comment_page.js index aa5b575b9..967cb9f21 100644 --- a/client/views/comments/comment_page.js +++ b/client/views/comments/comment_page.js @@ -3,8 +3,13 @@ Template.comment_page.post = function(){ return selectedComment && Posts.findOne(selectedComment.post); }; -Template.comment_page.comment = function(){ - var comment = Comments.findOne(Session.get('selectedCommentId')); - Template.comment_page.repress_recursion = true; - return comment; -}; \ No newline at end of file +Template.comment_page.helpers({ + comment: function(){ + var comment = Comments.findOne(Session.get('selectedCommentId')); + Template.comment_page.repress_recursion = true; + return comment; + }, + canComment: function(){ + return canComment(Meteor.user()); + } +}); \ No newline at end of file diff --git a/client/views/nav.html b/client/views/nav.html index 39dd8604b..94f1185cf 100644 --- a/client/views/nav.html +++ b/client/views/nav.html @@ -50,7 +50,7 @@
  • Sign Up
  • Sign In
  • {{/if}} --> - {{#if currentUser.approved}} + {{#if canPost}}
  • Post
  • {{/if}} diff --git a/client/views/nav.js b/client/views/nav.js index 9f426ccbb..bf6ffb377 100644 --- a/client/views/nav.js +++ b/client/views/nav.js @@ -21,28 +21,25 @@ Template.nav.helpers({ site_title: function(){ return getSetting('title'); }, - logo_url: function(){ return getSetting('logoUrl'); }, - logo_height: function(){ return getSetting('logoHeight'); }, - logo_width: function(){ return getSetting('logoWidth'); }, - logo_top: function(){ return Math.floor((70-getSetting('logoHeight'))/2); }, - logo_offset: function(){ return -Math.floor(getSetting('logoWidth')/2); }, - intercom: function(){ return !!getSetting('intercomId'); - } + }, + canPost: function(){ + return canPost(Meteor.user()); + } }); \ No newline at end of file diff --git a/client/views/notifications.js b/client/views/notifications.js index 53f61bfa9..24615aa71 100644 --- a/client/views/notifications.js +++ b/client/views/notifications.js @@ -15,6 +15,7 @@ Template.notifications.helpers({ notification_class: function(){ var notifications=Notifications.find({userId: Meteor.user()._id, read: false}).fetch(); if(notifications.length==0) + return 'no-notifications'; } }); diff --git a/client/views/posts/post_page.html b/client/views/posts/post_page.html index 279efe0e0..36719c605 100644 --- a/client/views/posts/post_page.html +++ b/client/views/posts/post_page.html @@ -3,7 +3,7 @@ {{#with post}} {{> post_item}} {{/with}} - {{#if currentUser.approved}} + {{#if canComment}} {{> comment_form}} {{/if}} {{> comment_list}} diff --git a/client/views/posts/post_page.js b/client/views/posts/post_page.js index 26bd06306..2a6d80ec7 100644 --- a/client/views/posts/post_page.js +++ b/client/views/posts/post_page.js @@ -7,6 +7,9 @@ Template.post_page.helpers({ var converter = new Markdown.Converter(); var html_body=converter.makeHtml(this.body); return html_body.autoLink(); + }, + canComment: function(){ + return canComment(Meteor.user()); } }); diff --git a/client/views/users/user_item.html b/client/views/users/user_item.html index 114892850..b02b7fbf1 100644 --- a/client/views/users/user_item.html +++ b/client/views/users/user_item.html @@ -14,6 +14,7 @@ {{comments_count}} {{karma}} - {{#if isAdmin}}{{/if}} + {{#if isInvited}}{{else}}Invite{{/if}} + {{#if isAdmin}}{{else}}Make admin{{/if}} \ No newline at end of file diff --git a/client/views/users/user_item.js b/client/views/users/user_item.js index c42fa673a..a07a9d20c 100644 --- a/client/views/users/user_item.js +++ b/client/views/users/user_item.js @@ -27,4 +27,39 @@ Template.user_item.helpers({ // Posts.find({'user_id':this._id}).forEach(function(post){console.log(post.headline);}); return Comments.find({'userId':this._id}).count(); } -}); \ No newline at end of file +}); + +Template.user_item.events({ + 'click .invite-link': function(e, instance){ + e.preventDefault(); + Meteor.users.update(instance.data._id,{ + $set:{ + isInvited: true + } + }); + }, + 'click .uninvite-link': function(e, instance){ + e.preventDefault(); + Meteor.users.update(instance.data._id,{ + $set:{ + isInvited: false + } + }); + }, + 'click .admin-link': function(e, instance){ + e.preventDefault(); + Meteor.users.update(instance.data._id,{ + $set:{ + isAdmin: true + } + }); + }, + 'click .unadmin-link': function(e, instance){ + e.preventDefault(); + Meteor.users.update(instance.data._id,{ + $set:{ + isAdmin: false + } + }); + } +}) \ No newline at end of file diff --git a/client/views/users/users.html b/client/views/users/users.html index 7f7801b58..4ec045f08 100644 --- a/client/views/users/users.html +++ b/client/views/users/users.html @@ -11,6 +11,7 @@ Posts Comments Karma + Is Invited? Is Admin? diff --git a/lib/comment.js b/lib/comment.js index 33ef035e3..62235c644 100644 --- a/lib/comment.js +++ b/lib/comment.js @@ -2,9 +2,9 @@ Meteor.methods({ comment: function(postId, parentCommentId, text){ var user = Meteor.user(); - if (!user || !user.approved) - throw new Meteor.Error('You need to login and be approved to post new comments.') - + if (!user || !canPost(user)) + throw new Meteor.Error('You need to login or be invited to post new comments.') + var comment = { post: postId , body: text diff --git a/lib/post.js b/lib/post.js index 2cb0e8320..58f6321d9 100644 --- a/lib/post.js +++ b/lib/post.js @@ -1,9 +1,9 @@ Meteor.methods({ post: function(post){ var user = Meteor.user(); - if (!user || !user.approved) - throw new Meteor.Error('You need to login and be approved to post new stories.') - + if (!user || !canPost(user)) + throw new Meteor.Error('You need to login or be invited to post new stories.') + post = _.extend(post, { userId: user._id, author: user.username, diff --git a/lib/users.js b/lib/users.js index 69c0746dd..626e62729 100644 --- a/lib/users.js +++ b/lib/users.js @@ -1,3 +1,10 @@ +isAdminById=function(userId){ + var user = Meteor.users.findOne(userId); + return user && isAdmin(user); +} +isAdmin=function(user){ + return user.isAdmin; +} getDisplayNameById = function(userId){ getDisplayName(Meteor.users.findOne(userId)); } @@ -36,3 +43,29 @@ getCurrentUserEmail = function(){ userProfileComplete = function(user) { return !!getEmail(user); } + +// Permissions + +canView = function(user){ + +} +canPost = function(user){ + if(typeof user=='undefined') + return false + if(isAdmin(user)) + return true; + if(getSetting('requireInvite')==true){ + return user.isInvited; + } + return true; +} +canComment = function(user){ + if(typeof user=='undefined') + return false; + if(isAdmin(user)) + return true; + if(getSetting('requireInvite')==true){ + return user.isInvited; + } + return true; +} \ No newline at end of file diff --git a/server/publish.js b/server/publish.js index 2baa0069e..dca16c138 100644 --- a/server/publish.js +++ b/server/publish.js @@ -1,11 +1,5 @@ -// Users -isAdmin=function(userId){ - var user = Meteor.users.findOne(userId); - return user && user.isAdmin; -} - Meteor.publish('users', function() { - if (this.userId() && isAdmin(this.userId())) { + if (this.userId() && isAdminById(this.userId())) { return Meteor.users.find(); }else{ return Meteor.users.find({}, {fields: {emails: false}}); @@ -24,13 +18,13 @@ Meteor.startup(function(){ // console.log(docs); // console.log('fields: '+fields); // console.log(modifier); //uncommenting this crashes everything - if(isAdmin(userId) || (docs[0]._id && docs[0]._id==userId)){ + if(isAdminById(userId) || (docs[0]._id && docs[0]._id==userId)){ return true; } return false; } , remove: function(userId, docs){ - if(isAdmin(userId) || (docs[0]._id && docs[0]._id==userId)){ + if(isAdminById(userId) || (docs[0]._id && docs[0]._id==userId)){ return true; } return false; @@ -61,13 +55,13 @@ Meteor.startup(function(){ // console.log(userId); // console.log(docs); // console.log('fields: '+fields); - if(isAdmin(userId) || (docs[0].user_id && docs[0].user_id==userId)){ + if(isAdminById(userId) || (docs[0].user_id && docs[0].user_id==userId)){ return true; } return false; } , remove: function(userId, docs){ - if(isAdmin(userId) || (docs[0].user_id && docs[0].user_id==userId)){ + if(isAdminById(userId) || (docs[0].user_id && docs[0].user_id==userId)){ return true; } return false; } @@ -91,13 +85,13 @@ Meteor.startup(function(){ return false; } , update: function(userId, docs, fields, modifier){ - if(isAdmin(userId) || (docs[0].user_id && docs[0].user_id==userId)){ + if(isAdminById(userId) || (docs[0].user_id && docs[0].user_id==userId)){ return true; } return false; } , remove: function(userId, docs){ - if(isAdmin(userId) || (docs[0].user_id && docs[0].user_id==userId)){ + if(isAdminById(userId) || (docs[0].user_id && docs[0].user_id==userId)){ return true; } return false; @@ -115,9 +109,9 @@ Meteor.publish('settings', function() { Meteor.startup(function(){ Settings.allow({ - insert: function(userId, docs){ return isAdmin(userId); } - , update: function(userId, docs, fields, modifier){ return isAdmin(userId); } - , remove: function(userId, docs){ return isAdmin(userId); } + insert: function(userId, docs){ return isAdminById(userId); } + , update: function(userId, docs, fields, modifier){ return isAdminById(userId); } + , remove: function(userId, docs){ return isAdminById(userId); } }); }); @@ -139,13 +133,13 @@ Meteor.startup(function(){ return false; } , update: function(userId, docs, fields, modifier){ - if(isAdmin(userId) || (docs[0].user_id && docs[0].user_id==userId)){ + if(isAdminById(userId) || (docs[0].user_id && docs[0].user_id==userId)){ return true; } return false; } , remove: function(userId, docs){ - if(isAdmin(userId) || (docs[0].user_id && docs[0].user_id==userId)){ + if(isAdminById(userId) || (docs[0].user_id && docs[0].user_id==userId)){ return true; } return false; diff --git a/server/users.js b/server/users.js index 81a30b2e8..1cca51ce4 100644 --- a/server/users.js +++ b/server/users.js @@ -5,7 +5,7 @@ Accounts.onCreateUser(function(options, extra, user){ user.profile = user.profile || {}; // users start pending, need to be approved - user.approved = false + user.isInvited = false if (options.email) user.profile.email = options.email;