Documentation

CUser extends CDB
in package

User authentication and authorization

Table of Contents

Properties

$all_modules  : array<string|int, mixed>
$all_tables  : array<string|int, mixed>
$cache  : ICache|null
$connection_manager  : IConnectionManager|null
$dbms  : IDBMS
$dbms_type  : string
$last_id  : int
$logger  : ILogger|null
$request  : CRequest
$static_connection_manager  : IConnectionManager|null
$table  : string
$id  : int|null
$name  : string
$role_permissions  : array<string|int, mixed>
$token  : string
$user_roles  : array<string|int, mixed>
$username  : string

Methods

__construct()  : mixed
Constructor
__destruct()  : mixed
Destructor
AddRolePermission()  : void
Set a User Role (Normally detected by DB)
AddUserRole()  : void
Add a Role to the user
Authenticate()  : string
Check if the username/password pair identifies a valid user
BeginTransaction()  : void
Starts a DB transaction.
CanViewAllModules()  : bool
Check if a user can view all modules (as an Administrator)
CheckPermission()  : bool
Check permissions by User Role (Normally detected by DB)
CheckToken()  : bool
CheckValidUser()  : int|null
Check if the username/password pair identifies a valid user
Commit()  : void
Commits the current transaction.
Connect()  : IConnectionManager
Static method to connect to database
ConnectionManager()  : IConnectionManager
Gets the connection manager instance.
ConvertDBType2BindType()  : string
Converts a generic DB data type into a driver-specific bind type.
CreateInstance()  : CUser
Factory method to create a new CUser instance.
CreateToken()  : mixed
Decrypt()  : string
Returns the DB instruction to decrypt a value.
DeleteRecord()  : bool
Delete records from a DB table
Disconnect()  : void
Releases the resource to connect to the DB
DisconnectAll()  : void
Releases all the resources of the previously opened DBs
Encrypt()  : string
Returns the DB instruction to encrypt a value.
Execute()  : void
Executes a previously prepared statement.
ExistsTable()  : bool
Checks if a table exists in the database.
ExistsTableColumns()  : bool
Checks if a list of columns exists in a table.
GetAffectedRows()  : int
Returns how many rows were affected by the last operation performed.
GetAllAvaiableModules()  : array<string|int, mixed>
Returns the list of modules available to the user
GetAllAvailableModules()  : array<string|int, mixed>
Get an array with all module names available for the current user
GetAllModules()  : array<string|int, mixed>
Returns a list of all modules, using cache.
GetAllModulesPermissions()  : array<string|int, mixed>
Get permissions for all visible modules for the current user
GetAllTables()  : array<string|int, mixed>
Returns a list of all tables, using cache.
GetArrayByName()  : array<string|int, mixed>|null
Fetches a result set into a key-value array, using array_column for optimization.
GetArrayItemOfElements()  : array<string|int, mixed>|null
Given an array of records, returns a new array containing only the values of a specific field.
GetAttributesFromType()  : bool
Gets field attributes based on a DB data type.
GetAvailableModuleByTableName()  : string
Get an associative array with boolean "can_read" true if can read the table and with module_name that contains the name of relative module usually used in related tables to display in templates the ability to perform an operation only if it has permissions.
GetCache()  : ICache|null
Returns the cache object
GetCachedArrayByName()  : array<string|int, mixed>|null
Fetches a result set into a key-value array, using cache.
GetCachedDataTable()  : void
Fetches a full result set into an array, using cache.
GetCachedDataValue()  : string|null
Fetches a single data value from a query, using cache.
GetCachedFetchAssoc()  : array<string|int, mixed>|null
Fetches an associative array from a query, using cache.
GetCachedFetchRow()  : array<string|int, mixed>|null
Fetches an associative array from a query, using cache.
GetCachedIDsArray()  : array<string|int, mixed>
Fetches a single column from a result set into an array, using cache.
GetColumnNames()  : array<string|int, string>|null
Returns an array containing the names of all columns for the last query executed.
GetConnectionManager()  : IConnectionManager|null
GetConnectionManager( )
GetCurrentDate()  : string
Returns the current date.
GetCurrentDateTime()  : string
Returns the current date and time.
GetDataTable()  : int|null
Fetches the entire result set of a query into an array.
GetDataValue()  : string|null
Returns a single value from a query result.
GetDbmsType()  : string
Get the name of DBMS used
GetDBResultAssoc()  : int
Returns the driver-specific value for an associative array fetch type.
GetDBResultBoth()  : int
Returns the driver-specific value for a fetch type that includes both associative and numeric arrays.
GetDBResultNum()  : int
Returns the driver-specific value for a numeric array fetch type.
GetDBTable()  : string
Returns the name of the current main table
GetFetchArray()  : array<string|int, mixed>|null
Fetches a result row as an array (both numeric and associative).
GetFetchAssoc()  : array<string|int, mixed>|null
Fetches a result row as an associative array.
GetFetchRow()  : array<string|int, mixed>|null
Fetches a result row as a numeric array.
GetId()  : int|null
GetIDsArray()  : array<string|int, mixed>
Fetches a single column from the last query result into an array.
GetLastDBErrorNumber()  : int
Gets any error number issued by the DB.
GetLastDBErrorString()  : string
Gets any error string issued by the DB.
GetLastID()  : int|null
Returns the ID of the last inserted item
GetName()  : string
GetNextID()  : int|null
Returns the next ID from a sequence or similar.
GetPermissions()  : array<string|int, mixed>
Set a User Role (Normally detected by DB)
GetQueryDataTable()  : int|null
Queries the DB and returns the result in a two-dimensional array.
GetQueryDataValue()  : string|null
Executes a query and fetches a single value.
GetQueryFetchAssoc()  : array<string|int, mixed>|null
Executes a query and fetches a single row as an associative array.
GetQueryFetchRow()  : array<string|int, mixed>|null
Executes a query and fetches a single row as an numeric index array.
GetTableColumnNames()  : array<string|int, string>
Returns an array containing the names of all columns of a table.
GetToken()  : string|null
GetTotalColumns()  : int|bool
Returns the number of columns of a previously executed query.
GetTotalRows()  : int|bool
Reads the total number of rows from a previously executed query.
GetUsername()  : string
GetUserRoles()  : array<string|int, mixed>
Get the list of id__role of current user
HasRole()  : bool
Check if a user have a role
ImportACL()  : bool
This method import automatically all permssions of the current user if exists an table named module_role_permission and the relative tables: role and user_role
InsertRecord()  : bool
Make an entry in the DB
InsertRecordBatch()  : bool
Insert multiple records in a single batch operation
IsAdmin()  : bool
IsAnonymous()  : bool
IsAuthenticated()  : bool
IsOnlyOwnerPermissions()  : bool
Set a User Role (Normally detected by DB)
LoadAllTables()  : void
Loads the schema of all tables into an array.
Logout()  : void
User Logout
Prepare()  : void
Prepares an SQL statement for execution.
Query()  : bool
Executes a query on the DB.
ResetUserRoles()  : void
Reset a User Role
Rollback()  : void
Rolls back the current transaction.
Run()  : string
SetAllModules()  : void
Returns a list of all modules, using cache.
SetAllTables()  : array<string|int, mixed>
Returns a list of all tables, using cache.
SetAutoCommit()  : void
Sets the autocommit mode.
SetDBColumns()  : void
Loads the schema of a table's columns into an array.
SetDBTable()  : void
Sets the name of the reference table for the instance.
SetRolePermissions()  : void
Set a User Role (Normally detected by DB)
SetUserRoles()  : void
Set a list User Roles
UpdateRecord()  : bool
Make an update in the DB
GetCachedData()  : mixed
Retrieves data from the cache.
GetCacheKey()  : string
Generates a unique cache key from a string.
InvalidateRelatedCache()  : void
Invalidates the cache for a given table and its related tables.
SetCachedData()  : void
Stores data in the cache.
GetRoleConditions()  : string
Role Condition for sql query

