diff options
author | Justin Reynolds <justinr1234@gmail.com> | 2019-06-26 17:47:27 -0500 |
---|---|---|
committer | Justin Reynolds <justinr1234@gmail.com> | 2019-06-27 09:13:20 -0500 |
commit | c60e80d25baa6a81b28f6090ca848553d20b2bb7 (patch) | |
tree | 64bcea66932f4b4635d5df6901f18d5ba6db0a37 /models/users.js | |
parent | fb728baf0c87bae5fa39d92089b667ff1ed69fa6 (diff) | |
download | wekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.tar.gz wekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.tar.bz2 wekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.zip |
Add createdAt and modifiedAt to all collections
Diffstat (limited to 'models/users.js')
-rw-r--r-- | models/users.js | 889 |
1 files changed, 490 insertions, 399 deletions
diff --git a/models/users.js b/models/users.js index 5f949c80..306193aa 100644 --- a/models/users.js +++ b/models/users.js @@ -1,237 +1,254 @@ // Sandstorm context is detected using the METEOR_SETTINGS environment variable // in the package definition. -const isSandstorm = Meteor.settings && Meteor.settings.public && - Meteor.settings.public.sandstorm; +const isSandstorm = + Meteor.settings && Meteor.settings.public && Meteor.settings.public.sandstorm; Users = Meteor.users; /** * A User in wekan */ -Users.attachSchema(new SimpleSchema({ - username: { - /** - * the username of the user - */ - type: String, - optional: true, - autoValue() { // eslint-disable-line consistent-return - if (this.isInsert && !this.isSet) { - const name = this.field('profile.fullname'); - if (name.isSet) { - return name.value.toLowerCase().replace(/\s/g, ''); +Users.attachSchema( + new SimpleSchema({ + username: { + /** + * the username of the user + */ + type: String, + optional: true, + // eslint-disable-next-line consistent-return + autoValue() { + if (this.isInsert && !this.isSet) { + const name = this.field('profile.fullname'); + if (name.isSet) { + return name.value.toLowerCase().replace(/\s/g, ''); + } } - } + }, }, - }, - emails: { - /** - * the list of emails attached to a user - */ - type: [Object], - optional: true, - }, - 'emails.$.address': { - /** - * The email address - */ - type: String, - regEx: SimpleSchema.RegEx.Email, - }, - 'emails.$.verified': { - /** - * Has the email been verified - */ - type: Boolean, - }, - createdAt: { - /** - * creation date of the user - */ - type: Date, - autoValue() { // eslint-disable-line consistent-return - if (this.isInsert) { - return new Date(); - } else { - this.unset(); - } + emails: { + /** + * the list of emails attached to a user + */ + type: [Object], + optional: true, }, - }, - profile: { - /** - * profile settings - */ - type: Object, - optional: true, - autoValue() { // eslint-disable-line consistent-return - if (this.isInsert && !this.isSet) { - return { - boardView: 'board-view-lists', - }; - } + 'emails.$.address': { + /** + * The email address + */ + type: String, + regEx: SimpleSchema.RegEx.Email, }, - }, - 'profile.avatarUrl': { - /** - * URL of the avatar of the user - */ - type: String, - optional: true, - }, - 'profile.emailBuffer': { - /** - * list of email buffers of the user - */ - type: [String], - optional: true, - }, - 'profile.fullname': { - /** - * full name of the user - */ - type: String, - optional: true, - }, - 'profile.hiddenSystemMessages': { - /** - * does the user wants to hide system messages? - */ - type: Boolean, - optional: true, - }, - 'profile.initials': { - /** - * initials of the user - */ - type: String, - optional: true, - }, - 'profile.invitedBoards': { - /** - * board IDs the user has been invited to - */ - type: [String], - optional: true, - }, - 'profile.language': { - /** - * language of the user - */ - type: String, - optional: true, - }, - 'profile.notifications': { - /** - * enabled notifications for the user - */ - type: [String], - optional: true, - }, - 'profile.showCardsCountAt': { - /** - * showCardCountAt field of the user - */ - type: Number, - optional: true, - }, - 'profile.starredBoards': { - /** - * list of starred board IDs - */ - type: [String], - optional: true, - }, - 'profile.icode': { - /** - * icode - */ - type: String, - optional: true, - }, - 'profile.boardView': { - /** - * boardView field of the user - */ - type: String, - optional: true, - allowedValues: [ - 'board-view-lists', - 'board-view-swimlanes', - 'board-view-cal', - ], - }, - 'profile.templatesBoardId': { - /** - * Reference to the templates board - */ - type: String, - defaultValue: '', - }, - 'profile.cardTemplatesSwimlaneId': { - /** - * Reference to the card templates swimlane Id - */ - type: String, - defaultValue: '', - }, - 'profile.listTemplatesSwimlaneId': { - /** - * Reference to the list templates swimlane Id - */ - type: String, - defaultValue: '', - }, - 'profile.boardTemplatesSwimlaneId': { - /** - * Reference to the board templates swimlane Id - */ - type: String, - defaultValue: '', - }, - services: { - /** - * services field of the user - */ - type: Object, - optional: true, - blackbox: true, - }, - heartbeat: { - /** - * last time the user has been seen - */ - type: Date, - optional: true, - }, - isAdmin: { - /** - * is the user an admin of the board? - */ - type: Boolean, - optional: true, - }, - createdThroughApi: { - /** - * was the user created through the API? - */ - type: Boolean, - optional: true, - }, - loginDisabled: { - /** - * loginDisabled field of the user - */ - type: Boolean, - optional: true, - }, - 'authenticationMethod': { - /** - * authentication method of the user - */ - type: String, - optional: false, - defaultValue: 'password', - }, -})); + 'emails.$.verified': { + /** + * Has the email been verified + */ + type: Boolean, + }, + createdAt: { + /** + * creation date of the user + */ + type: Date, + // eslint-disable-next-line consistent-return + autoValue() { + if (this.isInsert) { + return new Date(); + } else { + this.unset(); + } + }, + }, + modifiedAt: { + type: Date, + denyUpdate: false, + // eslint-disable-next-line consistent-return + autoValue() { + if (this.isInsert || this.isUpsert || this.isUpdate) { + return new Date(); + } else { + this.unset(); + } + }, + }, + profile: { + /** + * profile settings + */ + type: Object, + optional: true, + // eslint-disable-next-line consistent-return + autoValue() { + if (this.isInsert && !this.isSet) { + return { + boardView: 'board-view-lists', + }; + } + }, + }, + 'profile.avatarUrl': { + /** + * URL of the avatar of the user + */ + type: String, + optional: true, + }, + 'profile.emailBuffer': { + /** + * list of email buffers of the user + */ + type: [String], + optional: true, + }, + 'profile.fullname': { + /** + * full name of the user + */ + type: String, + optional: true, + }, + 'profile.hiddenSystemMessages': { + /** + * does the user wants to hide system messages? + */ + type: Boolean, + optional: true, + }, + 'profile.initials': { + /** + * initials of the user + */ + type: String, + optional: true, + }, + 'profile.invitedBoards': { + /** + * board IDs the user has been invited to + */ + type: [String], + optional: true, + }, + 'profile.language': { + /** + * language of the user + */ + type: String, + optional: true, + }, + 'profile.notifications': { + /** + * enabled notifications for the user + */ + type: [String], + optional: true, + }, + 'profile.showCardsCountAt': { + /** + * showCardCountAt field of the user + */ + type: Number, + optional: true, + }, + 'profile.starredBoards': { + /** + * list of starred board IDs + */ + type: [String], + optional: true, + }, + 'profile.icode': { + /** + * icode + */ + type: String, + optional: true, + }, + 'profile.boardView': { + /** + * boardView field of the user + */ + type: String, + optional: true, + allowedValues: [ + 'board-view-lists', + 'board-view-swimlanes', + 'board-view-cal', + ], + }, + 'profile.templatesBoardId': { + /** + * Reference to the templates board + */ + type: String, + defaultValue: '', + }, + 'profile.cardTemplatesSwimlaneId': { + /** + * Reference to the card templates swimlane Id + */ + type: String, + defaultValue: '', + }, + 'profile.listTemplatesSwimlaneId': { + /** + * Reference to the list templates swimlane Id + */ + type: String, + defaultValue: '', + }, + 'profile.boardTemplatesSwimlaneId': { + /** + * Reference to the board templates swimlane Id + */ + type: String, + defaultValue: '', + }, + services: { + /** + * services field of the user + */ + type: Object, + optional: true, + blackbox: true, + }, + heartbeat: { + /** + * last time the user has been seen + */ + type: Date, + optional: true, + }, + isAdmin: { + /** + * is the user an admin of the board? + */ + type: Boolean, + optional: true, + }, + createdThroughApi: { + /** + * was the user created through the API? + */ + type: Boolean, + optional: true, + }, + loginDisabled: { + /** + * loginDisabled field of the user + */ + type: Boolean, + optional: true, + }, + authenticationMethod: { + /** + * authentication method of the user + */ + type: String, + optional: false, + defaultValue: 'password', + }, + }) +); Users.allow({ update(userId) { @@ -240,7 +257,10 @@ Users.allow({ }, remove(userId, doc) { const adminsNumber = Users.find({ isAdmin: true }).count(); - const { isAdmin } = Users.findOne({ _id: userId }, { fields: { 'isAdmin': 1 } }); + const { isAdmin } = Users.findOne( + { _id: userId }, + { fields: { isAdmin: 1 } } + ); // Prevents remove of the only one administrator if (adminsNumber === 1 && isAdmin && userId === doc._id) { @@ -270,7 +290,9 @@ if (Meteor.isClient) { isNotNoComments() { const board = Boards.findOne(Session.get('currentBoard')); - return board && board.hasMember(this._id) && !board.hasNoComments(this._id); + return ( + board && board.hasMember(this._id) && !board.hasNoComments(this._id) + ); }, isNoComments() { @@ -280,7 +302,9 @@ if (Meteor.isClient) { isNotCommentOnly() { const board = Boards.findOne(Session.get('currentBoard')); - return board && board.hasMember(this._id) && !board.hasCommentOnly(this._id); + return ( + board && board.hasMember(this._id) && !board.hasCommentOnly(this._id) + ); }, isCommentOnly() { @@ -301,32 +325,32 @@ Users.helpers({ }, starredBoards() { - const {starredBoards = []} = this.profile || {}; - return Boards.find({archived: false, _id: {$in: starredBoards}}); + 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 || {}; - return Boards.find({archived: false, _id: {$in: invitedBoards}}); + 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); }, @@ -336,20 +360,20 @@ Users.helpers({ }, getEmailBuffer() { - const {emailBuffer = []} = this.profile || {}; + const { emailBuffer = [] } = this.profile || {}; return emailBuffer; }, getInitials() { const profile = this.profile || {}; - if (profile.initials) - return profile.initials; - + if (profile.initials) return profile.initials; else if (profile.fullname) { - return profile.fullname.split(/\s+/).reduce((memo, word) => { - return memo + word[0]; - }, '').toUpperCase(); - + return profile.fullname + .split(/\s+/) + .reduce((memo, word) => { + return memo + word[0]; + }, '') + .toUpperCase(); } else { return this.username[0].toUpperCase(); } @@ -379,7 +403,7 @@ Users.helpers({ }, remove() { - User.remove({ _id: this._id}); + User.remove({ _id: this._id }); }, }); @@ -426,10 +450,8 @@ Users.mutations({ }, toggleTag(tag) { - if (this.hasTag(tag)) - this.removeTag(tag); - else - this.addTag(tag); + if (this.hasTag(tag)) this.removeTag(tag); + else this.addTag(tag); }, toggleSystem(value = false) { @@ -473,16 +495,16 @@ Users.mutations({ }, setAvatarUrl(avatarUrl) { - return {$set: {'profile.avatarUrl': avatarUrl}}; + return { $set: { 'profile.avatarUrl': avatarUrl } }; }, setShowCardsCountAt(limit) { - return {$set: {'profile.showCardsCountAt': limit}}; + return { $set: { 'profile.showCardsCountAt': limit } }; }, setBoardView(view) { return { - $set : { + $set: { 'profile.boardView': view, }, }; @@ -492,11 +514,11 @@ Users.mutations({ Meteor.methods({ setUsername(username, userId) { check(username, String); - const nUsersWithUsername = Users.find({username}).count(); + const nUsersWithUsername = Users.find({ username }).count(); if (nUsersWithUsername > 0) { throw new Meteor.Error('username-already-taken'); } else { - Users.update(userId, {$set: {username}}); + Users.update(userId, { $set: { username } }); } }, toggleSystemMessages() { @@ -509,16 +531,21 @@ Meteor.methods({ }, setEmail(email, userId) { 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) { throw new Meteor.Error('email-already-taken'); } else { Users.update(userId, { $set: { - emails: [{ - address: email, - verified: false, - }], + emails: [ + { + address: email, + verified: false, + }, + ], }, }); } @@ -533,7 +560,7 @@ Meteor.methods({ setPassword(newPassword, userId) { check(userId, String); check(newPassword, String); - if(Meteor.user().isAdmin){ + if (Meteor.user().isAdmin) { Accounts.setPassword(userId, newPassword); } }, @@ -548,12 +575,13 @@ if (Meteor.isServer) { const inviter = Meteor.user(); const board = Boards.findOne(boardId); - const allowInvite = inviter && + const allowInvite = + inviter && board && board.members && _.contains(_.pluck(board.members, 'userId'), inviter._id) && - _.where(board.members, {userId: inviter._id})[0].isActive && - _.where(board.members, {userId: inviter._id})[0].isAdmin; + _.where(board.members, { userId: inviter._id })[0].isActive && + _.where(board.members, { userId: inviter._id })[0].isAdmin; if (!allowInvite) throw new Meteor.Error('error-board-notAMember'); this.unblock(); @@ -561,19 +589,21 @@ if (Meteor.isServer) { const posAt = username.indexOf('@'); let user = null; if (posAt >= 0) { - user = Users.findOne({emails: {$elemMatch: {address: username}}}); + user = Users.findOne({ emails: { $elemMatch: { address: username } } }); } else { - user = Users.findOne(username) || Users.findOne({username}); + user = Users.findOne(username) || Users.findOne({ username }); } 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'); } else { if (posAt <= 0) throw new Meteor.Error('error-user-doesNotExist'); - if (Settings.findOne().disableRegistration) throw new Meteor.Error('error-user-notCreated'); + if (Settings.findOne().disableRegistration) + throw new Meteor.Error('error-user-notCreated'); // Set in lowercase email before creating account const email = username.toLowerCase(); 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'); // assume new user speak same language with inviter if (inviter.profile && inviter.profile.language) { @@ -607,7 +637,7 @@ if (Meteor.isServer) { } catch (e) { 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) => { @@ -621,14 +651,22 @@ if (Meteor.isServer) { const email = user.services.oidc.email.toLowerCase(); user.username = user.services.oidc.username; user.emails = [{ address: email, verified: true }]; - const initials = user.services.oidc.fullname.match(/\b[a-zA-Z]/g).join('').toUpperCase(); - user.profile = { initials, fullname: user.services.oidc.fullname, boardView: 'board-view-lists' }; + const initials = user.services.oidc.fullname + .match(/\b[a-zA-Z]/g) + .join('') + .toUpperCase(); + user.profile = { + initials, + fullname: user.services.oidc.fullname, + boardView: 'board-view-lists', + }; user.authenticationMethod = 'oauth2'; // see if any existing user has this email address or username, otherwise create new - const existingUser = Meteor.users.findOne({$or: [{'emails.address': email}, {'username':user.username}]}); - if (!existingUser) - return user; + const existingUser = Meteor.users.findOne({ + $or: [{ 'emails.address': email }, { username: user.username }], + }); + if (!existingUser) return user; // copy across new service info const service = _.keys(user.services)[0]; @@ -638,7 +676,7 @@ if (Meteor.isServer) { existingUser.profile = user.profile; existingUser.authenticationMethod = user.authenticationMethod; - Meteor.users.remove({_id: existingUser._id}); // remove existing record + Meteor.users.remove({ _id: existingUser._id }); // remove existing record return existingUser; } @@ -660,7 +698,10 @@ if (Meteor.isServer) { } 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, @@ -668,26 +709,41 @@ if (Meteor.isServer) { valid: true, }); 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 { - user.profile = {icode: options.profile.invitationcode}; + user.profile = { icode: options.profile.invitationcode }; user.profile.boardView = 'board-view-lists'; // Deletes the invitation code after the user was created successfully. - setTimeout(Meteor.bindEnvironment(() => { - InvitationCodes.remove({'_id': invitationCode._id}); - }), 200); + setTimeout( + Meteor.bindEnvironment(() => { + InvitationCodes.remove({ _id: invitationCode._id }); + }), + 200 + ); return user; } }); } +Users.before.update((userId, doc, fieldNames, modifier, options) => { + modifier.$set = modifier.$set || {}; + modifier.$set.modifiedAt = Date.now(); +}); + if (Meteor.isServer) { // Let mongoDB ensure username unicity Meteor.startup(() => { - Users._collection._ensureIndex({ - username: 1, - }, {unique: true}); + Users._collection._ensureIndex({ modifiedAt: -1 }); + Users._collection._ensureIndex( + { + username: 1, + }, + { unique: true } + ); }); // OLD WAY THIS CODE DID WORK: When user is last admin of board, @@ -712,11 +768,10 @@ if (Meteor.isServer) { // counter. // We need to run this code on the server only, otherwise the incrementation // will be done twice. - Users.after.update(function (userId, user, fieldNames) { + Users.after.update(function(userId, user, fieldNames) { // The `starredBoards` list is hosted on the `profile` field. If this // field hasn't been modificated we don't need to run this hook. - if (!_.contains(fieldNames, 'profile')) - return; + if (!_.contains(fieldNames, 'profile')) return; // To calculate a diff of board starred ids, we get both the previous // and the newly board ids list @@ -732,7 +787,7 @@ if (Meteor.isServer) { // direction and then in the other. function incrementBoards(boardsIds, inc) { boardsIds.forEach((boardId) => { - Boards.update(boardId, {$inc: {stars: inc}}); + Boards.update(boardId, { $inc: { stars: inc } }); }); } @@ -754,7 +809,7 @@ if (Meteor.isServer) { }; fakeUserId.withValue(doc._id, () => { - /* + /* // Insert the Welcome Board Boards.insert({ title: TAPi18n.__('welcome-board'), @@ -773,57 +828,76 @@ if (Meteor.isServer) { }); */ - Boards.insert({ - title: TAPi18n.__('templates'), - permission: 'private', - type: 'template-container', - }, fakeUser, (err, boardId) => { - - // Insert the reference to our templates board - Users.update(fakeUserId.get(), {$set: {'profile.templatesBoardId': boardId}}); - - // Insert the card templates swimlane - Swimlanes.insert({ - title: TAPi18n.__('card-templates-swimlane'), - boardId, - sort: 1, - type: 'template-container', - }, fakeUser, (err, swimlaneId) => { - - // Insert the reference to out card templates swimlane - Users.update(fakeUserId.get(), {$set: {'profile.cardTemplatesSwimlaneId': swimlaneId}}); - }); - - // Insert the list templates swimlane - Swimlanes.insert({ - title: TAPi18n.__('list-templates-swimlane'), - boardId, - sort: 2, - type: 'template-container', - }, fakeUser, (err, swimlaneId) => { - - // Insert the reference to out list templates swimlane - Users.update(fakeUserId.get(), {$set: {'profile.listTemplatesSwimlaneId': swimlaneId}}); - }); - - // Insert the board templates swimlane - Swimlanes.insert({ - title: TAPi18n.__('board-templates-swimlane'), - boardId, - sort: 3, + Boards.insert( + { + title: TAPi18n.__('templates'), + permission: 'private', type: 'template-container', - }, fakeUser, (err, swimlaneId) => { - - // Insert the reference to out board templates swimlane - Users.update(fakeUserId.get(), {$set: {'profile.boardTemplatesSwimlaneId': swimlaneId}}); - }); - }); + }, + fakeUser, + (err, boardId) => { + // Insert the reference to our templates board + Users.update(fakeUserId.get(), { + $set: { 'profile.templatesBoardId': boardId }, + }); + + // Insert the card templates swimlane + Swimlanes.insert( + { + title: TAPi18n.__('card-templates-swimlane'), + boardId, + sort: 1, + type: 'template-container', + }, + fakeUser, + (err, swimlaneId) => { + // Insert the reference to out card templates swimlane + Users.update(fakeUserId.get(), { + $set: { 'profile.cardTemplatesSwimlaneId': swimlaneId }, + }); + } + ); + + // Insert the list templates swimlane + Swimlanes.insert( + { + title: TAPi18n.__('list-templates-swimlane'), + boardId, + sort: 2, + type: 'template-container', + }, + fakeUser, + (err, swimlaneId) => { + // Insert the reference to out list templates swimlane + Users.update(fakeUserId.get(), { + $set: { 'profile.listTemplatesSwimlaneId': swimlaneId }, + }); + } + ); + + // Insert the board templates swimlane + Swimlanes.insert( + { + title: TAPi18n.__('board-templates-swimlane'), + boardId, + sort: 3, + type: 'template-container', + }, + fakeUser, + (err, swimlaneId) => { + // Insert the reference to out board templates swimlane + Users.update(fakeUserId.get(), { + $set: { 'profile.boardTemplatesSwimlaneId': swimlaneId }, + }); + } + ); + } + ); }); }); } Users.after.insert((userId, doc) => { - if (doc.createdThroughApi) { // The admin user should be able to create a user despite disabling registration because // it is two different things (registration and creation). @@ -831,7 +905,7 @@ if (Meteor.isServer) { // the disableRegistration check. // Issue : https://github.com/wekan/wekan/issues/1232 // PR : https://github.com/wekan/wekan/pull/1251 - Users.update(doc._id, {$set: {createdThroughApi: ''}}); + Users.update(doc._id, { $set: { createdThroughApi: '' } }); return; } @@ -840,7 +914,10 @@ if (Meteor.isServer) { // If ldap, bypass the inviation code if the self registration isn't allowed. // TODO : pay attention if ldap field in the user model change to another content ex : ldap field to connection_type if (doc.authenticationMethod !== 'ldap' && disableRegistration) { - const invitationCode = InvitationCodes.findOne({code: doc.profile.icode, valid: true}); + const invitationCode = InvitationCodes.findOne({ + code: doc.profile.icode, + valid: true, + }); if (!invitationCode) { throw new Meteor.Error('error-invitation-code-not-exist'); } else { @@ -852,8 +929,8 @@ if (Meteor.isServer) { doc.profile = {}; } doc.profile.invitedBoards = invitationCode.boardsToBeInvited; - Users.update(doc._id, {$set: {profile: doc.profile}}); - InvitationCodes.update(invitationCode._id, {$set: {valid: false}}); + Users.update(doc._id, { $set: { profile: doc.profile } }); + InvitationCodes.update(invitationCode._id, { $set: { valid: false } }); } } }); @@ -862,13 +939,12 @@ if (Meteor.isServer) { // USERS REST API if (Meteor.isServer) { // Middleware which checks that API is enabled. - JsonRoutes.Middleware.use(function (req, res, next) { + JsonRoutes.Middleware.use(function(req, res, next) { const api = req.url.search('api'); - if (api === 1 && process.env.WITH_API === 'true' || api === -1){ + if ((api === 1 && process.env.WITH_API === 'true') || api === -1) { return next(); - } - else { - res.writeHead(301, {Location: '/'}); + } else { + res.writeHead(301, { Location: '/' }); return res.end(); } }); @@ -882,14 +958,13 @@ if (Meteor.isServer) { JsonRoutes.add('GET', '/api/user', function(req, res) { try { Authentication.checkLoggedIn(req.userId); - const data = Meteor.users.findOne({ _id: req.userId}); + const data = Meteor.users.findOne({ _id: req.userId }); delete data.services; JsonRoutes.sendResult(res, { code: 200, data, }); - } - catch (error) { + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -906,17 +981,16 @@ if (Meteor.isServer) { * @return_type [{ _id: string, * username: string}] */ - JsonRoutes.add('GET', '/api/users', function (req, res) { + JsonRoutes.add('GET', '/api/users', function(req, res) { try { Authentication.checkUserId(req.userId); JsonRoutes.sendResult(res, { code: 200, - data: Meteor.users.find({}).map(function (doc) { + data: Meteor.users.find({}).map(function(doc) { return { _id: doc._id, username: doc.username }; }), }); - } - catch (error) { + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -934,7 +1008,7 @@ if (Meteor.isServer) { * @param {string} userId the user ID * @return_type Users */ - JsonRoutes.add('GET', '/api/users/:userId', function (req, res) { + JsonRoutes.add('GET', '/api/users/:userId', function(req, res) { try { Authentication.checkUserId(req.userId); const id = req.params.userId; @@ -942,8 +1016,7 @@ if (Meteor.isServer) { code: 200, data: Meteor.users.findOne({ _id: id }), }); - } - catch (error) { + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -968,7 +1041,7 @@ if (Meteor.isServer) { * @return_type {_id: string, * title: string} */ - JsonRoutes.add('PUT', '/api/users/:userId', function (req, res) { + JsonRoutes.add('PUT', '/api/users/:userId', function(req, res) { try { Authentication.checkUserId(req.userId); const id = req.params.userId; @@ -990,8 +1063,16 @@ if (Meteor.isServer) { }; }); } else { - if ((action === 'disableLogin') && (id !== req.userId)) { - Users.update({ _id: id }, { $set: { loginDisabled: true, 'services.resume.loginTokens': '' } }); + if (action === 'disableLogin' && id !== req.userId) { + Users.update( + { _id: id }, + { + $set: { + loginDisabled: true, + 'services.resume.loginTokens': '', + }, + } + ); } else if (action === 'enableLogin') { Users.update({ _id: id }, { $set: { loginDisabled: '' } }); } @@ -1002,8 +1083,7 @@ if (Meteor.isServer) { code: 200, data, }); - } - catch (error) { + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -1030,13 +1110,16 @@ if (Meteor.isServer) { * @return_type {_id: string, * title: string} */ - JsonRoutes.add('POST', '/api/boards/:boardId/members/:userId/add', function (req, res) { + JsonRoutes.add('POST', '/api/boards/:boardId/members/:userId/add', function( + req, + res + ) { try { Authentication.checkUserId(req.userId); const userId = req.params.userId; const boardId = req.params.boardId; const action = req.body.action; - const {isAdmin, isNoComments, isCommentOnly} = req.body; + const { isAdmin, isNoComments, isCommentOnly } = req.body; let data = Meteor.users.findOne({ _id: userId }); if (data !== undefined) { if (action === 'add') { @@ -1045,10 +1128,16 @@ if (Meteor.isServer) { }).map(function(board) { if (!board.hasMember(userId)) { board.addMember(userId); - function isTrue(data){ + function isTrue(data) { return data.toLowerCase() === 'true'; } - board.setMemberPermission(userId, isTrue(isAdmin), isTrue(isNoComments), isTrue(isCommentOnly), userId); + board.setMemberPermission( + userId, + isTrue(isAdmin), + isTrue(isNoComments), + isTrue(isCommentOnly), + userId + ); } return { _id: board._id, @@ -1061,8 +1150,7 @@ if (Meteor.isServer) { code: 200, data: query, }); - } - catch (error) { + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -1084,40 +1172,43 @@ if (Meteor.isServer) { * @return_type {_id: string, * title: string} */ - JsonRoutes.add('POST', '/api/boards/:boardId/members/:userId/remove', function (req, res) { - try { - Authentication.checkUserId(req.userId); - const userId = req.params.userId; - const boardId = req.params.boardId; - const action = req.body.action; - let data = Meteor.users.findOne({ _id: userId }); - if (data !== undefined) { - if (action === 'remove') { - data = Boards.find({ - _id: boardId, - }).map(function(board) { - if (board.hasMember(userId)) { - board.removeMember(userId); - } - return { - _id: board._id, - title: board.title, - }; - }); + JsonRoutes.add( + 'POST', + '/api/boards/:boardId/members/:userId/remove', + function(req, res) { + try { + Authentication.checkUserId(req.userId); + const userId = req.params.userId; + const boardId = req.params.boardId; + const action = req.body.action; + let data = Meteor.users.findOne({ _id: userId }); + if (data !== undefined) { + if (action === 'remove') { + data = Boards.find({ + _id: boardId, + }).map(function(board) { + if (board.hasMember(userId)) { + board.removeMember(userId); + } + return { + _id: board._id, + title: board.title, + }; + }); + } } + JsonRoutes.sendResult(res, { + code: 200, + data: query, + }); + } catch (error) { + JsonRoutes.sendResult(res, { + code: 200, + data: error, + }); } - JsonRoutes.sendResult(res, { - code: 200, - data: query, - }); } - catch (error) { - JsonRoutes.sendResult(res, { - code: 200, - data: error, - }); - } - }); + ); /** * @operation new_user @@ -1131,7 +1222,7 @@ if (Meteor.isServer) { * @param {string} password the password of the new user * @return_type {_id: string} */ - JsonRoutes.add('POST', '/api/users/', function (req, res) { + JsonRoutes.add('POST', '/api/users/', function(req, res) { try { Authentication.checkUserId(req.userId); const id = Accounts.createUser({ @@ -1146,8 +1237,7 @@ if (Meteor.isServer) { _id: id, }, }); - } - catch (error) { + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -1165,7 +1255,7 @@ if (Meteor.isServer) { * @param {string} userId the ID of the user to delete * @return_type {_id: string} */ - JsonRoutes.add('DELETE', '/api/users/:userId', function (req, res) { + JsonRoutes.add('DELETE', '/api/users/:userId', function(req, res) { try { Authentication.checkUserId(req.userId); const id = req.params.userId; @@ -1176,8 +1266,7 @@ if (Meteor.isServer) { _id: id, }, }); - } - catch (error) { + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -1185,3 +1274,5 @@ if (Meteor.isServer) { } }); } + +export default Users; |