Vulcan/server/publish.js

118 lines
2.7 KiB
JavaScript
Raw Normal View History

2012-09-06 15:28:58 +09:00
// Users
2012-09-27 23:18:02 -07:00
isAdmin=function(userId){
var user = Meteor.users.findOne(userId);
return user && user.isAdmin;
}
2012-09-06 15:28:58 +09:00
Meteor.publish('users', function() {
if (this.userId() && isAdmin(this.userId())) {
return Meteor.users.find();
}else{
return Meteor.users.find({}, {fields: {emails: false}});
}
2012-09-06 15:28:58 +09:00
});
2012-09-19 09:03:25 +09:00
Meteor.startup(function(){
Meteor.users.allow({
insert: function(userId, doc){
//TODO
return true;
}
, update: function(userId, docs, fields, modifier){
2012-09-24 14:51:21 +09:00
// console.log("updating");
// console.log(userId);
// console.log(docs);
// console.log(fields);
// console.log(modifier);
if(isAdmin(userId) || (docs[0]._id && docs[0]._id==userId)){
2012-09-24 11:31:45 +09:00
return true;
}
return false;
2012-09-19 09:03:25 +09:00
}
, remove: function(userId, docs){
2012-09-24 14:51:21 +09:00
if(isAdmin(userId) || (docs[0]._id && docs[0]._id==userId)){
2012-09-24 11:31:45 +09:00
return true;
}
2012-09-19 09:03:25 +09:00
return false;
}
});
});
2012-08-22 23:24:33 -04:00
// Posts
2012-08-22 21:27:22 -04:00
Posts = new Meteor.Collection('posts');
Meteor.publish('posts', function() {
2012-09-11 15:32:25 +10:00
return Posts.find({}, {sort: {score: -1}});
2012-08-22 21:27:22 -04:00
});
2012-08-22 23:24:33 -04:00
2012-09-11 18:52:01 +10:00
// FIXME -- check all docs, not just the first one.
2012-08-22 23:24:33 -04:00
Meteor.startup(function(){
Posts.allow({
2012-09-06 15:28:58 +09:00
insert: function(userId, doc){
if(userId){
2012-09-11 18:52:01 +10:00
doc.userId = userId;
2012-09-06 15:28:58 +09:00
return true;
}
return false;
2012-09-06 11:34:05 +09:00
}
, update: function(userId, docs, fields, modifier){
if(isAdmin(userId) || (docs[0].user_id && docs[0].user_id==userId)){
2012-09-06 11:09:24 +09:00
return true;
}
return false;
}
2012-09-06 11:34:05 +09:00
, remove: function(userId, docs){
if(isAdmin(userId) || (docs[0].user_id && docs[0].user_id==userId)){
2012-09-06 11:34:05 +09:00
return true;
}
2012-09-13 11:57:35 +09:00
return false; }
2012-08-22 23:24:33 -04:00
});
});
// Comments
Comments = new Meteor.Collection('comments');
Meteor.publish('comments', function() {
return Comments.find();
});
Meteor.startup(function(){
Comments.allow({
2012-09-06 15:28:58 +09:00
insert: function(userId, doc){
if(userId){
return true;
}
return false;
2012-09-06 11:34:05 +09:00
}
, update: function(userId, docs, fields, modifier){
if(isAdmin(userId) || (docs[0].user_id && docs[0].user_id==userId)){
2012-09-19 09:03:25 +09:00
return true;
}
return false;
2012-09-06 11:34:05 +09:00
}
, remove: function(userId, docs){
if(isAdmin(userId) || (docs[0].user_id && docs[0].user_id==userId)){
2012-09-06 11:34:05 +09:00
return true;
}
2012-09-27 23:18:02 -07:00
return false;
2012-09-06 11:34:05 +09:00
}
2012-08-30 21:35:48 -04:00
});
});
2012-09-06 19:42:11 +09:00
// Settings
2012-09-06 11:34:05 +09:00
2012-09-06 19:42:11 +09:00
Settings = new Meteor.Collection('settings');
2012-09-06 11:34:05 +09:00
2012-09-06 19:42:11 +09:00
Meteor.publish('settings', function() {
return Settings.find();
2012-09-06 11:34:05 +09:00
});
Meteor.startup(function(){
2012-09-06 19:42:11 +09:00
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); }
2012-09-06 11:34:05 +09:00
});
});