ApiKeys extends AppModel
API Key management
Table of Contents
Methods
| __construct() | Initialize the API Keys |
| add() | Adds a new API key for the given company ID and user. |
| auth() | Authenticates the given credentials and returns the company ID the API user has access to. |
| delete() | Permanently removes an API key |
| edit() | Updates an API key |
| generateKey() | Generates an API key using the company ID as a seed. Not intended to be invoked independently. See ApiKeys::add(). |
| get() | Fetches the API key information for the given user |
| getList() | Returns a list of API keys |
| getListCount() | Returns a count of the number of results in a list of API keys |
| validateUniqueUser() | Validates the given user is unique across all API keys for the given company |
| getRules() | Rules to validate when adding an API key |
| keys() | Builds a partial query to fetch a list of API keys |
Methods
__construct()
Initialize the API Keys
public
__construct() : mixed
add()
Adds a new API key for the given company ID and user.
public
add(array<string|int, mixed> $vars) : mixed
Parameters
- $vars : array<string|int, mixed>
-
An array of API credential information including:
- company_id The ID of the company to add the API key for
- user The user to use as the API user
auth()
Authenticates the given credentials and returns the company ID the API user has access to.
public
auth(string $user, string $key) : int
Parameters
- $user : string
-
The API user
- $key : string
-
The API user's key
Return values
int —The ID of the company the user belongs to, void if the credentials are invalid. Raises Input::errors() on error.
delete()
Permanently removes an API key
public
delete(int $id) : mixed
Parameters
- $id : int
-
The ID of the API key to delete
edit()
Updates an API key
public
edit(int $id, array<string|int, mixed> $vars) : mixed
Parameters
- $id : int
-
The ID of the API key to edit
- $vars : array<string|int, mixed>
-
An array of API key data to update including:
- notes Notes about this key
- user The username of the API key
- company_id The ID of the company the API key belongs to
generateKey()
Generates an API key using the company ID as a seed. Not intended to be invoked independently. See ApiKeys::add().
public
generateKey(string $key, int $company_id) : string
Parameters
- $key : string
-
The variable to set the key into
- $company_id : int
-
The ID of the company to generate the key for
Tags
Return values
string —The generated key for the given company ID
get()
Fetches the API key information for the given user
public
get(int $id) : mixed
Parameters
- $id : int
-
The ID of the key to fetch
Return values
mixed —A stdClass object representing the API key, false if no such key exists
getList()
Returns a list of API keys
public
getList([int $page = 1 ][, array<string|int, mixed> $order_by = ['date_created' => 'desc'] ]) : array<string|int, mixed>
Parameters
- $page : int = 1
-
The page to fetch results on
- $order_by : array<string|int, mixed> = ['date_created' => 'desc']
-
$order_by The sort and order conditions (e.g. ['sort_field' => "ASC"])
Return values
array<string|int, mixed> —An array of stdClass objects
getListCount()
Returns a count of the number of results in a list of API keys
public
getListCount() : int
Return values
int —The number of results in a list of API keys
validateUniqueUser()
Validates the given user is unique across all API keys for the given company
public
validateUniqueUser(string $user, int $company_id[, int $api_id = null ]) : bool
Parameters
- $user : string
-
The user to be validated against the given company
- $company_id : int
-
The company ID to validate uniqueness across
- $api_id : int = null
-
The ID of the API key (if given) to exclude from the uniqueness test
Return values
bool —True if the user is unique for the given company (besides this $api_id), false otherwise
getRules()
Rules to validate when adding an API key
private
getRules(array<string|int, mixed> $vars) : array<string|int, mixed>
Parameters
- $vars : array<string|int, mixed>
-
An array of input fields to validate against
Return values
array<string|int, mixed> —Rules to validate
keys()
Builds a partial query to fetch a list of API keys
private
keys() : Record