usersPermissionsDataSourceMySQLi
extends usersDataSourceMySQLi
in package
usersDataSource class
usersDataSource provides the data access capabilities for the Users class.
To build a data access class for Users, the following 5 methods must exist. getPassword(username) getAllUsers() getARecord update
Additionally, where appropriate, the following function is useful buildTable()
This particular instance provides an interface to MySQL using the mysqli libraries.
Create an instance of this, then pass the variable to several Users calls.
Table of Contents
- $configuration : array<string|int, string>
- $dbConnection : mysqli
- __construct() : null
- extends usersDataSource
- addPermission() : mixed
- Adds a permission to the permissions table
- buildTable() : mixed
- Function will build tables when called
- getAllUsers() : array<string|int, string>
- retrieves all users from the database
- getARecord() : array<string|int, string>
- Get a record from the database
- getFullPermissions() : mixed
- Gets all permissions with names, group names, etc... from database
- getPassword() : array<string|int, string>
- Retrieves the password field from table
- getPermissions() : mixed
- Gets permissions for one user
- getUserID() : int
- retrieves the userID from the table
- initTables() : mixed
- Convenience function to initialize tables to values
- setUsersPermissions() : mixed
- Sets any unset user permissions
- test() : bool
- Tests that the database connection works and the table is built
- update() : mysqli_result|bool
- updates row in database with $newData
- updatePermissions() : mixed
- Adds/Updates a group of permissions for a user
- buildQuery() : string
- Create a query to retrieve info from database
- doSQL() : mixed
- Executes an SQL statement, returning the result
- escapeString() : string
- Make string safe for MySQL
- getAField() : mixed
- Gets a single field from a table
- setDBConnection() : mysqli|false
- Make the database connection
- tableColumnName() : string
- Gets the actual database column name from the configuration file
Properties
$configuration
protected
array<string|int, string>
$configuration
= array()
Contains the configuration for the class
May be modified by the calling program. Must be replicated in userDataSource class
$dbConnection
protected
mysqli
$dbConnection
= alse
Holds the mysqli database connection
Methods
__construct()
extends usersDataSource
public
__construct(mysqli $connection[, array<string|int, string> $customFields = array() ]) : null
Adds the new fields needed for permissions. First, creates array $permissions, then does a recursive merge into usersDataSource::$configuration. NOTE: we must merge this before we merge in $dbDef so the developer can modify permissions also
Parameters
- $connection : mysqli
-
Existing mysqli database connection
- $customFields : array<string|int, string> = array()
-
Array to be merged with $configuration
Return values
null —addPermission()
Adds a permission to the permissions table
public
addPermission(mixed $category, mixed $name, mixed $description, mixed $defaultValue) : mixed
This simply adds a permission to the permissions table, adding the category if it doesn't exist already.
Parameters
- $category : mixed
- $name : mixed
- $description : mixed
- $defaultValue : mixed
Tags
Return values
mixed —buildTable()
Function will build tables when called
public
buildTable() : mixed
Calls parent::buildTable first, then creates a view. The query for the view is created using sprintf so we can allow the developers to modify the table names, etc...
Return values
mixed —getAllUsers()
retrieves all users from the database
public
abstract getAllUsers() : array<string|int, string>
Retrieves all data for all users from table
Return values
array<string|int, string> —array of array of rows/columns
getARecord()
Get a record from the database
public
getARecord(mixed $whereFields[, mixed $fieldList = null ]) : array<string|int, string>
Gets a single record from the database which matches $field containing $username. If more than one record is returned, will return the first one
Parameters
- $whereFields : mixed
-
column=>value pairs for where clause
- $fieldList : mixed = null
-
a list of columns to return. If empty, returns all columns
Return values
array<string|int, string> —a hash containing fieldname=>value pairs from fetch_assoc
getFullPermissions()
Gets all permissions with names, group names, etc... from database
public
getFullPermissions(int $id) : mixed
NOTE: if $id is -1, indicating a new user, simply retrieves all permissions from the permissions table with the default value
WARNING: this uses the view, which is not as flexible as the tables as far as modifying programmatically
Parameters
- $id : int
-
the user id of the record we want.
Tags
Return values
mixed —getPassword()
Retrieves the password field from table
public
abstract getPassword(string $username) : array<string|int, string>
Note that the password is stored as a hash in the table
Parameters
- $username : string
-
username used to find record
Return values
array<string|int, string> —an array of values key/value pairs
getPermissions()
Gets permissions for one user
public
getPermissions(int $id) : mixed
NOTE: if $id is -1, indicating a new user, simply retrieves all permissions from the permissions table with the default value
WARNING: this uses the view, which is not as flexible as the tables as far as modifying programmatically
The return value is an associative array, where the key is the permission (short) name and the value is 0 or 1 (boolean true/false)
Parameters
- $id : int
-
the user id of the record we want.
Tags
Return values
mixed —getUserID()
retrieves the userID from the table
public
getUserID(string $username) : int
Parameters
- $username : string
Return values
int —user id
initTables()
Convenience function to initialize tables to values
public
initTables(array<string|int, string> $initValues) : mixed
Parameters
- $initValues : array<string|int, string>
-
Array of tablenames, column names and values
Return values
mixed —setUsersPermissions()
Sets any unset user permissions
public
setUsersPermissions([mixed $user_id = null ]) : mixed
This will set any missing permissions which have a true value in the users permissions join table. This will NOT modify any existing records but will add new records if they do not exist in the table.
Parameters
- $user_id : mixed = null
Tags
Return values
mixed —test()
Tests that the database connection works and the table is built
public
test() : bool
Return values
bool —True if table exists (does not verify columns)
update()
updates row in database with $newData
public
abstract update(array<string|int, string> $newData) : mysqli_result|bool
Parameters
- $newData : array<string|int, string>
-
fieldname/value pairs to be updated in table
Return values
mysqli_result|bool —The mysqli result from a query
updatePermissions()
Adds/Updates a group of permissions for a user
public
updatePermissions(int $userID, array<string|int, string> $newData) : mixed
Parameters
- $userID : int
-
The user ID to update
- $newData : array<string|int, string>
-
An array where the key is the field name and the value is the new value to use
Return values
mixed —buildQuery()
Create a query to retrieve info from database
protected
buildQuery(mixed $whereFields[, array<string|int, string> $fieldList = null ]) : string
Builds a query to retrieve records from the database. With all parameters set to null, will retrieve all columns and records Setting $field and $toFind create a where clause, and setting $fieldList as a has (ie, 'fieldname' => 1) will limit the fields returned
Parameters
- $whereFields : mixed
- $fieldList : array<string|int, string> = null
-
a hash where the keys make a list of columns to return. If empty, returns all columns
Return values
string —A cleaned and formatted SQL Query
doSQL()
Executes an SQL statement, returning the result
protected
doSQL(mixed $query[, mixed $comment = '' ]) : mixed
This simply runs mysqli::query, and returns the value of that
Created for testing and debugging, if the second parameter is true, will NOT execute the query, but will instead display the query passed.
Parameters
- $query : mixed
- $comment : mixed = ''
Tags
Return values
mixed —escapeString()
Make string safe for MySQL
protected
escapeString(string $string) : string
If the string is completely numeric, returns it, otherwise puts single quotes around it
Parameters
- $string : string
-
The string to be fixed
Return values
string —A copy of the string, ready for SQL
getAField()
Gets a single field from a table
protected
getAField(mixed $tableName, mixed $returnColumn, mixed $fieldName, mixed $value) : mixed
Builds a query similar to select $returnColumn from $tableName where $fieldName = $value executes it, and returns the first column of the first row returned, or null if it does not exist.
Parameters
- $tableName : mixed
- $returnColumn : mixed
- $fieldName : mixed
- $value : mixed
Tags
Return values
mixed —setDBConnection()
Make the database connection
protected
setDBConnection(array<string|int, string> $parameters) : mysqli|false
Parameters
- $parameters : array<string|int, string>
-
Parameters for makeing the connection
Return values
mysqli|false —tableColumnName()
Gets the actual database column name from the configuration file
protected
tableColumnName(string $table[, string $field = '' ][, bool $fullTableColumn = false ]) : string
Since we use a lot of indirection, this is a handy function which allows us to replace something like $this->configuration['tables']['users']['fields']['name']['dbColumn'] with $this->tableColumnName( 'users', 'name' )
If called with only one parameter (the table), will return the actual database table name
Parameters
- $table : string
-
Name of Table
- $field : string = ''
-
Name of field in $table
- $fullTableColumn : bool = false
-
If set to true, will return table.column format
Return values
string —The actual name of the dbColumn in the table