Reading Data from Existing Users

Getting access to User and UserInfo objects is easy.

Retrieve the User Object for the Logged-In User

This is a common use case: you want to check to see if the current user viewing a page is logged in, from within the context of a block template or a page template. As previously mentioned, you're going to want the Concrete\Core\User\User object for this. Obtaining it for the current user is easy. First import it at the top of your PHP script:

use Concrete\Core\User\User;

Then, instantiate it

$u = new User();

That's it! That gives you the user object for the currently logged in user. From here, you can access all of the methods in the User API

User API Examples

Some of these methods include:

Is the current user logged in?

if ($u->isRegistered()) {
    print 'User is logged in.';
}

Is the current user the root user/super user?

if ($u->isSuperUser()) {
    print 'Yes, they are!';
}

Retrieve the user ID of the current user

print $u->getUserID();

Get the user's groups

$groups = $u->getUserGroups();
foreach($groups as $groupID) {
    $group = \Concrete\Core\User\Group\Group::getByID($groupID);
    print $group->getGroupName();
}

Other methods can be found in the API documentation above.

Retrieve a User Object by ID

If you have a user ID, and you need a User object (perhaps to retrieve the user's groups) you can get it easily:

$user = User::getByUserID(3); // Retrieve the user object for the user with the ID of 3.
$groups = $user->getUserGroups();

Do you want to actually log this user in? Pass true as a second parameter to User::getByUserID();

User::getByUserID(3, true); // Now user 3 is logged in.

Retrieve a UserInfo Object by ID

Need to access custom attributes or other information about a user? You need the UserInfo object. First import it at the top of your PHP script:

use Concrete\Core\User\UserInfo;

Next, retrieve by ID:

$ui = UserInfo::getByID(3);

Now you have a UserInfo object. This object has nothing to do with the current user, authentication, or anything important like that; it's simply a way to gather information about a particular user. There are some other ways to grab a UserInfo object as well.

$andrew = UserInfo::getByName('andrew');
$jane = UserInfo::getByEmail('jane@concretecms.org');

Once you have the UserInfo object, you can access all of the methods in the UserInfo API

UserInfo API Examples

Some of these methods include:

Get the User's Last IP Address

print $ui->getLastIPAddress();

Get Number of Logins

print $ui->getNumLogins();

Get Name, Email

print $ui->getUserEmail();
print $ui->getUserName();

Custom Attributes

The UserInfo object allows you to get custom attributes attached to a user object. For example, by default Concrete CMS ships with a custom attribute named "I would like to receive private messages." It has a handle of "profile_private_messages_enabled". Want to get that setting for a given user? Retrieve the user's UserInfo object, and call this on it.

$response = $ui->getAttribute('profile_private_messages_enabled');

The $response in this case will be the standard response based on the type of attribute. In this case, it'll be a true or a false, since that's what the checkbox attribute returns. However, if you attach an address attribute to a user, you might get a custom address object back instead:

$response = $ui->getAttribute('contact_address');
print $response->getAddress1();
print $response->getCity();

Return Presentable Data

Custom attributes types can return their data in ways that are meant to be presented to the end user. While the custom getAttribute() call returns the data in its mode programmatic state (e.g. returning file objects, address objects, etc…) it's easy to return the data in such a way that it can be quickly printed out to the end user.

$response = $ui->getAttribute('contact_address', 'display');

This will return the address attribute using the address attribute type's getDisplayValue() method, which returns a nicely formatted text paragraph, instead of an object.

User Attribute Magic Methods

The UserInfo object also employs PHP magic methods for attributes. For example, above we retrieve the contact address custom attribute with a getAttribute() call. We could also retrieve it with this:

$addresss = $ui->getUserContactAddress();

This isn't actually a method on the UserInfo object – but any method called on the class that doesn't exist will be passed into UserInfo::getAttribute() automatically.