     """
 
 
-  checkBlockedStatus = (user, callback)->
-    if user.status is 'blocked'
-      if user.blockedUntil and user.blockedUntil > new Date
-        toDate    = user.blockedUntil.toUTCString()
-        message   = JUser.getBlockedMessage toDate
-
-        callback new KodingError message
-      else
-        user.unblock callback
+  checkBlockedStatus = (user, callback) ->
+
+    return callback null  if user.status isnt 'blocked'
+
+    if user.blockedUntil and user.blockedUntil > new Date
+      toDate    = user.blockedUntil.toUTCString()
+      message   = JUser.getBlockedMessage toDate
+      callback new KodingError message
+
     else
-      callback null
+      user.unblock callback
+
 
   @normalizeLoginId = (loginId, callback) ->
+
     if /@/.test loginId
-      JUser.someData {email: loginId}, {username: 1}, (err, cursor) ->
+      JUser.someData { email: loginId }, { username: 1 }, (err, cursor) ->
         return callback err  if err
 
         cursor.nextObject (err, data) ->

    callback null


  @changeEmail = secure (client,options,callback)->

    {email} = options

    account = client.connection.delegate
    account.fetchUser (err, user)=>
      return callback new KodingError "Something went wrong please try again!" if err
      if email is user.email then return callback new KodingError "EmailIsSameError"
      @emailAvailable email, (err, res)=>
        return callback new KodingError "Something went wrong please try again!" if err
        if res is no
          callback new KodingError "Email is already in use!"
        else
          user.changeEmail account, options, callback


  @emailAvailable = (email, callback)->
    unless typeof email is 'string'
      return callback new KodingError 'Not a valid email!'

    @count {email}, (err, count)-> callback err, count is 0


  @getValidUsernameLengthRange = -> { minLength : 4, maxLength : 25 }


  @usernameAvailable = (username, callback)->

    JName     = require '../name'


      console.log "ALERT: #{username} is trying to confirm '#{status}' email"
      return callback null

    @update { $set: { status: 'confirmed' } }, (err, res) =>
      return callback err if err
      JUser.emit 'EmailConfirmed', this

      callback null

      Tracker.track username, { subject : Tracker.types.FINISH_REGISTER }


  block: (blockedUntil, callback) ->

    unless blockedUntil then return callback new KodingError 'Blocking date is not defined'
    @update
      $set           :
        status       : 'blocked',
        blockedUntil : blockedUntil
    , (err) =>
        return callback err if err
        JUser.emit 'UserBlocked', this
        # clear all of the cookies of the blocked user

        console.log 'JUser#block JSession#remove', { @username, blockedUntil }

        JSession.remove { username: @username }, callback


  unblock: (callback) ->

    @update
      $set            :
        blockedUntil  : new Date()
    , (err) =>
      return callback err if err

      JUser.emit 'UserUnblocked', this
      return callback err


  unlinkOAuths: (callback) ->

    @update $unset: { foreignAuth:1, foreignAuthType:1 }, (err) =>
      return callback err  if err

      @fetchOwnAccount (err, account)->
        return callback err  if err
        account.unstoreAll callback


  @persistOauthInfo: (username, clientId, callback) ->

    @extractOauthFromSession clientId, (err, foreignAuthInfo) =>
      return callback err   if err
      return callback null  unless foreignAuthInfo
      return callback null  unless foreignAuthInfo.session

      @saveOauthToUser foreignAuthInfo, username, (err) =>
        return callback err  if err

        @clearOauthFromSession foreignAuthInfo.session, (err) =>
          return callback err  if err

          @copyPublicOauthToAccount username, foreignAuthInfo, (err, resp = {}) ->
            return callback err  if err
            { session: { returnUrl } } = foreignAuthInfo
            resp.returnUrl = returnUrl  if returnUrl
            return callback null, resp


  @extractOauthFromSession: (clientId, callback) ->

    JSession.one { clientId: clientId }, (err, session) ->
      return callback err   if err
      return callback null  unless session

      { foreignAuth, foreignAuthType } = session
      if foreignAuth and foreignAuthType
        callback null, { foreignAuth, foreignAuthType, session }
      else
        callback null # WARNING: don't assume it's an error if there's no foreignAuth


  @saveOauthToUser: ({foreignAuth, foreignAuthType}, username, callback) ->

    query = {}
    query["foreignAuth.#{foreignAuthType}"] = foreignAuth[foreignAuthType]

    @update { username }, { $set: query }, callback


  @clearOauthFromSession: (session, callback) ->

    session.update { $unset: { foreignAuth:1, foreignAuthType:1 } }, callback


  @copyPublicOauthToAccount: (username, {foreignAuth, foreignAuthType}, callback) ->

    JAccount.one { "profile.nickname" : username }, (err, account) ->
      return callback err  if err

      name    = "ext|profile|#{foreignAuthType}"
      content = foreignAuth[foreignAuthType].profile
      account._store { name, content }, callback


  @setSSHKeys: secure (client, sshKeys, callback) ->

    @fetchUser client, (err,user) ->
      user.sshKeys = sshKeys
      user.save callback


  @getSSHKeys: secure (client, callback) ->

    @fetchUser client, (err,user) ->
      callback user.sshKeys or []


