Users can now be gotten by slug

This commit is contained in:
Ruben Vereecken 2014-07-09 20:23:05 +02:00
parent 928e4362ba
commit ac95d775e6
6 changed files with 35 additions and 3 deletions

View file

@ -76,6 +76,8 @@ module.exports.getByPath = (target, path) ->
obj = obj[piece]
obj
module.exports.isID = (id) -> _.isString(id) and id.length is 24 and id.match(/[a-f0-9]/gi)?.length is 24
module.exports.round = _.curry (digits, n) ->
n = +n.toFixed(digits)

View file

@ -1,6 +1,7 @@
GRAVATAR_URL = 'https://www.gravatar.com/'
cache = {}
CocoModel = require './CocoModel'
util = require 'lib/utils'
module.exports = class User extends CocoModel
@className: 'User'
@ -45,6 +46,28 @@ module.exports = class User extends CocoModel
cache[id] = user
user
# callbacks can be either success or error
@getByIDOrSlug: (idOrSlug, force, callbacks={}) ->
{me} = require 'lib/auth'
isID = util.isID idOrSlug
if me.id is idOrSlug or me.slug is idOrSlug
callbacks.success me if callbacks.success?
return me
cached = cache[idOrSlug]
user = cached or new @ _id: idOrSlug
if force or not cached
user.loading = true
user.fetch
success: ->
user.loading = false
Backbone.Mediator.publish 'user:fetched'
callbacks.success user if callbacks.success?
error: ->
user.loading = false
callbacks.error user if callbacks.error?
cache[idOrSlug] = user
user
getEnabledEmails: ->
@migrateEmails()
emails = _.clone(@get('emails')) or {}

View file

@ -1,8 +1,12 @@
c = require './../schemas'
emailSubscriptions = ['announcement', 'tester', 'level_creator', 'developer', 'article_editor', 'translator', 'support', 'notification']
UserSchema = c.object {},
name: c.shortString({title: 'Display Name', default: ''})
UserSchema = c.object
title: 'User'
c.extendNamedProperties UserSchema # let's have the name be the first property
_.extend UserSchema.properties,
email: c.shortString({title: 'Email', format: 'email'})
firstName: c.shortString({title: 'First Name'})
lastName: c.shortString({title: 'Last Name'})

View file

@ -4,6 +4,7 @@ crypto = require 'crypto'
{salt, isProduction} = require '../../server_config'
mail = require '../commons/mail'
log = require 'winston'
plugins = require '../plugins/plugins'
sendwithus = require '../sendwithus'
@ -136,6 +137,8 @@ UserSchema.statics.hashPassword = (password) ->
shasum.update(salt + password)
shasum.digest('hex')
UserSchema.plugin plugins.NamedPlugin
module.exports = User = mongoose.model('User', UserSchema)
AchievablePlugin = require '../plugins/achievements'

View file

@ -116,7 +116,7 @@ UserHandler = class UserHandler extends Handler
]
getById: (req, res, id) ->
if req.user?._id.equals(id)
if Handler.isID(id) and req.user?._id.equals(id)
return @sendSuccess(res, @formatEntity(req, req.user, 256))
super(req, res, id)

View file