Properties

$all_modules

protected array<string|int, mixed> $all_modules = []

list of all modules with column_list list

$all_tables

protected array<string|int, mixed> $all_tables = []

list of all tables of current DB with column_list list

$cache

protected ICache|null $cache = null

The cache instance

$connection_manager

protected IConnectionManager|null $connection_manager = null

The connection manager instance

$dbms

protected IDBMS $dbms

The actual DB driver instance

$dbms_type

protected string $dbms_type = ''

name of DBMS for this connection (mysql, oracle, postgresql)

$last_id

protected int $last_id = 0

The last inserted ID

$static_connection_manager

protected static IConnectionManager|null $static_connection_manager = null

Static connection manager for backward compatibility

$table

protected string $table = ''

The default table name for operations

$id

private int|null $id = USER_ID_ANONYMOUS

The user ID (USER_ID_ANONYMOUS for anonymous users)

$name

private string $name = 'Anonymous'

The display name of the user

$role_permissions

private array<string|int, mixed> $role_permissions = []

Role-based permissions [ROLE][PERMISSION]

$token

private string $token = ''

The JWT authentication token

$user_roles

private array<string|int, mixed> $user_roles = []

Current user's role IDs

$username

private string $username = 'nobody'

The username for authentication

Methods

__construct()

Constructor

