• Developer guides

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.

Database structure

Table users
idINT 255User ID
emailVARCHARUser email address
usernameVARCHARUsername
passwordVARCHARPassword
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

Code samples

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
    dd($exception->getMessage());
} catch(Models\Community\UserDuplicateUsernameException $exception) {
    // Username already exists
    dd($exception->getMessage());
} catch(Models\Community\UserManagementException $exception) {
    // Other type of failure
    dd($exception->getMessage());
}

Get a user

// By ID:
$user = new User($user_id);
$user = User::find($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');

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

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?
if($user->is_connected_to_community($community)){
  echo 'You belong here!';
}

// Is the user a community member (admin) of this community?
if($user->is_manager_of_community($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');