Authentication: The User Object

The User object is used specifically for User authentication. If you're storing any custom information against user accounts in your website/application, including any user attributes, you'll want to access the UserInfo object below.

Getting the Current User Object

$u = new User()

When using the User() constructor, the User object will contain information about the currently logged in user account.

Getting any User Object

$u = User::getByUserID($userID)

Returns a user object for a specific user ID.

Methods

User::getByUserID($userID)

Returns a user object for a specific user ID.

User::loginByUserID($userID)

Replaces the current session with that belonging to $userID, logging that user in.

$u->isRegistered()

Checks to see if the current User object is registered. Useful when using the constructor to grab the user object of the current session.

$u->isLoggedIn()

Checks to see if the current User object is logged in.

$u->checkLogin()

Returns true of the current $u object is logged in, is active. Updates the time last seen online value against the user account.

$u->recordLogin()

Updates the user and records that the user has logged in. Increments their number of logins property.

$u->recordView($page)

Records that the user viewed a given page.

User::encryptPassword($password, $salt = PASSWORD_SALT)

Encrypts as passed $password for storage against the User record.

$u->isActive()

Returns true if the user is active.

$u->isSuperUser()

Returns true if the user objects is the super admin (User ID 1)

$u->getLastOnline()

Gets the timestamp the user was last seen online.

$u->getUserName()

Gets the user's name.

$u->getUserID()

Gets the user's ID.

$u->getUserTimezone()

Gets the user's timezone, if timezone's are being used.

$u->logout();

Destroys a user's session cookie, logging them out.

$groups = $u->getUserGroups()

Returns an associative array of groups, where the key is the group ID, and the value is the group name. These are the groups that user object is in.

$u->enterGroup(Group $g)

When provided a Group object, places the user in that group.

$u->exitGroup(Group $g)

When provided a Group object, removes the user from that group if they are in it.

$u->inGroup(Group $g)

Returns true if the user in the group $g.

$u->loadCollectionEdit($page)

Checks out the page to the user for editing.

$u->unloadCollectionEdit($page)

Checks a page back in.

$u->config($key)

Retrieves a specific configuration $key that has been saved against a specific user record.

$u->saveConfig($key, $value)

Saves a specific configuration key/value pair against a user record.

User Information: The UserInfo Object

While the User object handles base authentication and group membership, the UserInfo object provides access to all the core properties and attributes bound to a particular user object, and allows programmatic addition, update and deletion of user accounts.

Getting the UserInfo object

	$ui = UserInfo::getByID($userID)
$ui = UserInfo::getByUserName($username)
$ui = UserInfo::getByEmail($email)
$ui = UserInfo::getByOpenID($openID); // does not log a user in

All of these methods return a UserInfo object.

Methods for Retrieving Core User Data

User $u = $ui->getUserObject()

Returns a User object from the UserInfo object.

$ui->hasAvatar()

Returns true if the user has an avatar.

$ui->getLastLogin()

Gets the last time a user logged in.

$ui->getPreviousLogin()

Gets the login before that.

$ui->isActive()

Returns true if the user is active.

$ui->isValidated()

Returns 1 if the user's email has been validated, 0 if it has not, and -1 if validation is not required/enabled.

$ui->isFullRecord()

Returns 0 if the user is just an email and not a fully qualified user record.

$ui->getNumLogins()

Gets the number of times a user has logged in.

$ui->getUserEmail()

Returns the user's email address.

$ui->getUserPassword()

Returns the user's encrypted password.

$ui->getUserDateAdded($type = 'system')

Returns the date the user was added. Pass 'user' if you want the date to be translated against the user's timezone (Requires ENABLE_USER_TIMEZONES to be on.)

$ui->getLastOnline($type = 'system')

Returns the date the user was last seen online. Pass 'user' if you want the date to be translated against the user's timezone (Requires ENABLE_USER_TIMEZONES to be on.)

$ui->getUserName()

$ui->getUserID()

$ui->getUserTimezone()

These are the same as their User counterparts.

