Added update all user profile from admin panel

This commit is contained in:
Thuan Pham Quoc 2017-11-08 11:34:37 +07:00
parent e3b7f85cc3
commit fa1d8cd5ef
2 changed files with 81 additions and 59 deletions

View file

@ -89,22 +89,25 @@ Template.editUserPopup.events({
const username = tpl.find('.js-profile-username').value.trim();
const initials = tpl.find('.js-profile-initials').value.trim();
const isAdmin = tpl.find('.js-profile-isadmin').value.trim();
const isActive = tpl.find('.js-profile-isactive').value.trim();
const email = tpl.find('.js-profile-email').value.trim();
console.log('isAdmin', isAdmin);
let isChangeUserName = false;
let isChangeEmail = false;
Users.update(this.userId, {
$set: {
'profile.fullname': fullname,
'profile.initials': initials,
'isAdmin': true,
'isAdmin': isAdmin === 'true',
'loginDisabled': isActive === 'true',
},
});
isChangeUserName = username !== user.username;
isChangeEmail = email.toLowerCase() !== user.emails[0].address.toLowerCase();
if (isChangeUserName && isChangeEmail) {
Meteor.call('setUsernameAndEmail', username, email.toLowerCase(), function (error) {
Meteor.call('setUsernameAndEmail', username, email.toLowerCase(), this.userId, function (error) {
const usernameMessageElement = tpl.$('.username-taken');
const emailMessageElement = tpl.$('.email-taken');
if (error) {
@ -119,29 +122,35 @@ Template.editUserPopup.events({
} else {
usernameMessageElement.hide();
emailMessageElement.hide();
Popup.back();
Popup.close();
}
});
} else if (isChangeUserName) {
Meteor.call('setUsername', username, function (error) {
const messageElement = tpl.$('.username-taken');
Meteor.call('setUsername', username, this.userId, function (error) {
const usernameMessageElement = tpl.$('.username-taken');
if (error) {
messageElement.show();
const errorElement = error.error;
if (errorElement === 'username-already-taken') {
usernameMessageElement.show();
}
} else {
messageElement.hide();
Popup.back();
usernameMessageElement.hide();
Popup.close();
}
});
} else if (isChangeEmail) {
Meteor.call('setEmail', email.toLowerCase(), function (error) {
const messageElement = tpl.$('.email-taken');
Meteor.call('setEmail', email.toLowerCase(), this.userId, function (error) {
const emailMessageElement = tpl.$('.email-taken');
if (error) {
messageElement.show();
const errorElement = error.error;
if (errorElement === 'email-already-taken') {
emailMessageElement.show();
}
} else {
messageElement.hide();
Popup.back();
emailMessageElement.hide();
Popup.close();
}
});
} else Popup.back();
} else Popup.close();
},
});

View file

@ -118,6 +118,13 @@ Users.attachSchema(new SimpleSchema({
},
}));
Users.allow({
update(userId) {
const user = Users.findOne(userId);
return user && Meteor.user().isAdmin;
},
});
// Search a user in the complete server database by its name or username. This
// is used for instance to add a new user to a board.
const searchInFields = ['username', 'profile.fullname'];
@ -458,7 +465,11 @@ if (Meteor.isServer) {
if (!options || !options.profile) {
throw new Meteor.Error('error-invitation-code-blank', 'The invitation code is required');
}
const invitationCode = InvitationCodes.findOne({ code: options.profile.invitationcode, email: options.email, valid: true });
const invitationCode = InvitationCodes.findOne({
code: options.profile.invitationcode,
email: options.email,
valid: true
});
if (!invitationCode) {
throw new Meteor.Error('error-invitation-code-not-exist', 'The invitation code doesn\'t exist');
} else {
@ -493,6 +504,7 @@ if (Meteor.isServer) {
function getStarredBoardsIds(doc) {
return doc.profile && doc.profile.starredBoards;
}
const oldIds = getStarredBoardsIds(this.previous);
const newIds = getStarredBoardsIds(user);
@ -504,6 +516,7 @@ if (Meteor.isServer) {
Boards.update(boardId, {$inc: {stars: inc}});
});
}
incrementBoards(_.difference(oldIds, newIds), -1);
incrementBoards(_.difference(newIds, oldIds), +1);
});