Class: User

User

Active record class to represent users of application, with mongodb as backend. This class is ancestor of mongoose active record object, described here http://mongoosejs.com/docs/documents.html, http://mongoosejs.com/docs/queries.html

Constructor

new User()

Properties:
Name Type Description
_id ObjectId
id string
accountVerified Boolean
salt string
password string
keychain object

object to hold all authorization methods- oauth profile ids,emails, usernames...

huntKey string

unique key, used for authorization, invalidating sessions, account confirmation, api interactions...

huntKeyCreatedAt Date
lastSeenOnline Date
lastSeenOnlineAgo Number

how long ago, in seconds, user was online.

isOnline Boolean

true, if user was online less than 60 seconds

displayName string
name object

consists of 'title','familyName', 'middleName', 'givenName'

root Boolean

does user have root permissions? default is false - no

isBanned Boolean

is user banned? default is false - no

roles object

hash (indexed in mongodb) to store user's roles

profile object

free form object, that can store any structured data associated with user account

email string

E-mail address of user, unique. Part of keychain

username string

Username, unique. Part of keychain

gravatar string

link to gravatar image of user with 300x300 pixels size

gravatar30 string

link to gravatar image of user with 30x30 pixels size

gravatar50 string

link to gravatar image of user with 50x50 pixels size

gravatar80 string

link to gravatar image of user with 80x80 pixels size

gravatar100 string

link to gravatar image of user with 100x100 pixels size

Tutorials:
Fires:

Methods

(static) findOneByEmailOrUsername(emailOrUsername, callback)

Parameters:
Name Type Description
emailOrUsername string
callback function

function(err,userFound)

(static) findOneByHuntKey(huntKey, callback)

Parameters:
Name Type Description
huntKey string
callback function

function(err,userFound)

(static) findOneByHuntKeyAndResetPassword(welcomeLink, password, callback)

This function is used for reseting users password by link in email with submitting form later

Parameters:
Name Type Description
welcomeLink string

welcome lint to use

password string

new password to set

callback function

function(error, userFoundAndUpdated) is fired when user is saved

Tutorials:

(static) findOneByHuntKeyAndVerifyEmail(welcomeLink, callback)

Parameters:
Name Type Description
welcomeLink string
callback function

function(error, userSigned){}

Tutorials:

(static) findOneByKeychain(provider, id, callback)

Parameters:
Name Type Description
provider string
id string
callback function

function(err,userFound)

Tutorials:

(static) findOneByWelcomeLinkAndVerifyEmail(welcomeLink, callback)

Parameters:
Name Type Description
welcomeLink string
callback function

function(error, userSigned){}

Tutorials:

(static) findOneFuzzy(idOrEmailOrHuntKey, callback)

Find one user by _id, username,email, huntKey

Parameters:
Name Type Description
idOrEmailOrHuntKey string
callback function

function(err,userFound){...}

Tutorials:

(static) processOAuthProfile(request, profile, callback)

Function to process oauth profile, and if user is authorized, we attach this profile to his keychain If user is not authorized and found, we sign up him/her with accountVerified

This:
Parameters:
Name Type Description
request request

current request (from controllers request)

profile object
callback function

function(err,userAuthorized)

Since:
  • 0.4.18
Tutorials:
See:
Fires:

(static) signIn(email, password, callback)

Parameters:
Name Type Description
email string
password string
callback function

function(error, userSigned){}

Tutorials:
Fires:

(static) signUp(username, email, password, callback)

Parameters:
Name Type Description
username string
email string
password string
callback function

function(error, userSignedUp){}

Tutorials:
Fires:

ban(callback)

This:
Parameters:
Name Type Description
callback function

function(err, userSaved)

getDialog(usernameOrUser, messageLimit, messageOffset, callback)

Get recent messages for dialog with this and user with username in reverse chronological order - the most recent on top

Parameters:
Name Type Description
usernameOrUser User

author of message - string of username/email or user object

messageLimit int

limit of messages

messageOffset int

offset

callback function

function(err,messages) to be called with message object

getGravatar(sizeopt, typeopt, ratingopt) → {string}

Get gravatar for user, based on his/her email or id

Parameters:
Name Type Attributes Default Description
size number <optional>
300

image size in pixels

type string <optional>
'wavatar'

type

rating string <optional>
'g'

image rating, default - suitable for all sites

Returns:

url to gravatar

Type
string

getRecentMessages(messageLimit, messageOffset, callback)

Get recent messages in reverse chronological order - the most recent on top

Parameters:
Name Type Description
messageLimit int

limit of messages

messageOffset int

offset

callback function

function(err,messages) to be called with message object

hasRole(roleName) → {Boolean}

Parameters:
Name Type Description
roleName String

name of the role

Tutorials:
Returns:
  • true, if user has the Role required
Type
Boolean
Example
Hunt.model.User.find({}, function(error, usersFound){
  if(error) {
    throw error;
  } else {
    usersFound.map(function(user){
      if(!user.hasRole('hunter')){
        throw new Error('User #'+user.id +' is not a Hunter! We need to punish him/her!');
      }
    });
  }
});

invalidateSession(callback)

Invalidates session, forcing user to be logged out on every clien by means of regenerating huntkey

This:
Parameters:
Name Type Description
callback function

function(err, newhuntKey)

See:
  • User#huntKey

notify(channelopt, message) → {Boolean}

Send notifications to this user. Note, that notifications ARE not stored in database

This:
Parameters:
Name Type Attributes Default Description
channel string <optional>
"all"

channel- channel to use for notifying users

message object | string
Fires:
Returns:
  • true, if some listener processed the event
Type
Boolean

notifyByEmail(message) → {Boolean}

Send email notification to user

This:
Parameters:
Name Type Description
message object | string
Fires:
Returns:
  • true, if some listener processed the event, false if user do not have an email
Type
Boolean

notifyBySocketIo(message, socketIoEventTypeopt) → {Boolean}

Send socket.io notification to this particular user only.

This:
Parameters:
Name Type Attributes Default Description
message object | string
socketIoEventType string <optional>
'notify:sio'

event name

Fires:
Returns:
  • true, if some listener processed the event
Type
Boolean

receiveMessage(from, message, callback)

Sends private message to this user from other one

Parameters:
Name Type Description
from User/string

receiver of message

message string

text of message

callback function

function to be called on message delivery

Example
User1.sendMessage(User2,'hello!',function(err){if(err) throw err;});
//User1 sends message to User2

revokeKeyChain(provider, callback)

Parameters:
Name Type Description
provider string

provider name, for example - twitter, github, google

callback function

function(err,userSaved)

Tutorials:

sendMessage(to, message, callback)

Sends private message from this user to other one

Parameters:
Name Type Description
to User/string

receiver of message

message string

text of message

callback function

function to be called on message delivery

Example
User1.sendMessage(User2,'hello!',function(err){if(err) throw err;});
//User1 sends message to User2

setKeyChain(provider, id, callback)

Parameters:
Name Type Description
provider string

provider name, for example - twitter, github, google

id string

id of user on this provider

callback function

function(err,userSaved){..}

Tutorials:

unban(callback)

This:
Parameters:
Name Type Description
callback function

function(err, userSaved)