\User\Model\EntityUserSession

Represents an logged in user.

Summary

Methods
Properties
Constants
updateToken()
isAdmin()
isAllowed()
avatar()
cache()
dropCache()
$id
$roleIds
$roleNames
$roleSlugs
$role_ids
$role_names
$role_slugs
No constants found
_getAvatar()
_setPassword()
_getRoleIds()
_getRoleNames()
_getRoleSlugs()
_getCancelCode()
$_cache
N/A
No private methods found
No private properties found
N/A

Properties

$id

$id : integer

Type

integer

$roleIds

$roleIds : array

Type

array

$roleNames

$roleNames : array

Type

array

$roleSlugs

$roleSlugs : array

Type

array

$role_ids

$role_ids : array

Type

array

$role_names

$role_names : array

Type

array

$role_slugs

$role_slugs : array

Type

array

$_cache

$_cache : array

Used internally.

Type

array

Methods

updateToken()

updateToken() : $this

Updates this user's Token value.

The new token is persisted in DB and in this entity property.

Throws

\Cake\Error\FatalErrorException

When an invalid user entity was given

Returns

$this

isAdmin()

isAdmin() : boolean

Whether this use belongs to the administrator role.

Returns

boolean

isAllowed()

isAllowed(string $aco) : boolean

Verifies this user is allowed access the given ACO.

Usage:

// Checks if current user is allowed to edit created contents:
user()->isAllowed('Content/Admin/Manage/edit');

Parameters

string $aco

An ACO path. e.g. Plugin/Prefix/Controller/action

Returns

boolean —

True if user can access ACO, false otherwise

avatar()

avatar(array $options) : string

Gets user avatar image's URL.

Powered by Gravatar, it uses user's email to get avatar image URL from Gravatar service.

Use this method instead of avatar property when you need to customize avatar's parameters such as size, etc.

$user->avatar(['s' => 150]); // instead of: $user->avatar;

Parameters

array $options

Array of options for Gravatar API

Returns

string —

URL to user's avatar

cache()

cache(null|string $key, mixed $value) : mixed

Reads, writes or search internal class's cache.

Usages:

  • When reading if no cache key is found NULL will be returned. e.g. $null = static::cache('invalid-key');
  • When writing, this method return the value that was written. e.g. $value = static::cache('key', 'value');
  • Set both arguments to NULL to read the whole cache content at the moment. e.g. $allCache = static::cache()
  • Set key to null and value to anything to find the first key holding the given value. e.g. $key = static::cache(null, 'search key for this value'), if no key for the given value is found NULL will be returned.

Examples:

Writing cache:

static::cache('user_name', 'John');
// returns 'John'

static::cache('user_last', 'Locke');
// returns 'Locke'

Reading cache:

static::cache('user_name');
// returns: John

static::cache('unexisting_key');
// returns: null

static::cache();
// Reads the entire cache
// returns: ['user_name' => 'John', 'user_last' => 'Locke']

Searching keys:

static::cache(null, 'Locke');
// returns: user_last

static::cache(null, 'Unexisting Value');
// returns: null

Parameters

null|string $key

Cache key to read or write, set both $key and $value to get the whole cache information

mixed $value

Values to write into the given $key, or null indicates reading from cache

Returns

mixed

dropCache()

dropCache(string|null $key) : void

Drops the entire cache or a specific key.

Usage:

static::dropCache('user_cache'); // removes "user_cache" only
static::dropCache(); // removes every key

Parameters

string|null $key

Cache key to clear, if NULL the entire cache will be erased.

_getAvatar()

_getAvatar() : string

Gets user default-avatar image's URL.

Powered by Gravatar, it uses user's email to get avatar image URL from Gravatar service.

Returns

string —

URL to user's avatar

_setPassword()

_setPassword(string $password) : string

Hashes the password if not empty.

Parameters

string $password

The RAW password

Returns

string —

Encrypted password

_getRoleIds()

_getRoleIds() : array

Gets an array list of role IDs this user belongs to.

Returns

array

_getRoleNames()

_getRoleNames() : array

Gets an array list of role NAMES this user belongs to.

Returns

array

_getRoleSlugs()

_getRoleSlugs() : array

Gets an array list of role NAMES this user belongs to.

Returns

array

_getCancelCode()

_getCancelCode() : string

Generates cancel code for this user.

Throws

\Cake\Error\FatalErrorException

When code cannot be created

Returns

string