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 username = tpl.find('.js-profile-username').value.trim();
const initials = tpl.find('.js-profile-initials').value.trim(); const initials = tpl.find('.js-profile-initials').value.trim();
const isAdmin = tpl.find('.js-profile-isadmin').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(); const email = tpl.find('.js-profile-email').value.trim();
console.log('isAdmin', isAdmin);
let isChangeUserName = false; let isChangeUserName = false;
let isChangeEmail = false; let isChangeEmail = false;
Users.update(this.userId, { Users.update(this.userId, {
$set: { $set: {
'profile.fullname': fullname, 'profile.fullname': fullname,
'profile.initials': initials, 'profile.initials': initials,
'isAdmin': true, 'isAdmin': isAdmin === 'true',
'loginDisabled': isActive === 'true',
}, },
}); });
isChangeUserName = username !== user.username; isChangeUserName = username !== user.username;
isChangeEmail = email.toLowerCase() !== user.emails[0].address.toLowerCase(); isChangeEmail = email.toLowerCase() !== user.emails[0].address.toLowerCase();
if (isChangeUserName && isChangeEmail) { 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 usernameMessageElement = tpl.$('.username-taken');
const emailMessageElement = tpl.$('.email-taken'); const emailMessageElement = tpl.$('.email-taken');
if (error) { if (error) {
@ -119,29 +122,35 @@ Template.editUserPopup.events({
} else { } else {
usernameMessageElement.hide(); usernameMessageElement.hide();
emailMessageElement.hide(); emailMessageElement.hide();
Popup.back(); Popup.close();
} }
}); });
} else if (isChangeUserName) { } else if (isChangeUserName) {
Meteor.call('setUsername', username, function (error) { Meteor.call('setUsername', username, this.userId, function (error) {
const messageElement = tpl.$('.username-taken'); const usernameMessageElement = tpl.$('.username-taken');
if (error) { if (error) {
messageElement.show(); const errorElement = error.error;
if (errorElement === 'username-already-taken') {
usernameMessageElement.show();
}
} else { } else {
messageElement.hide(); usernameMessageElement.hide();
Popup.back(); Popup.close();
} }
}); });
} else if (isChangeEmail) { } else if (isChangeEmail) {
Meteor.call('setEmail', email.toLowerCase(), function (error) { Meteor.call('setEmail', email.toLowerCase(), this.userId, function (error) {
const messageElement = tpl.$('.email-taken'); const emailMessageElement = tpl.$('.email-taken');
if (error) { if (error) {
messageElement.show(); const errorElement = error.error;
if (errorElement === 'email-already-taken') {
emailMessageElement.show();
}
} else { } else {
messageElement.hide(); emailMessageElement.hide();
Popup.back(); 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 // 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. // is used for instance to add a new user to a board.
const searchInFields = ['username', 'profile.fullname']; const searchInFields = ['username', 'profile.fullname'];
@ -152,36 +159,36 @@ if (Meteor.isClient) {
Users.helpers({ Users.helpers({
boards() { boards() {
return Boards.find({ userId: this._id }); return Boards.find({userId: this._id});
}, },
starredBoards() { starredBoards() {
const { starredBoards = [] } = this.profile; const {starredBoards = []} = this.profile;
return Boards.find({ archived: false, _id: { $in: starredBoards } }); return Boards.find({archived: false, _id: {$in: starredBoards}});
}, },
hasStarred(boardId) { hasStarred(boardId) {
const { starredBoards = [] } = this.profile; const {starredBoards = []} = this.profile;
return _.contains(starredBoards, boardId); return _.contains(starredBoards, boardId);
}, },
invitedBoards() { invitedBoards() {
const { invitedBoards = [] } = this.profile; const {invitedBoards = []} = this.profile;
return Boards.find({ archived: false, _id: { $in: invitedBoards } }); return Boards.find({archived: false, _id: {$in: invitedBoards}});
}, },
isInvitedTo(boardId) { isInvitedTo(boardId) {
const { invitedBoards = [] } = this.profile; const {invitedBoards = []} = this.profile;
return _.contains(invitedBoards, boardId); return _.contains(invitedBoards, boardId);
}, },
hasTag(tag) { hasTag(tag) {
const { tags = [] } = this.profile; const {tags = []} = this.profile;
return _.contains(tags, tag); return _.contains(tags, tag);
}, },
hasNotification(activityId) { hasNotification(activityId) {
const { notifications = [] } = this.profile; const {notifications = []} = this.profile;
return _.contains(notifications, activityId); return _.contains(notifications, activityId);
}, },
@ -191,7 +198,7 @@ Users.helpers({
}, },
getEmailBuffer() { getEmailBuffer() {
const { emailBuffer = [] } = this.profile; const {emailBuffer = []} = this.profile;
return emailBuffer; return emailBuffer;
}, },
@ -316,18 +323,18 @@ Users.mutations({
}, },
setAvatarUrl(avatarUrl) { setAvatarUrl(avatarUrl) {
return { $set: { 'profile.avatarUrl': avatarUrl } }; return {$set: {'profile.avatarUrl': avatarUrl}};
}, },
setShowCardsCountAt(limit) { setShowCardsCountAt(limit) {
return { $set: { 'profile.showCardsCountAt': limit } }; return {$set: {'profile.showCardsCountAt': limit}};
}, },
}); });
Meteor.methods({ Meteor.methods({
setUsername(username, userId) { setUsername(username, userId) {
check(username, String); check(username, String);
const nUsersWithUsername = Users.find({ username }).count(); const nUsersWithUsername = Users.find({username}).count();
if (nUsersWithUsername > 0) { if (nUsersWithUsername > 0) {
throw new Meteor.Error('username-already-taken'); throw new Meteor.Error('username-already-taken');
} else { } else {
@ -344,7 +351,7 @@ Meteor.methods({
}, },
setEmail(email, userId) { setEmail(email, userId) {
check(email, String); check(email, String);
const existingUser = Users.findOne({ 'emails.address': email }, { fields: { _id: 1 } }); const existingUser = Users.findOne({'emails.address': email}, {fields: {_id: 1}});
if (existingUser) { if (existingUser) {
throw new Meteor.Error('email-already-taken'); throw new Meteor.Error('email-already-taken');
} else { } else {
@ -380,8 +387,8 @@ if (Meteor.isServer) {
board && board &&
board.members && board.members &&
_.contains(_.pluck(board.members, 'userId'), inviter._id) && _.contains(_.pluck(board.members, 'userId'), inviter._id) &&
_.where(board.members, { userId: inviter._id })[0].isActive && _.where(board.members, {userId: inviter._id})[0].isActive &&
_.where(board.members, { userId: inviter._id })[0].isAdmin; _.where(board.members, {userId: inviter._id})[0].isAdmin;
if (!allowInvite) throw new Meteor.Error('error-board-notAMember'); if (!allowInvite) throw new Meteor.Error('error-board-notAMember');
this.unblock(); this.unblock();
@ -389,9 +396,9 @@ if (Meteor.isServer) {
const posAt = username.indexOf('@'); const posAt = username.indexOf('@');
let user = null; let user = null;
if (posAt >= 0) { if (posAt >= 0) {
user = Users.findOne({ emails: { $elemMatch: { address: username } } }); user = Users.findOne({emails: {$elemMatch: {address: username}}});
} else { } else {
user = Users.findOne(username) || Users.findOne({ username }); user = Users.findOne(username) || Users.findOne({username});
} }
if (user) { if (user) {
if (user._id === inviter._id) throw new Meteor.Error('error-user-notAllowSelf'); if (user._id === inviter._id) throw new Meteor.Error('error-user-notAllowSelf');
@ -401,7 +408,7 @@ if (Meteor.isServer) {
// Set in lowercase email before creating account // Set in lowercase email before creating account
const email = username.toLowerCase(); const email = username.toLowerCase();
username = email.substring(0, posAt); username = email.substring(0, posAt);
const newUserId = Accounts.createUser({ username, email }); const newUserId = Accounts.createUser({username, email});
if (!newUserId) throw new Meteor.Error('error-user-notCreated'); if (!newUserId) throw new Meteor.Error('error-user-notCreated');
// assume new user speak same language with inviter // assume new user speak same language with inviter
if (inviter.profile && inviter.profile.language) { if (inviter.profile && inviter.profile.language) {
@ -435,7 +442,7 @@ if (Meteor.isServer) {
} catch (e) { } catch (e) {
throw new Meteor.Error('email-fail', e.message); throw new Meteor.Error('email-fail', e.message);
} }
return { username: user.username, email: user.emails[0].address }; return {username: user.username, email: user.emails[0].address};
}, },
}); });
Accounts.onCreateUser((options, user) => { Accounts.onCreateUser((options, user) => {
@ -458,11 +465,15 @@ if (Meteor.isServer) {
if (!options || !options.profile) { if (!options || !options.profile) {
throw new Meteor.Error('error-invitation-code-blank', 'The invitation code is required'); 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) { if (!invitationCode) {
throw new Meteor.Error('error-invitation-code-not-exist', 'The invitation code doesn\'t exist'); throw new Meteor.Error('error-invitation-code-not-exist', 'The invitation code doesn\'t exist');
} else { } else {
user.profile = { icode: options.profile.invitationcode }; user.profile = {icode: options.profile.invitationcode};
} }
return user; return user;
@ -474,7 +485,7 @@ if (Meteor.isServer) {
Meteor.startup(() => { Meteor.startup(() => {
Users._collection._ensureIndex({ Users._collection._ensureIndex({
username: 1, username: 1,
}, { unique: true }); }, {unique: true});
}); });
// Each board document contains the de-normalized number of users that have // Each board document contains the de-normalized number of users that have
@ -493,6 +504,7 @@ if (Meteor.isServer) {
function getStarredBoardsIds(doc) { function getStarredBoardsIds(doc) {
return doc.profile && doc.profile.starredBoards; return doc.profile && doc.profile.starredBoards;
} }
const oldIds = getStarredBoardsIds(this.previous); const oldIds = getStarredBoardsIds(this.previous);
const newIds = getStarredBoardsIds(user); const newIds = getStarredBoardsIds(user);
@ -501,9 +513,10 @@ if (Meteor.isServer) {
// direction and then in the other. // direction and then in the other.
function incrementBoards(boardsIds, inc) { function incrementBoards(boardsIds, inc) {
boardsIds.forEach((boardId) => { boardsIds.forEach((boardId) => {
Boards.update(boardId, { $inc: { stars: inc } }); Boards.update(boardId, {$inc: {stars: inc}});
}); });
} }
incrementBoards(_.difference(oldIds, newIds), -1); incrementBoards(_.difference(oldIds, newIds), -1);
incrementBoards(_.difference(newIds, oldIds), +1); incrementBoards(_.difference(newIds, oldIds), +1);
}); });
@ -530,7 +543,7 @@ if (Meteor.isServer) {
}, fakeUser, (err, boardId) => { }, fakeUser, (err, boardId) => {
['welcome-list1', 'welcome-list2'].forEach((title) => { ['welcome-list1', 'welcome-list2'].forEach((title) => {
Lists.insert({ title: TAPi18n.__(title), boardId }, fakeUser); Lists.insert({title: TAPi18n.__(title), boardId}, fakeUser);
}); });
}); });
}); });
@ -546,14 +559,14 @@ if (Meteor.isServer) {
// the disableRegistration check. // the disableRegistration check.
// Issue : https://github.com/wekan/wekan/issues/1232 // Issue : https://github.com/wekan/wekan/issues/1232
// PR : https://github.com/wekan/wekan/pull/1251 // PR : https://github.com/wekan/wekan/pull/1251
Users.update(doc._id, { $set: { createdThroughApi: '' } }); Users.update(doc._id, {$set: {createdThroughApi: ''}});
return; return;
} }
//invite user to corresponding boards //invite user to corresponding boards
const disableRegistration = Settings.findOne().disableRegistration; const disableRegistration = Settings.findOne().disableRegistration;
if (disableRegistration) { if (disableRegistration) {
const invitationCode = InvitationCodes.findOne({ code: doc.profile.icode, valid: true }); const invitationCode = InvitationCodes.findOne({code: doc.profile.icode, valid: true});
if (!invitationCode) { if (!invitationCode) {
throw new Meteor.Error('error-invitation-code-not-exist'); throw new Meteor.Error('error-invitation-code-not-exist');
} else { } else {
@ -565,8 +578,8 @@ if (Meteor.isServer) {
doc.profile = {}; doc.profile = {};
} }
doc.profile.invitedBoards = invitationCode.boardsToBeInvited; doc.profile.invitedBoards = invitationCode.boardsToBeInvited;
Users.update(doc._id, { $set: { profile: doc.profile } }); Users.update(doc._id, {$set: {profile: doc.profile}});
InvitationCodes.update(invitationCode._id, { $set: { valid: false } }); InvitationCodes.update(invitationCode._id, {$set: {valid: false}});
} }
} }
}); });
@ -575,9 +588,9 @@ if (Meteor.isServer) {
// USERS REST API // USERS REST API
if (Meteor.isServer) { if (Meteor.isServer) {
JsonRoutes.add('GET', '/api/user', function(req, res, next) { JsonRoutes.add('GET', '/api/user', function (req, res, next) {
Authentication.checkLoggedIn(req.userId); Authentication.checkLoggedIn(req.userId);
const data = Meteor.users.findOne({ _id: req.userId}); const data = Meteor.users.findOne({_id: req.userId});
delete data.services; delete data.services;
JsonRoutes.sendResult(res, { JsonRoutes.sendResult(res, {
code: 200, code: 200,
@ -586,33 +599,33 @@ if (Meteor.isServer) {
}); });
JsonRoutes.add('GET', '/api/users', function (req, res, next) { JsonRoutes.add('GET', '/api/users', function (req, res, next) {
Authentication.checkUserId( req.userId); Authentication.checkUserId(req.userId);
JsonRoutes.sendResult(res, { JsonRoutes.sendResult(res, {
code: 200, code: 200,
data: Meteor.users.find({}).map(function (doc) { data: Meteor.users.find({}).map(function (doc) {
return { _id: doc._id, username: doc.username }; return {_id: doc._id, username: doc.username};
}), }),
}); });
}); });
JsonRoutes.add('GET', '/api/users/:id', function (req, res, next) { JsonRoutes.add('GET', '/api/users/:id', function (req, res, next) {
Authentication.checkUserId( req.userId); Authentication.checkUserId(req.userId);
const id = req.params.id; const id = req.params.id;
JsonRoutes.sendResult(res, { JsonRoutes.sendResult(res, {
code: 200, code: 200,
data: Meteor.users.findOne({ _id: id }), data: Meteor.users.findOne({_id: id}),
}); });
}); });
JsonRoutes.add('PUT', '/api/users/:id', function (req, res, next) { JsonRoutes.add('PUT', '/api/users/:id', function (req, res, next) {
Authentication.checkUserId( req.userId); Authentication.checkUserId(req.userId);
const id = req.params.id; const id = req.params.id;
const action = req.body.action; const action = req.body.action;
let data = Meteor.users.findOne({ _id: id }); let data = Meteor.users.findOne({_id: id});
if (data !== undefined) { if (data !== undefined) {
if (action === 'takeOwnership') { if (action === 'takeOwnership') {
data = Boards.find({ data = Boards.find({
'members.userId': id, 'members.userId': id,
'members.isAdmin': true, 'members.isAdmin': true,
}).map(function(board) { }).map(function (board) {
if (board.hasMember(req.userId)) { if (board.hasMember(req.userId)) {
board.removeMember(req.userId); board.removeMember(req.userId);
} }
@ -624,11 +637,11 @@ if (Meteor.isServer) {
}); });
} else { } else {
if ((action === 'disableLogin') && (id !== req.userId)) { if ((action === 'disableLogin') && (id !== req.userId)) {
Users.update({ _id: id }, { $set: { loginDisabled: true, 'services.resume.loginTokens': '' } }); Users.update({_id: id}, {$set: {loginDisabled: true, 'services.resume.loginTokens': ''}});
} else if (action === 'enableLogin') { } else if (action === 'enableLogin') {
Users.update({ _id: id }, { $set: { loginDisabled: '' } }); Users.update({_id: id}, {$set: {loginDisabled: ''}});
} }
data = Meteor.users.findOne({ _id: id }); data = Meteor.users.findOne({_id: id});
} }
} }
JsonRoutes.sendResult(res, { JsonRoutes.sendResult(res, {
@ -637,7 +650,7 @@ if (Meteor.isServer) {
}); });
}); });
JsonRoutes.add('POST', '/api/users/', function (req, res, next) { JsonRoutes.add('POST', '/api/users/', function (req, res, next) {
Authentication.checkUserId( req.userId); Authentication.checkUserId(req.userId);
const id = Accounts.createUser({ const id = Accounts.createUser({
username: req.body.username, username: req.body.username,
email: req.body.email, email: req.body.email,
@ -654,9 +667,9 @@ if (Meteor.isServer) {
}); });
JsonRoutes.add('DELETE', '/api/users/:id', function (req, res, next) { JsonRoutes.add('DELETE', '/api/users/:id', function (req, res, next) {
Authentication.checkUserId( req.userId); Authentication.checkUserId(req.userId);
const id = req.params.id; const id = req.params.id;
Meteor.users.remove({ _id: id }); Meteor.users.remove({_id: id});
JsonRoutes.sendResult(res, { JsonRoutes.sendResult(res, {
code: 200, code: 200,
data: { data: {