2016-06-23 15:00:58 +09:00
|
|
|
|
import Users from './collection.js';
|
2016-06-15 11:07:10 +09:00
|
|
|
|
|
2016-02-23 11:34:40 +09:00
|
|
|
|
var completeUserProfile = function (userId, modifier, user) {
|
2015-05-06 16:41:15 +09:00
|
|
|
|
|
2015-05-19 12:34:27 +09:00
|
|
|
|
Users.update(userId, modifier);
|
2015-05-06 16:41:15 +09:00
|
|
|
|
|
2015-05-19 12:34:27 +09:00
|
|
|
|
Telescope.callbacks.runAsync("profileCompletedAsync", Users.findOne(userId));
|
2015-05-06 16:41:15 +09:00
|
|
|
|
|
2015-05-19 12:34:27 +09:00
|
|
|
|
return Users.findOne(userId);
|
2015-05-06 16:41:15 +09:00
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-23 11:34:40 +09:00
|
|
|
|
Users.methods = {};
|
|
|
|
|
|
2016-06-23 11:40:35 +09:00
|
|
|
|
/**
|
|
|
|
|
* @summary Edit a user in the database
|
|
|
|
|
* @param {string} userId – the ID of the user being edited
|
|
|
|
|
* @param {Object} modifier – the modifier object
|
|
|
|
|
* @param {Object} user - the current user object
|
|
|
|
|
*/
|
2016-02-23 11:34:40 +09:00
|
|
|
|
Users.methods.edit = (userId, modifier, user) => {
|
|
|
|
|
|
|
|
|
|
if (typeof user === "undefined") {
|
2016-06-23 11:40:35 +09:00
|
|
|
|
user = Users.findOne(userId);
|
2016-02-23 11:34:40 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------ Callbacks ------------------------------ //
|
|
|
|
|
|
2016-04-19 15:45:36 +09:00
|
|
|
|
modifier = Telescope.callbacks.run("UsersEdit", modifier, user);
|
2016-02-23 11:34:40 +09:00
|
|
|
|
|
|
|
|
|
// ------------------------------ Update ------------------------------ //
|
|
|
|
|
|
|
|
|
|
Users.update(userId, modifier);
|
|
|
|
|
|
|
|
|
|
// ------------------------------ Callbacks ------------------------------ //
|
|
|
|
|
|
2016-04-19 15:45:36 +09:00
|
|
|
|
Telescope.callbacks.runAsync("UsersEditAsync", Users.findOne(userId), user);
|
2016-02-23 11:34:40 +09:00
|
|
|
|
|
|
|
|
|
// ------------------------------ After Update ------------------------------ //
|
|
|
|
|
return Users.findOne(userId);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 19:55:59 +09:00
|
|
|
|
Users.methods.setSetting = (userId, settingName, value) => {
|
|
|
|
|
// all settings should be in the user.telescope namespace, so add "telescope." if needed
|
|
|
|
|
var field = settingName.slice(0,10) === "telescope." ? settingName : "telescope." + settingName;
|
|
|
|
|
|
|
|
|
|
var modifier = {$set: {}};
|
|
|
|
|
modifier.$set[field] = value;
|
|
|
|
|
|
|
|
|
|
Users.update(userId, modifier);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 17:30:59 +09:00
|
|
|
|
Users.methods.addGroup = (userId, groupName) => {
|
|
|
|
|
Users.update(userId, {$push: {"telescope.groups": groupName}});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Users.methods.removeGroup = (userId, groupName) => {
|
|
|
|
|
Users.update(userId, {$pull: {"telescope.groups": groupName}});
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-06 12:28:00 +09:00
|
|
|
|
Meteor.methods({
|
2016-02-23 11:34:40 +09:00
|
|
|
|
'users.compleProfile'(modifier, userId) {
|
2015-07-15 10:11:29 +09:00
|
|
|
|
|
|
|
|
|
check(modifier, Match.OneOf({$set: Object}, {$unset: Object}, {$set: Object, $unset: Object}));
|
2015-07-10 11:40:11 +09:00
|
|
|
|
check(userId, String);
|
|
|
|
|
|
2015-05-06 16:41:15 +09:00
|
|
|
|
var currentUser = Meteor.user(),
|
|
|
|
|
user = Users.findOne(userId),
|
|
|
|
|
schema = Users.simpleSchema()._schema;
|
|
|
|
|
|
|
|
|
|
// ------------------------------ Checks ------------------------------ //
|
|
|
|
|
|
|
|
|
|
// check that user can edit document
|
|
|
|
|
if (!user || !Users.can.edit(currentUser, user)) {
|
2016-07-04 10:42:50 +09:00
|
|
|
|
throw new Meteor.Error(601, 'sorry_you_cannot_edit_this_user');
|
2015-05-06 12:28:00 +09:00
|
|
|
|
}
|
2015-05-06 16:41:15 +09:00
|
|
|
|
|
2015-06-02 11:53:18 +09:00
|
|
|
|
// if an $unset modifier is present, it means one or more of the fields is missing
|
|
|
|
|
if (modifier.$unset) {
|
2016-07-04 10:42:50 +09:00
|
|
|
|
throw new Meteor.Error(601, 'all_fields_are_required');
|
2015-06-02 11:53:18 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check for existing emails and throw error if necessary
|
|
|
|
|
// NOTE: redundant with collection hook, but better to throw the error here to avoid wiping out the form
|
|
|
|
|
if (modifier.$set && modifier.$set["telescope.email"]) {
|
|
|
|
|
var email = modifier.$set["telescope.email"];
|
|
|
|
|
if (Users.findByEmail(email)) {
|
2016-07-04 10:42:50 +09:00
|
|
|
|
throw new Meteor.Error("email_taken1", "this_email_is_already_taken" + " (" + email + ")");
|
2015-06-02 11:53:18 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-06 16:41:15 +09:00
|
|
|
|
// go over each field and throw an error if it's not editable
|
|
|
|
|
// loop over each operation ($set, $unset, etc.)
|
|
|
|
|
_.each(modifier, function (operation) {
|
|
|
|
|
// loop over each property being operated on
|
|
|
|
|
_.keys(operation).forEach(function (fieldName) {
|
|
|
|
|
var field = schema[fieldName];
|
|
|
|
|
if (!Users.can.editField(user, field, user)) {
|
2016-07-07 10:38:18 +09:00
|
|
|
|
throw new Meteor.Error("disallowed_property", 'disallowed_property_detected' + ": " + fieldName);
|
2015-05-06 12:28:00 +09:00
|
|
|
|
}
|
2015-05-06 16:41:15 +09:00
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-23 11:34:40 +09:00
|
|
|
|
completeUserProfile(userId, modifier, user);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
'users.edit'(userId, modifier) {
|
|
|
|
|
|
|
|
|
|
// checking might be redundant because SimpleSchema already enforces the schema, but you never know
|
|
|
|
|
check(modifier, Match.OneOf({$set: Users.simpleSchema()}, {$unset: Object}, {$set: Users.simpleSchema(), $unset: Object}));
|
|
|
|
|
check(userId, String);
|
|
|
|
|
|
|
|
|
|
var currentUser = Meteor.user(),
|
|
|
|
|
user = Users.findOne(userId),
|
|
|
|
|
schema = Users.simpleSchema()._schema;
|
|
|
|
|
|
|
|
|
|
// ------------------------------ Checks ------------------------------ //
|
|
|
|
|
|
|
|
|
|
// check that user can edit document
|
|
|
|
|
if (!user || !Users.can.edit(currentUser, user)) {
|
2016-07-07 10:38:18 +09:00
|
|
|
|
throw new Meteor.Error(601, 'sorry_you_cannot_edit_this_user');
|
2016-02-23 11:34:40 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// go over each field and throw an error if it's not editable
|
|
|
|
|
// loop over each operation ($set, $unset, etc.)
|
|
|
|
|
_.each(modifier, function (operation) {
|
|
|
|
|
// loop over each property being operated on
|
|
|
|
|
_.keys(operation).forEach(function (fieldName) {
|
|
|
|
|
|
|
|
|
|
var field = schema[fieldName];
|
|
|
|
|
if (!Users.can.editField(currentUser, field, user)) {
|
2016-07-07 10:38:18 +09:00
|
|
|
|
throw new Meteor.Error("disallowed_property", 'disallowed_property_detected' + ": " + fieldName);
|
2016-02-23 11:34:40 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Users.methods.edit(userId, modifier, user);
|
|
|
|
|
|
2015-09-21 09:59:17 +09:00
|
|
|
|
},
|
|
|
|
|
|
2016-06-23 11:40:35 +09:00
|
|
|
|
'users.remove'(userId, options) {
|
2015-09-21 09:59:17 +09:00
|
|
|
|
|
|
|
|
|
if (Users.is.adminById(this.userId)) {
|
|
|
|
|
|
2016-06-23 11:40:35 +09:00
|
|
|
|
const user = Users.findOne(userId);
|
2015-09-21 09:59:17 +09:00
|
|
|
|
|
|
|
|
|
Meteor.users.remove(userId);
|
|
|
|
|
|
2016-06-23 11:40:35 +09:00
|
|
|
|
Telescope.callbacks.runAsync("users.remove.async", user, options);
|
2015-09-21 09:59:17 +09:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-27 17:30:28 +09:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
'users.setSetting'(userId, settingName, value) {
|
|
|
|
|
|
|
|
|
|
check(userId, String);
|
|
|
|
|
check(settingName, String);
|
|
|
|
|
check(value, Match.OneOf(String, Number, Boolean));
|
|
|
|
|
|
|
|
|
|
var currentUser = Meteor.user(),
|
|
|
|
|
user = Users.findOne(userId);
|
|
|
|
|
|
|
|
|
|
// check that user can edit document
|
|
|
|
|
if (!user || !Users.can.edit(currentUser, user)) {
|
2016-07-04 10:42:50 +09:00
|
|
|
|
throw new Meteor.Error(601, 'sorry_you_cannot_edit_this_user');
|
2016-03-27 17:30:28 +09:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 19:55:59 +09:00
|
|
|
|
Users.methods.setSetting(userId, settingName, value);
|
2016-03-27 17:30:28 +09:00
|
|
|
|
|
2015-05-06 12:28:00 +09:00
|
|
|
|
}
|
2015-09-21 09:59:17 +09:00
|
|
|
|
|
2015-05-06 12:28:00 +09:00
|
|
|
|
});
|