public __construct(CRequest $request[, IConnectionManager|null $connection_manager = null ][, ICache|null $cache = null ][, ILogger|null $logger = null ]) : mixed
Parameters
$request : CRequest
$connection_manager : IConnectionManager|null = null

Optional connection manager for dependency injection

$cache : ICache|null = null

Optional cache instance for dependency injection

$logger : ILogger|null = null

Optional logger instance

Tags
throws
Exception

if the method (GET,POST) or the table/module is not authorized for the user

__destruct()

Destructor

public __destruct() : mixed

AddRolePermission()

Set a User Role (Normally detected by DB)

public AddRolePermission(string|int $role, string $module_name, array<string|int, mixed> $permissions[, bool $only_owner = false ]) : void
Parameters
$role : string|int

role of user where role can be "Admin", "Publisher" and anything else or a number 1=Admin, 2=Publisher, 3=Normal User the role name and type depends from your app

$module_name : string

the name of module (see "_module" db table)

$permissions : array<string|int, mixed>

list of permission (can_read, can_insert, can_delete, can_udate)

$only_owner : bool = false

if true the permission is valid only for the owners (see "RELATED_USER_ID" defined in config.php and present in various tables)

AddUserRole()

Add a Role to the user

public AddUserRole(string|int $role) : void
Parameters
$role : string|int

role of user where role can be "Admin", "Publisher" and anything else or a number 1=Admin, 2=Publisher, 3=Normal User the role name and type depends from your app

Authenticate()

Check if the username/password pair identifies a valid user

public Authenticate(array<string|int, mixed> $params) : string
Parameters
$params : array<string|int, mixed>

list of parameters (if contains "dusername" and "dpassword" try to login)

Tags
throws
Exception

for a invalid login

Return values
string —

a user token if the user exists, otherwise is a string 'Invalid login'

BeginTransaction()

Starts a DB transaction.

public BeginTransaction() : void

CanViewAllModules()

Check if a user can view all modules (as an Administrator)

public CanViewAllModules() : bool
Return values
bool —

true if the user can view all modules

CheckPermission()

Check permissions by User Role (Normally detected by DB)

public CheckPermission(string $module_name, string $http_method) : bool
Parameters
$module_name : string

the name of module (see "_module" db table)

$http_method : string

method of operation [GET, POST, PATCH, PUT, DELETE, OPTIONS, HEAD] Each method corresponds to a C.R.U.D. permission [C=Create=POST, R=Read=GET/HEAD/OPTIONS, U=Update=PATCH|PUT, D=DELETE].

Return values
bool —

true if the current user have the permission for this method

CheckToken()

public CheckToken(string $token) : bool
Parameters
$token : string
Tags
throws
Exception

if the token is not valid or not found in the request

Return values
bool —

true if the token is valid

CheckValidUser()

Check if the username/password pair identifies a valid user

public CheckValidUser(string $username, string $pwd) : int|null
Parameters
$username : string

username of a user

$pwd : string

password of a user

Return values
int|null —

a user id if exists, otherwise is 0

Commit()

Commits the current transaction.

public Commit() : void

Connect()

Static method to connect to database

public static Connect(string $dbname, string $username, string $pwd[, string|null $type = null ][, string|null $hostname = null ][, int|string|null $port = null ]) : IConnectionManager
Parameters
$dbname : string

name of the database

$username : string

database username

$pwd : string

database password

$type : string|null = null

DBMS type

$hostname : string|null = null

server hostname

$port : int|string|null = null

communication port

Tags
throws
CConnectionException

If invalid database type

Return values
IConnectionManager —

The connection manager instance

ConvertDBType2BindType()

Converts a generic DB data type into a driver-specific bind type.

public ConvertDBType2BindType(string|null $db_type) : string
Parameters
$db_type : string|null

The DB data type.

Return values
string —

The corresponding variable type for parameter binding.

CreateInstance()

Factory method to create a new CUser instance.

public static CreateInstance(CRequest $request[, IConnectionManager|null $connection_manager = null ][, ICache|null $cache = null ][, ILogger|null $logger = null ]) : CUser
Parameters
$request : CRequest

The request object.

$connection_manager : IConnectionManager|null = null

