summaryrefslogtreecommitdiffstats
path: root/models/boards.js
diff options
context:
space:
mode:
Diffstat (limited to 'models/boards.js')
-rw-r--r--models/boards.js86
1 files changed, 67 insertions, 19 deletions
diff --git a/models/boards.js b/models/boards.js
index 98d6ec77..c10e51a3 100644
--- a/models/boards.js
+++ b/models/boards.js
@@ -80,8 +80,7 @@ Boards.helpers({
},
lists() {
- return Lists.find({ boardId: this._id, archived: false },
- { sort: { sort: 1 }});
+ return Lists.find({ boardId: this._id, archived: false }, { sort: { sort: 1 }});
},
activities() {
@@ -92,6 +91,14 @@ Boards.helpers({
return _.where(this.members, {isActive: true});
},
+ activeAdmins() {
+ return _.where(this.members, {isActive: true, isAdmin: true});
+ },
+
+ memberUsers() {
+ return Users.find({ _id: {$in: _.pluck(this.members, 'userId')} });
+ },
+
getLabel(name, color) {
return _.findWhere(this.labels, { name, color });
},
@@ -172,20 +179,30 @@ Boards.mutations({
addMember(memberId) {
const memberIndex = this.memberIndex(memberId);
if (memberIndex === -1) {
- return {
- $push: {
- members: {
- userId: memberId,
- isAdmin: false,
- isActive: true,
+ const xIndex = this.memberIndex('x');
+ if (xIndex === -1) {
+ return {
+ $push: {
+ members: {
+ userId: memberId,
+ isAdmin: false,
+ isActive: true,
+ },
},
- },
- };
+ };
+ } else {
+ return {
+ $set: {
+ [`members.${xIndex}.userId`]: memberId,
+ [`members.${xIndex}.isActive`]: true,
+ [`members.${xIndex}.isAdmin`]: false,
+ },
+ };
+ }
} else {
return {
$set: {
[`members.${memberIndex}.isActive`]: true,
- [`members.${memberIndex}.isAdmin`]: false,
},
};
}
@@ -194,16 +211,34 @@ Boards.mutations({
removeMember(memberId) {
const memberIndex = this.memberIndex(memberId);
- return {
- $set: {
- [`members.${memberIndex}.isActive`]: false,
- },
- };
+ // we do not allow the only one admin to be removed
+ const allowRemove = (!this.members[memberIndex].isAdmin) || (this.activeAdmins().length > 1);
+
+ if (allowRemove) {
+ return {
+ $set: {
+ [`members.${memberIndex}.userId`]: 'x',
+ [`members.${memberIndex}.isActive`]: false,
+ [`members.${memberIndex}.isAdmin`]: false,
+ },
+ };
+ } else {
+ return {
+ $set: {
+ [`members.${memberIndex}.isActive`]: true,
+ },
+ };
+ }
},
setMemberPermission(memberId, isAdmin) {
const memberIndex = this.memberIndex(memberId);
+ // do not allow change permission of self
+ if (memberId === Meteor.userId()) {
+ isAdmin = this.members[memberIndex].isAdmin;
+ }
+
return {
$set: {
[`members.${memberIndex}.isAdmin`]: isAdmin,
@@ -240,9 +275,7 @@ if (Meteor.isServer) {
return false;
// If there is more than one admin, it's ok to remove anyone
- const nbAdmins = _.filter(doc.members, (member) => {
- return member.isAdmin;
- }).length;
+ const nbAdmins = _.where(doc.members, {isActive: true, isAdmin: true}).length;
if (nbAdmins > 1)
return false;
@@ -256,6 +289,21 @@ if (Meteor.isServer) {
},
fetch: ['members'],
});
+
+ Meteor.methods({
+ quitBoard(boardId) {
+ check(boardId, String);
+ const board = Boards.findOne(boardId);
+ if (board) {
+ const userId = Meteor.userId();
+ const index = board.memberIndex(userId);
+ if (index>=0) {
+ board.removeMember(userId);
+ return true;
+ } else throw new Meteor.Error('error-board-notAMember');
+ } else throw new Meteor.Error('error-board-doesNotExist');
+ },
+ });
}
Boards.before.insert((userId, doc) => {