Get/Set User Attributes

$value = $ui->getAttribute(mixed $ak, $displayMode = false)

Returns the value of a user's attribute based on $ak. $ak may be either an attribute key handle, or a UserAttributeKey object. $displayMode determines which method will be run in the attribute type controller to return the proper display value.

	print $ui->getAttribute('my_rating'); // returns a numerical value
print $ui->getAttribute(' my_rating', 'display'); // outputs nice HTML rating stars.

Additionally, UserInfo objects use a magic method to create automatic getters for all User Attributes. Just simply use the camelcased version of an attribute handle, after "getUser".

print $ui->getUserMyRating();
print $ui->getUserGender(); // returns the value of the attribute with the "gender" handle.
print $ui->getUserDateOfBirth(); // "date_of_birth" handle.

$ui->clearAttribute(mixed $ak)

Deletes the attribute $ak. $ak may be either an attribute key handle, or a UserAttributeKey object. Does not remove the key, simply removes the value as bound to the current User.

$ui->reindex()

Reindexes all user attributes against a given user.

$ui->setAttribute(mixed $ak, $value)

Sets the value of an attribute $ak against particular UserInfo record. $ak may be either an attribute key handle, or a UserAttributeKey object.

AttributeValue $av = $ui->getAttributeValueObject($ak, $createIfNotFound = false)

Returns an AttributeValue object for the current UserInfo/AttributeKey combination.

Add a User

$newUserInfoObject = UserInfo::add($data)

Where $data is an array that can contain

  • uName - the username. Required.
  • uEmail - Email address. Required.
  • uPassword - the non-encrypted password.
  • uIsValidated - Whether the email address has been validated. If not specified, -1 will be saved.
  • uIsFullRecord - Set to 1 unless set in the array to 0.

Updating Users

$ui->update($data)

Where $data is an array that can contain

  • uName - the username. Required.
  • uEmail - Email address. Required.
  • uHasAvatar - 1 if the user has an avatar. 0 if not.
  • uTimezone - the user's timezone.
  • uPassword - the non-encrypted password.

$ui->changePassword($newPassword)

Changes a user's password.

$ui->activate()

Activate a user account.

$ui->deactivate()

Deactivate a user account.

$ui->resetUserPassword()

Resets a user's password, and returns the value of the password.

Deleting Users

$ui->delete()

Deletes a user, including all attributes and group membership. Ownership of blocks and pages is moved to super user. The super user may not be deleted.

Methods for Dealing with Private Messages

As of concrete 5.3, the core includes support for private messaging between users, if user profiles are enabled. The following methods are useful in this regard.

$ui->sendPrivateMessage(UserInfo $recipient, $subject, $text, UserPrivateMessage $inReplyTo = false)

Sends a private message to a user.

$ui->canReadPrivateMessage($userPrivateMessage)

Returns true if the user can read the private message (i.e. their user ID matches.) You can learn more about the UserPrivateMessage object in the Profile Section.


Recent Discussions on this Topic

Can't view current user info

This doesn't seem to say that you have to load a library or anything to get access to the current users info, but I'm not having any luck. I have created a package which is made up of a single library - it's a 3rd party voting script that I want to u…

Different info required for different user groups

We have a client that has several different user groups and those groups require different information. For example, for one group they need the user's zip code, for the other group this isn't needed nor does it need to be required. In order for the fi…

isRegistered or inGroup

It should probably be noted that "Registered Users" aren't added to the "UserGroups" table in the database which the $u->inGroup(Group $g) function checks solely against, therefore even if a user is a member of the "Registered Users" group, if you try to …

Advanced group permission

Hi, I have modified the advanced group permissions in concrete5, so that it's possible to add sub groups (at least one layer), and the sub groups are inheriting the parent group's permissions. Since most of the code was under the /concrete folder, I wa…

How to get the user's address?

I want to display the user's address data, but until so far I only found $ui->getUserAddress(), which displays the whole address. Does anyone know how I can display address1, address2, postal_code etc. seperately?