Optional connection manager.

$cache : ICache|null = null

Optional cache instance.

$logger : ILogger|null = null

Optional logger instance.

Return values
CUser —

A new CUser instance.

CreateToken()

public CreateToken(string $username, string $name, string $id) : mixed
Parameters
$username : string
$name : string
$id : string

Decrypt()

Returns the DB instruction to decrypt a value.

public Decrypt(string $val[, string $name = '' ]) : string
Parameters
$val : string

value or field name to decrypt

$name : string = ''

alias for the returned field

Return values
string —

DB instruction to decrypt $val

DeleteRecord()

Delete records from a DB table

public DeleteRecord(string|null $table, string $conditions, array<string|int, mixed> $conditions_values, array<string|int, mixed> $types) : bool
Parameters
$table : string|null

name of the table

$conditions : string

of the where clause

$conditions_values : array<string|int, mixed>

values present in the WHERE condition

$types : array<string|int, mixed>

of DB table fields

Return values
bool —

result of the operation, true if successful

Disconnect()

Releases the resource to connect to the DB

public static Disconnect([string|null $dbkey = null ]) : void
Parameters
$dbkey : string|null = null

key of db to disconnect (optional)

DisconnectAll()

Releases all the resources of the previously opened DBs

public static DisconnectAll() : void

Encrypt()

Returns the DB instruction to encrypt a value.

public Encrypt(string $val[, string $name = '' ]) : string
Parameters
$val : string

value or field name to be encrypted

$name : string = ''

alias for the returned field

Return values
string —

DB instruction to encrypt $val

Execute()

Executes a previously prepared statement.

public Execute([array<string|int, mixed> $bind_param_values = [] ][, array<string|int, mixed> $bind_param_types = [] ]) : void
Parameters
$bind_param_values : array<string|int, mixed> = []
$bind_param_types : array<string|int, mixed> = []

ExistsTable()

Checks if a table exists in the database.

public ExistsTable(string $table) : bool
Parameters
$table : string

name of the table

Return values
bool —

true if it exists, false if it does not

ExistsTableColumns()

Checks if a list of columns exists in a table.

public ExistsTableColumns(string|null $table, array<string|int, mixed> $names) : bool
Parameters
$table : string|null

name of table

$names : array<string|int, mixed>

list of field names to check

Return values
bool —

true if all fields exist

GetAffectedRows()

Returns how many rows were affected by the last operation performed.

public GetAffectedRows() : int
Return values
int —

total of rows affected by the last operation

GetAllAvaiableModules()

Returns the list of modules available to the user

public GetAllAvaiableModules() : array<string|int, mixed>
Return values
array<string|int, mixed> —

the list of modules available to the user

GetAllAvailableModules()

Get an array with all module names available for the current user

public GetAllAvailableModules([mixed $only_visible = false ][, mixed $only_picklist = false ]) : array<string|int, mixed>
Parameters
$only_visible : mixed = false
$only_picklist : mixed = false
Return values
array<string|int, mixed> —

the list of all modules name available for the current user

GetAllModules()

Returns a list of all modules, using cache.

public GetAllModules() : array<string|int, mixed>
Return values
array<string|int, mixed>

GetAllModulesPermissions()

Get permissions for all visible modules for the current user

public GetAllModulesPermissions() : array<string|int, mixed>
Return values
array<string|int, mixed> —

associative array with module_name => [can_read, can_insert, can_update, can_delete]

GetAllTables()

Returns a list of all tables, using cache.

public GetAllTables() : array<string|int, mixed>
Return values
array<string|int, mixed> —

list of all tables

GetArrayByName()

Fetches a result set into a key-value array, using array_column for optimization.

public GetArrayByName([string|int $name = 'name' ][, string|null $fieldname_value = null ]) : array<string|int, mixed>|null
Parameters
$name : string|int = 'name'

The column to use as the key.

$fieldname_value : string|null = null

The column to use as the value. If null, the rest of the row is used.

Return values
array<string|int, mixed>|null —

The resulting key-value array.

GetArrayItemOfElements()

Given an array of records, returns a new array containing only the values of a specific field.

public GetArrayItemOfElements(array<string|int, mixed> &$elements, string $fieldname) : array<string|int, mixed>|null
Parameters
$elements : array<string|int, mixed>

The source array of records.

$fieldname : string

The field name to extract.

Return values
array<string|int, mixed>|null —

An array containing the extracted values.

GetAttributesFromType()

Gets field attributes based on a DB data type.

