Skip to content
kip9 edited this page Feb 5, 2014 · 1 revision
Model Extends Table Path
Tokens_Model Plain_Model tokens /application/models/tokens_model.php

Loading the model

$this->load->model('tokens_model', 'tokens');

Constants

Constant Value Description
DEFAULT_TOKEN_VALID_TIME_SECONDS 86400 Default token validity time in seconds
TYPE_FORGOT_PASSWORD FORGOT_PASSWORD Constant defining token type for recovering forgot passwords

Methods

__construct - Public

Called automatically which in turn calls the parent constructor and sets the model $data_types properties.


create - Public

Used to create new records in the tokens table. Currently there is one type of tokens : FORGOT_PASSWORD used to recover forgotten password by user.

Arguments

Variable Type Default Required Description
$options array array() Yes An associative array that contains the column names and the values for the record to be created. Array keys are the column names for each value.

Example

$this->load->model('tokens_model', 'token');
$createdToken = $this->token->create(array(
                    'token_type' => Tokens_model::TYPE_FORGOT_PASSWORD,
                    'user_id' => $user->user_id
                )
);
// If token was created
if (isset($createdToken->token_id)) {
    // Good to go
}

isValid - Public

Checks if given tokenValue exists and is still valid for use. Behaviour depends on argument passed:

  • if it is string (tokenValue), then it runs check directly on database using queries,
  • if argument is array, it assumes object was already retrieved from database and checks it's properties for validity (active flag and valid_until field)

Arguments

Variable Type Default Required Description
$tokenValue string|object Yes Token value as a string or retrieved from database token object.

Example

$this->load->model('tokens_model', 'token');
if ($this->token->isValid('abcdefghijkl')) {
    $this->data['token_valid'] = true;
}

useToken - Public

Mark selected token as used in the database.

Arguments

Variable Type Default Required Description
$tokenValue string Yes Token value as a string.

Example

$this->load->model('tokens_model', 'token');
$token = 'ABCDEFGH';
if (! $this->token->useToken($token)) {
    log_message('DEBUG', 'Failed to mark token ' . $token . ' as used in DB');
}```