From cdef8a33e4df1caf9c8796ded4d946a76acb28a0 Mon Sep 17 00:00:00 2001 From: guillaume Date: Fri, 26 Apr 2019 17:53:48 +0200 Subject: Delete user feature --- models/users.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'models/users.js') diff --git a/models/users.js b/models/users.js index 0dd9c1d6..c2687e35 100644 --- a/models/users.js +++ b/models/users.js @@ -238,6 +238,19 @@ Users.allow({ const user = Users.findOne(userId); return user && Meteor.user().isAdmin; }, + remove(userId, doc) { + const adminsNumber = Users.find({ isAdmin: true }).count(); + const { isAdmin } = Users.findOne({ _id: userId }, { fields: { 'isAdmin': 1 } }); + + // Prevents remove of the only one administrator + if (adminsNumber === 1 && isAdmin && userId === doc._id) { + return false; + } + + // If it's the user or an admin + return userId === doc._id || isAdmin; + }, + fetch: [], }); // Search a user in the complete server database by its name or username. This @@ -364,6 +377,10 @@ Users.helpers({ getTemplatesBoardSlug() { return Boards.findOne(this.profile.templatesBoardId).slug; }, + + remove() { + User.remove({ _id: this._id}); + }, }); Users.mutations({ @@ -673,6 +690,17 @@ if (Meteor.isServer) { }, {unique: true}); }); + Users.before.remove((userId, doc) => { + Boards + .find({members: {$elemMatch: {userId: doc._id, isAdmin: true}}}) + .forEach((board) => { + // If only one admin for the board + if (board.members.filter((e) => e.isAdmin).length === 1) { + Boards.remove(board._id); + } + }); + }); + // Each board document contains the de-normalized number of users that have // starred it. If the user star or unstar a board, we need to update this // counter. -- cgit v1.2.3-1-g7c22 From 64ee60a008c929dcf63ac5d2c49f7f189508a757 Mon Sep 17 00:00:00 2001 From: Lauri Ojansivu Date: Thu, 9 May 2019 14:32:38 +0300 Subject: Fix missing profile checks. Thanks to justinr1234 ! --- models/users.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'models/users.js') diff --git a/models/users.js b/models/users.js index 0dd9c1d6..3240f8de 100644 --- a/models/users.js +++ b/models/users.js @@ -288,32 +288,32 @@ Users.helpers({ }, starredBoards() { - const {starredBoards = []} = this.profile; + const {starredBoards = []} = this.profile || {}; return Boards.find({archived: false, _id: {$in: starredBoards}}); }, hasStarred(boardId) { - const {starredBoards = []} = this.profile; + const {starredBoards = []} = this.profile || {}; return _.contains(starredBoards, boardId); }, invitedBoards() { - const {invitedBoards = []} = this.profile; + const {invitedBoards = []} = this.profile || {}; return Boards.find({archived: false, _id: {$in: invitedBoards}}); }, isInvitedTo(boardId) { - const {invitedBoards = []} = this.profile; + const {invitedBoards = []} = this.profile || {}; return _.contains(invitedBoards, boardId); }, hasTag(tag) { - const {tags = []} = this.profile; + const {tags = []} = this.profile || {}; return _.contains(tags, tag); }, hasNotification(activityId) { - const {notifications = []} = this.profile; + const {notifications = []} = this.profile || {}; return _.contains(notifications, activityId); }, @@ -323,7 +323,7 @@ Users.helpers({ }, getEmailBuffer() { - const {emailBuffer = []} = this.profile; + const {emailBuffer = []} = this.profile || {}; return emailBuffer; }, @@ -358,11 +358,11 @@ Users.helpers({ }, getTemplatesBoardId() { - return this.profile.templatesBoardId; + return (this.profile || {}).templatesBoardId; }, getTemplatesBoardSlug() { - return Boards.findOne(this.profile.templatesBoardId).slug; + return (Boards.findOne((this.profile || {}).templatesBoardId) || {}).slug; }, }); -- cgit v1.2.3-1-g7c22 From e1b016cf3d4ff93e9e0fe1feb96372e3e1625233 Mon Sep 17 00:00:00 2001 From: Lauri Ojansivu Date: Thu, 9 May 2019 16:17:53 +0300 Subject: Prevent data loss. Thanks to xet7 ! --- models/users.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'models/users.js') diff --git a/models/users.js b/models/users.js index 4ce0ca3f..5f949c80 100644 --- a/models/users.js +++ b/models/users.js @@ -690,16 +690,22 @@ if (Meteor.isServer) { }, {unique: true}); }); - Users.before.remove((userId, doc) => { - Boards - .find({members: {$elemMatch: {userId: doc._id, isAdmin: true}}}) - .forEach((board) => { - // If only one admin for the board - if (board.members.filter((e) => e.isAdmin).length === 1) { - Boards.remove(board._id); - } - }); - }); + // OLD WAY THIS CODE DID WORK: When user is last admin of board, + // if admin is removed, board is removed. + // NOW THIS IS COMMENTED OUT, because other board users still need to be able + // to use that board, and not have board deleted. + // Someone can be later changed to be admin of board, by making change to database. + // TODO: Add UI for changing someone as board admin. + //Users.before.remove((userId, doc) => { + // Boards + // .find({members: {$elemMatch: {userId: doc._id, isAdmin: true}}}) + // .forEach((board) => { + // // If only one admin for the board + // if (board.members.filter((e) => e.isAdmin).length === 1) { + // Boards.remove(board._id); + // } + // }); + //}); // Each board document contains the de-normalized number of users that have // starred it. If the user star or unstar a board, we need to update this -- cgit v1.2.3-1-g7c22