public GetAttributesFromType(string $type, array<string|int, mixed> &$values) : bool
Parameters
$type : string

the db type to check (e.g., INT, VARCHAR)

$values : array<string|int, mixed>

container for the attributes

Return values
bool —

true if attributes were found

GetAvailableModuleByTableName()

Get an associative array with boolean "can_read" true if can read the table and with module_name that contains the name of relative module usually used in related tables to display in templates the ability to perform an operation only if it has permissions.

public GetAvailableModuleByTableName(string $table_name) : string
Parameters
$table_name : string

the name of table

Return values
string —

the name of module that the current user che read, empty string otherwise

GetCache()

Returns the cache object

public GetCache() : ICache|null
Return values
ICache|null —

the cache object if exists

GetCachedArrayByName()

Fetches a result set into a key-value array, using cache.

public GetCachedArrayByName(ISqlComposer $sql[, string|int $name = 'name' ][, string|null $fieldname_value = null ]) : array<string|int, mixed>|null
Parameters
$sql : ISqlComposer

The SQL query object.

$name : string|int = 'name'

The column to use as the key.

$fieldname_value : string|null = null

The column to use as the value.

Return values
array<string|int, mixed>|null —

The resulting key-value array.

GetCachedDataTable()

Fetches a full result set into an array, using cache.

public GetCachedDataTable(mixed &$data, ISqlComposer $sql[, int|null $type = null ][, string|null $with_index = null ]) : void
Parameters
$data : mixed

The array to populate.

$sql : ISqlComposer

The SQL query object.

$type : int|null = null

The generic fetch type (IDBMS::DB_ASSOC, etc.).

$with_index : string|null = null

The field to use as an index for the result array.

GetCachedDataValue()

Fetches a single data value from a query, using cache.

public GetCachedDataValue(ISqlComposer $sql[, int|string $name_col = '0' ][, int|null $pos_row = null ]) : string|null
Parameters
$sql : ISqlComposer

The SQL query object.

$name_col : int|string = '0'

The column name or index.

$pos_row : int|null = null

The row number.

Return values
string|null —

The fetched value or null.

GetCachedFetchAssoc()

Fetches an associative array from a query, using cache.

