mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-02-17 08:50:58 -05:00
Users can now be gotten by slug
This commit is contained in:
parent
928e4362ba
commit
ac95d775e6
6 changed files with 35 additions and 3 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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 {}
|
||||
|
|
|
@ -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'})
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
0
test/app/models/User.spec.coffee
Normal file
0
test/app/models/User.spec.coffee
Normal file
Loading…
Reference in a new issue