meteor-interface
Version:
Simple Content Management System to generate your administration interface for Meteor and React.
109 lines (87 loc) • 3.26 kB
JavaScript
;
var _configuration = _interopRequireDefault(require("../../lib/configuration"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Global Config
Meteor.methods({
'interface.users.create': function interfaceUsersCreate(_ref) {
var user = _ref.user;
var config = _configuration.default.get(); // Extract datas from config
var _config$roles = config.roles,
roles = _config$roles === void 0 ? [] : _config$roles;
var isAuthorized = Roles.userIsInRole(this.userId, [roles[0]]);
try {
if (!isAuthorized) {
throw new Meteor.Error(403, "You aren't authorized to do that");
}
var userId = Accounts.createUser(user);
Roles.addUsersToRoles(userId, user.roles);
console.log(user, userId);
return userId;
} catch (error) {
throw new Meteor.Error(error.error, error.message);
}
},
'interface.users.delete': function interfaceUsersDelete(_ref2) {
var userId = _ref2.userId;
var config = _configuration.default.get(); // Extract datas from config
var _config$roles2 = config.roles,
roles = _config$roles2 === void 0 ? [] : _config$roles2;
var isAuthorized = Roles.userIsInRole(this.userId, [roles[0]]);
try {
if (!isAuthorized) {
throw new Meteor.Error(403, "You aren't authorized to do that");
} else if (this.userId === userId) {
throw new Meteor.Error(403, "You can't delete your own account");
}
return Meteor.users.remove({
_id: userId
});
} catch (error) {
throw new Meteor.Error(error.error, error.message);
}
},
'interface.users.update.roles': function interfaceUsersUpdateRoles(_ref3) {
var newRoles = _ref3.newRoles,
userId = _ref3.userId;
var config = _configuration.default.get(); // Extract datas from config
var _config$roles3 = config.roles,
roles = _config$roles3 === void 0 ? [] : _config$roles3;
var isAuthorized = Roles.userIsInRole(this.userId, [roles[0]]);
try {
if (!isAuthorized) {
throw new Meteor.Error(403, "You aren't authorized to do that");
} else if (this.userId === userId && newRoles.indexOf(roles[0]) === -1) {
throw new Meteor.Error(403, "You can't delete the ".concat(roles[0], " role from you account"));
}
return Meteor.users.update({
_id: userId
}, {
$set: {
roles: newRoles
}
});
} catch (error) {
throw new Meteor.Error(error.error, error.message);
}
},
'interface.users.update.profile': function interfaceUsersUpdateProfile(_ref4) {
var email = _ref4.email,
username = _ref4.username;
try {
//get old email
var user = Meteor.users.findOne(this.userId);
var oldEmail = user.emails[0].address;
var oldUsername = user.username;
if (email !== oldEmail) {
Accounts.addEmail(this.userId, email);
Accounts.removeEmail(this.userId, oldEmail);
}
if (oldUsername !== username) {
Accounts.setUsername(this.userId, username);
}
return true;
} catch (error) {
throw new Meteor.Error(error.error, error.message);
}
}
});