public GetCachedFetchAssoc(ISqlComposer $sql[, int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$sql : ISqlComposer

The SQL query object.

$pos_row : int|null = null

The row number to fetch.

Return values
array<string|int, mixed>|null —

The fetched row or null.

GetCachedFetchRow()

Fetches an associative array from a query, using cache.

public GetCachedFetchRow(ISqlComposer $sql[, int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$sql : ISqlComposer

The SQL query object.

$pos_row : int|null = null

The row number to fetch.

Return values
array<string|int, mixed>|null —

The fetched row or null.

GetCachedIDsArray()

Fetches a single column from a result set into an array, using cache.

public GetCachedIDsArray(ISqlComposer $sql[, int|string $item = 0 ][, int|string|null $idx = null ]) : array<string|int, mixed>
Parameters
$sql : ISqlComposer

The SQL query object.

$item : int|string = 0

The column index or name to fetch.

$idx : int|string|null = null

The column index or name to use as the array key.

Return values
array<string|int, mixed> —

An array of values.

GetColumnNames()

Returns an array containing the names of all columns for the last query executed.

public GetColumnNames() : array<string|int, string>|null
Return values
array<string|int, string>|null —

the associative array of column names

GetCurrentDate()

Returns the current date.

public static GetCurrentDate() : string
Return values
string —

The current date in 'Y-m-d' format.

GetCurrentDateTime()

Returns the current date and time.

public static GetCurrentDateTime() : string
Return values
string —

The current date and time in 'Y-m-d H:i:s' format.

GetDataTable()

Fetches the entire result set of a query into an array.

public GetDataTable(array<string|int, mixed> &$data[, int|null $type = null ][, string|null $with_index = null ]) : int|null
Parameters
$data : array<string|int, mixed>

array containing the records found

$type : int|null = null

the fetch type (e.g., IDBMS::DB_ASSOC)

$with_index : string|null = null

field name to use as the index of the main array

Return values
int|null —

the total of records found or null on error

GetDataValue()

Returns a single value from a query result.

public GetDataValue([int|string $name_col = '0' ][, int|null $pos_row = null ]) : string|null
Parameters
$name_col : int|string = '0'

column name or numeric index

$pos_row : int|null = null

position of the row

Return values
string|null —

value of the indicated cell, or null if it does not exist

GetDbmsType()

Get the name of DBMS used

public GetDbmsType() : string
Return values
string —

the name of DBMS used. i.e.: mysql, oracle, postgresql (the convention is all in lowercase)

GetDBResultAssoc()

Returns the driver-specific value for an associative array fetch type.

public GetDBResultAssoc() : int
Return values
int

GetDBResultBoth()

Returns the driver-specific value for a fetch type that includes both associative and numeric arrays.

public GetDBResultBoth() : int
Return values
int

GetDBResultNum()

Returns the driver-specific value for a numeric array fetch type.

public GetDBResultNum() : int
Return values
int

GetDBTable()

Returns the name of the current main table

public GetDBTable() : string
Return values
string —

name of the main reference table

GetFetchArray()

Fetches a result row as an array (both numeric and associative).

public GetFetchArray([int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$pos_row : int|null = null

position of the row to read

Return values
array<string|int, mixed>|null —

the fetched row

GetFetchAssoc()

Fetches a result row as an associative array.

public GetFetchAssoc([int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$pos_row : int|null = null

position of the row to read

Return values
array<string|int, mixed>|null —

the fetched row

GetFetchRow()

Fetches a result row as a numeric array.

public GetFetchRow([int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$pos_row : int|null = null

position of the row to read

Return values
array<string|int, mixed>|null —

the fetched row

GetId()

public GetId() : int|null
Return values
int|null —

get the id of current user

GetIDsArray()

Fetches a single column from the last query result into an array.

public GetIDsArray([int|string $item = 0 ][, int|string|null $idx = null ]) : array<string|int, mixed>
Parameters
$item : int|string = 0

The column index or name to fetch.

$idx : int|string|null = null

The column index or name to use as the array key.

Return values
array<string|int, mixed> —

An array of values.

GetLastDBErrorNumber()

Gets any error number issued by the DB.

public static GetLastDBErrorNumber() : int
Return values
int —

integer value containing the error issued by the DB

GetLastDBErrorString()

Gets any error string issued by the DB.

public static GetLastDBErrorString() : string
Return values
string —

containing the error issued by the DB

GetLastID()

Returns the ID of the last inserted item

public GetLastID() : int|null
Return values
int|null —

The ID of the last inserted element, or null on failure.

GetName()

public GetName() : string
Return values
string —

get the name of current user

GetNextID()

Returns the next ID from a sequence or similar.

public GetNextID([string $table = '' ]) : int|null
Parameters
$table : string = ''

the name of counter associated to SEQUENCE

Return values
int|null —

the next ID of the table

GetPermissions()

Set a User Role (Normally detected by DB)

public GetPermissions(string $module_name) : array<string|int, mixed>
Parameters
$module_name : string

the name of module (see "_module" db table)

Return values
array<string|int, mixed> —

contains all permissions for selected module

GetQueryDataTable()

Queries the DB and returns the result in a two-dimensional array.

public GetQueryDataTable(array<string|int, mixed> &$data, CSqlComposer $sql[, int|null $type = null ][, string|null $with_index = null ]) : int|null
Parameters
$data : array<string|int, mixed>

array containing the records found

$sql : CSqlComposer

the query to execute

$type : int|null = null

fetch type using generic constants (IDBMS::DB_ASSOC, etc.)

$with_index : string|null = null

field name to use as the index of the main array

Return values
int|null —

the total of records found or null on error

GetQueryDataValue()

Executes a query and fetches a single value.

public GetQueryDataValue(CSqlComposer $sql[, string $name_col = '0' ][, int|null $pos_row = null ]) : string|null
Parameters
$sql : CSqlComposer

The SQL query string.

$name_col : string = '0'

The column name or index.

$pos_row : int|null = null

The row number.

Return values
string|null —

The fetched value or null.

GetQueryFetchAssoc()

Executes a query and fetches a single row as an associative array.

public GetQueryFetchAssoc(CSqlComposer $sql[, int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$sql : CSqlComposer

The SQL query string.

$pos_row : int|null = null

The row number to fetch.

Return values
array<string|int, mixed>|null —

The fetched row or null.

GetQueryFetchRow()

Executes a query and fetches a single row as an numeric index array.

public GetQueryFetchRow(CSqlComposer $sql[, int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$sql : CSqlComposer

The SQL query string.

$pos_row : int|null = null

The row number to fetch.

Return values
array<string|int, mixed>|null —

The fetched row or null.

GetTableColumnNames()

Returns an array containing the names of all columns of a table.

public GetTableColumnNames(string $tablename) : array<string|int, string>
Parameters
$tablename : string

name of table

Return values
array<string|int, string> —

an array with all field names

GetToken()

public GetToken() : string|null
Return values
string|null

GetTotalColumns()

Returns the number of columns of a previously executed query.

public GetTotalColumns() : int|bool
Return values
int|bool —

total of the columns or false on error

GetTotalRows()

Reads the total number of rows from a previously executed query.

public GetTotalRows() : int|bool
Return values
int|bool —

total number of rows or false on error

GetUsername()

public GetUsername() : string
Return values
string —

get the username of current user

GetUserRoles()

Get the list of id__role of current user

public GetUserRoles() : array<string|int, mixed>
Return values
array<string|int, mixed> —

list of current id__role available

HasRole()

Check if a user have a role

public HasRole(string $name) : bool
Parameters
$name : string
Return values
bool —

true if the user have the role, false otherwise

ImportACL()

This method import automatically all permssions of the current user if exists an table named module_role_permission and the relative tables: role and user_role

public ImportACL(string $module_name) : bool

For convenience "controller" and "permission" are not indexes of external tables but if you want, you can create and manage them in a derived CUser class

For example:

The "Role" table contains the possible roles:

id name

1 Administrator 2 Anonymous // All permissions for anonymous is valid for all user 3 Pasquale Tufano 4 Michael Franks 5 Joe Sample

The "User" table contains all the user subscribed:

id name

1 Administrator // Can make all 2 Anonymous // Can read page and comment 3 Editor // Full power: can read, update, delete or update a page or a comment of all users 4 Writer // Can read, write, update a page created by current user 5 Commentator // Can read a page and read or write a comment

id role module can_read can_insert can_update can_delete only_owner

1 1 null YES YES YES YES NO 2 2 page YES NO NO NO NO 3 2 comment YES NO NO NO NO 4 3 page YES YES YES YES NO 5 3 comment YES YES YES YES NO 6 4 page YES YES YES NO YES 6 5 comment YES YES NO NO NO

For identificate the roles of current user is necessary use a cross table like this

id role user

1 1 1 2 2 2 3 3 5

Parameters
$module_name : string

name of module to import

Return values
bool

InsertRecord()

Make an entry in the DB

public InsertRecord(string|null $table, array<string|int, mixed> $items, array<string|int, mixed> $types) : bool
Parameters
$table : string|null

name of the table

$items : array<string|int, mixed>

associative array FIELD_NAME => FIELD_VALUE

$types : array<string|int, mixed>

of DB table fields

Return values
bool —

true if the insertion has been made

InsertRecordBatch()

Insert multiple records in a single batch operation

public InsertRecordBatch(string|null $table, array<string|int, mixed> $items, array<string|int, mixed> $batch_data, array<string|int, mixed> $types) : bool
Parameters
$table : string|null

name of the table (if null uses the default table)

$items : array<string|int, mixed>

associative array with field names as keys for the first record

$batch_data : array<string|int, mixed>

array of arrays, each containing values for one record

$types : array<string|int, mixed>

list of types mapped one to one with fields

Return values
bool —

true if successful, false otherwise

IsAdmin()

public IsAdmin() : bool
Return values
bool —

true if the current user is Admin Note: Using this method is not recommended as it may cause inconsistencies with permissions. If possible use checkPermission()

IsAnonymous()

public IsAnonymous() : bool
Return values
bool —

true if the current user is Anonymous

IsAuthenticated()

public IsAuthenticated() : bool
Return values
bool —

true if the current user is NOT Anonymous

IsOnlyOwnerPermissions()

Set a User Role (Normally detected by DB)

public IsOnlyOwnerPermissions(string $module_name, string $http_method) : bool
Parameters
$module_name : string

the name of module (see "_module" db table)

$http_method : string

method of operation [GET, POST, PATCH, PUT, DELETE, OPTIONS, HEAD]

Return values
bool —

if true the permission is valid only for the owners (see "RELATED_USER_ID" defined in config.php and present in various tables)

LoadAllTables()

Loads the schema of all tables into an array.

public LoadAllTables(array<string|int, mixed> &$db_tables) : void
Parameters
$db_tables : array<string|int, mixed>

list of tables to be returned

Logout()

User Logout

public Logout() : void

Clears the user session and resets the user to anonymous state. For API clients, they should discard their token. For HTML clients, this clears the session token.

Prepare()

Prepares an SQL statement for execution.

public Prepare(string $sql) : void
Parameters
$sql : string

string containing the query to prepare

Query()

Executes a query on the DB.

public Query(string $sql[, array<string|int, mixed> $bind_param_values = [] ][, array<string|int, mixed> $bind_param_types = [] ]) : bool
Parameters
$sql : string

string containing the query to execute

$bind_param_values : array<string|int, mixed> = []

deferred values for the query

$bind_param_types : array<string|int, mixed> = []

DB data types

Return values
bool —

true if the query was successful

ResetUserRoles()

Reset a User Role

public ResetUserRoles() : void

Rollback()

Rolls back the current transaction.

public Rollback() : void

Run()

public Run() : string
Return values
string —

OK if is done

SetAllModules()

Returns a list of all modules, using cache.

public SetAllModules() : void

SetAllTables()

Returns a list of all tables, using cache.

public SetAllTables() : array<string|int, mixed>
Tags
throws
Exception

if no tables are found

Return values
array<string|int, mixed> —

list of all tables

SetAutoCommit()

Sets the autocommit mode.

public SetAutoCommit([bool $bool = true ]) : void
Parameters
$bool : bool = true

if is true the autocommit is running

SetDBColumns()

Loads the schema of a table's columns into an array.

public SetDBColumns(array<string|int, mixed> &$db_cols[, string|null $table = null ]) : void
Parameters
$db_cols : array<string|int, mixed>

the results to be returned

$table : string|null = null

the table to be sampled

SetDBTable()

Sets the name of the reference table for the instance.

public SetDBTable(string $table) : void
Parameters
$table : string

The name of the main table to set.

SetRolePermissions()

Set a User Role (Normally detected by DB)

public SetRolePermissions(string|int $role, string $module_name, array<string|int, mixed> $permissions[, bool $only_owner = false ]) : void
Parameters
$role : string|int

role of user where role can be "Admin", "Publisher" and anything else or a number 1=Admin, 2=Publisher, 3=Normal User the role name and type depends from your app

$module_name : string

the name of module (see "_module" db table)

$permissions : array<string|int, mixed>

Name of permissions [GET, POST, PATCh, PUT, DELETE, OPTIONS] or if you prefer you can use C.R.U.D.O. letters [C=POST (Insert), R=GET (Read), U=PATCH|PUT (Modify), D=DELETE, O=OPTIONS]. Finally, you can use * for all valid permission

$only_owner : bool = false

if true the permission is valid only for the owners (see "RELATED_USER_ID" defined in config.php and present in various tables)

SetUserRoles()

Set a list User Roles

public SetUserRoles(array<string|int, mixed> $roles) : void
Parameters
$roles : array<string|int, mixed>

role of user where role can be "Admin", "Publisher" and anything else or a number 1=Admin, 2=Publisher, 3=Normal User the role name and type depends from your app

UpdateRecord()

Make an update in the DB

public UpdateRecord(string|null $table, string $conditions, array<string|int, mixed> $items, array<string|int, mixed> $conditions_values, array<string|int, mixed> $types) : bool
Parameters
$table : string|null

name of the table

$conditions : string

of the update

$items : array<string|int, mixed>

associative array FIELD_NAME => FIELD_VALUE

$conditions_values : array<string|int, mixed>

values of the conditions

$types : array<string|int, mixed>

of DB table fields

Return values
bool —

true if the update has been made

GetCachedData()

Retrieves data from the cache.

protected GetCachedData(string $cache_key[, string|null $table = null ][, bool $is_global = false ]) : mixed
Parameters
$cache_key : string

The key for the cache entry.

$table : string|null = null

The cache area (usually the table name).

$is_global : bool = false

Whether the cache is global or user-specific.

Return values
mixed —

The cached data or null if not found.

GetCacheKey()

Generates a unique cache key from a string.

protected GetCacheKey(string $key) : string
Parameters
$key : string

The input string.

Return values
string —

The hashed cache key.

InvalidateRelatedCache()

Invalidates the cache for a given table and its related tables.

protected InvalidateRelatedCache(string|null $table) : void
Parameters
$table : string|null

The table name to invalidate. Defaults to the current table.

SetCachedData()

Stores data in the cache.

protected SetCachedData(string $cache_key, mixed $data[, string|null $table = null ][, bool $is_global = false ]) : void
Parameters
$cache_key : string

The key for the cache entry.

$data : mixed

The data to store.

$table : string|null = null

The cache area (usually the table name).

$is_global : bool = false

Whether the cache is global or user-specific.

GetRoleConditions()

Role Condition for sql query

private GetRoleConditions() : string
Return values
string —

the role conditions


        
On this page

Search results