summaryrefslogtreecommitdiffstats
path: root/models/users.js
diff options
context:
space:
mode:
authorLauri Ojansivu <x@xet7.org>2019-05-09 16:06:17 +0300
committerGitHub <noreply@github.com>2019-05-09 16:06:17 +0300
commitc8506697776c5f5ed0d82de0f4bd84bcc379db45 (patch)
treed31562f4bd14dedc096f806e2b250a0e7711cf25 /models/users.js
parent04c7372a4e665becb7383c319b9b514217468e54 (diff)
parentcdef8a33e4df1caf9c8796ded4d946a76acb28a0 (diff)
downloadwekan-c8506697776c5f5ed0d82de0f4bd84bcc379db45.tar.gz
wekan-c8506697776c5f5ed0d82de0f4bd84bcc379db45.tar.bz2
wekan-c8506697776c5f5ed0d82de0f4bd84bcc379db45.zip
Merge pull request #2384 from Akuket/origin/edge
Delete user feature
Diffstat (limited to 'models/users.js')
-rw-r--r--models/users.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/models/users.js b/models/users.js
index 3240f8de..4ce0ca3f 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.