• The basics

User accounts

Much on the Includable platform has to do with user accounts.

This document lists some code samples related to managing user accounts in your code.

Get a user instance

// Currently signed in user (or null if not signed in):
$user = user();

// By ID:
$user = User::find($user_id);
$user = new User($user_id); // alternative syntax
$user = User::getById($user_id); // alternative syntax

// By email:
$user = User::getByEmail($user_email);

// By a random field:
$user = User::findWith('source', 'external_id');

Get user avatar image

echo $user->getPicture($size = 300);

// Or as field:
echo $user->picture;

Check permissions within a certain community

$community = community(); // get current community
$user = user(); // get current user

// Is the user a member of this community?
  echo 'You belong here!';

// Is the user a community member (admin) of this community?
  echo 'Moreover, you manage this thing!';

Getting and setting user attributes

Also see fields and attributes.

$user = user();

// Setting attributes
$user->attr('my_age', 21);
$user->attr('my_interests', ['walking', 'programming', 'reading']);

// Getting attributes
$age = $user->attr('my_age');

Create a new user account

Using a Community class instance, where community()->users is an instance of Models\Community\Users.

$user_attributes = [
    'email' => 'test@test.com',
    'password_temp' => Login\TokenGenerator::randomToken(),
    'name' => 'Test Test',
    'source' => 'external_id',
    'time_created' => time()

try {
    $user = community()->users->add($user_attributes);
    echo 'Success, new user created with ID: ' . $user->id;
} catch(Models\Community\UserDuplicateEmailException $exception) {
    // Email address already exists
} catch(Models\Community\UserDuplicateUsernameException $exception) {
    // Username already exists
} catch(Models\Community\UserManagementException $exception) {
    // Other type of failure

Database structure

Table users
idINT 255User ID
emailVARCHARUser email address
communityINTCommunity ID in which the user was created initially
sourceVARCHARCustom field (12 chars limit)
time_createdINTCreation timestamp
suspendedINTDefault 0, 1 if suspended
password_tempVARCHARTemporary password, if any
superadminINTInternal field