From 5f7ef825f81ef814b3e80949d212526844b8112d Mon Sep 17 00:00:00 2001 From: Ruben Vereecken Date: Thu, 14 Aug 2014 16:38:50 +0200 Subject: [PATCH] Introduced findBySlugOrId to fix a bug --- server/lib/utils.coffee | 3 +++ server/plugins/plugins.coffee | 7 ++++++- server/users/user_handler.coffee | 4 ++-- 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 server/lib/utils.coffee diff --git a/server/lib/utils.coffee b/server/lib/utils.coffee new file mode 100644 index 000000000..d0fd9d09e --- /dev/null +++ b/server/lib/utils.coffee @@ -0,0 +1,3 @@ + +module.exports = + isID: (id) -> _.isString(id) and id.length is 24 and id.match(/[a-f0-9]/gi)?.length is 24 diff --git a/server/plugins/plugins.coffee b/server/plugins/plugins.coffee index 238bcce3d..918055042 100644 --- a/server/plugins/plugins.coffee +++ b/server/plugins/plugins.coffee @@ -1,6 +1,7 @@ mongoose = require('mongoose') textSearch = require('mongoose-text-search') log = require 'winston' +utils = require '../lib/utils' module.exports.MigrationPlugin = (schema, migrations) -> # Property name migrations made EZ @@ -31,9 +32,13 @@ module.exports.NamedPlugin = (schema) -> schema.add({name: String, slug: String}) schema.index({'slug': 1}, {unique: true, sparse: true, name: 'slug index'}) - schema.statics.getBySlug = (slug, done) -> + schema.statics.findBySlug = (slug, done) -> @findOne {slug: slug}, done + schema.statics.findBySlugOrId = (slugOrID, done) -> + return @findById slugOrID, done if utils.isID slugOrID + @findOne {slug: slugOrID}, done + schema.pre('save', (next) -> if schema.uses_coco_versions v = @get('version') diff --git a/server/users/user_handler.coffee b/server/users/user_handler.coffee index 917a43c0c..c87073cd8 100644 --- a/server/users/user_handler.coffee +++ b/server/users/user_handler.coffee @@ -208,7 +208,7 @@ UserHandler = class UserHandler extends Handler @sendSuccess(res, {result: 'success'}) avatar: (req, res, id) -> - @modelClass.findById(id).exec (err, document) => + @modelClass.findBySlugOrId(id).exec (err, document) => return @sendDatabaseError(res, err) if err return @sendNotFoundError(res) unless document photoURL = document?.get('photoURL') @@ -232,7 +232,7 @@ UserHandler = class UserHandler extends Handler IDify: (idOrSlug, done) -> return done null, idOrSlug if Handler.isID idOrSlug - User.getBySlug idOrSlug, (err, user) -> done err, user?.get '_id' + User.findBySlug idOrSlug, (err, user) -> done err, user?.get '_id' getLevelSessions: (req, res, userIDOrSlug) -> @IDify userIDOrSlug, (err, userID) =>