z_loginModel
extends z_model
in package
The login model holds logging in and out of users
Table of Contents
- $booter : z_framework
- $lastInsertId : int
- $z_db : z_db
- __construct() : mixed
- Creates a z_model instance.
- addResetCode() : string
- Adds a code for the user to reset its password
- addTooManyLoginsEmailByUserId() : mixed
- Adds a too many login try of a user in the databse
- checkPassword() : bool
- countLoginTriesByTimeSpan() : int
- Counts the login tries after a given time
- countResults() : int
- Returns the number of results in the last query.
- countTableEntries() : int
- Returns the number of datasets in a table.
- createLoginToken() : string
- Creates a login token for a user
- dbDelete() : DeleteQuery
- Create a new DeleteQuery instance for the CakePHP\Database Connection.
- dbInsert() : InsertQuery
- Create a new InsertQuery instance for the CakePHP\Database Connection.
- dbSelect() : SelectQuery
- Create a new SelectQuery instance for the CakePHP\Database Connection.
- dbUpdate() : UpdateQuery
- Create a new UpdateQuery instance for the CakePHP\Database Connection.
- disableResetCode() : mixed
- Disabled a password reset code
- exec() : z_db
- Executes a query as a prepared statement.
- getFullTable() : array<string|int, array<string|int, mixed>>
- Selects a full table or specified fields of it and returns the result as two-dimensional array.
- getInsertId() : int
- Returns the last insert id. Ignores inserts done by log.
- getLogCategoryIdByName() : int
- Gets the ID of a log category. If a category does not exist, this function will create it.
- getModel() : z_model
- Returns a model
- getQueryBuilder() : mixed
- getResetCode() : mixed
- Gets the dataset of a reset code
- getResult() : null|bool|mysqli_result
- Returns the result of the last query.
- getTableWhere() : array<string|int, array<string|int, mixed>>
- Selects a full table or specified fields, filtered with an additional where statement. It returns the result as a two-dimensional array.
- getUserByLogin() : bool|array<string|int, mixed>|null
- Gets an user by its email
- heartbeat() : void
- Runs a very lightweight query to keep the connection alive.
- invalidateSession() : void
- Invalidates and removes a login token (session) for a user
- logAction() : mixed
- Logs an action.
- logActionByCategory() : mixed
- Logs an action.
- newLoginTry() : mixed
- Rescords a new login try of a user
- resultToArray() : array<string|int, array<string|int, mixed>>
- Converts the result of the last query into an array and returns it.
- resultToLine() : array<string|int, mixed>|null
- Returns one line of the last query.
- sendTooManyLoginsEmailByUserId() : bool
- sendTooManyLoginsEmailByUserId
- updatePassword() : void
- Updates the password of an user
- validateCookie() : array<string|int, mixed>|false
- Validate a login token retrieved from the users Cookie
Properties
$booter
protected
z_framework
$booter
Reference to the booter
$lastInsertId
protected
int
$lastInsertId
Holds the last ID returned from an insert query. Does not change during logging.
$z_db
protected
z_db
$z_db
Reference to the database proxy
Methods
__construct()
Creates a z_model instance.
public
__construct(z_db &$z_db, z_framework $booter) : mixed
This constructor should only be called from the booter. If you need a model, use $booter->getModel() instead.
Parameters
- $z_db : z_db
-
The database proxy class (Usually one lives in the booter)
- $booter : z_framework
-
Booter object
Return values
mixed —addResetCode()
Adds a code for the user to reset its password
public
addResetCode(int $userId, string $ref, string $reason) : string
Parameters
- $userId : int
-
The id of the user
- $ref : string
-
The reference
- $reason : string
-
The reason of the reset
Return values
string —The reference
addTooManyLoginsEmailByUserId()
Adds a too many login try of a user in the databse
public
addTooManyLoginsEmailByUserId(int $userId) : mixed
Parameters
- $userId : int
-
Id of the user
Return values
mixed —checkPassword()
public
checkPassword(string $password, string $hash, string $salt) : bool
Parameters
- $password : string
- $hash : string
- $salt : string
Return values
bool —countLoginTriesByTimeSpan()
Counts the login tries after a given time
public
countLoginTriesByTimeSpan(int $userId, string $datetime) : int
Parameters
- $userId : int
-
Id of the user
- $datetime : string
-
The datetime in SQL compatible format
Return values
int —The number of login tries in this time
countResults()
Returns the number of results in the last query.
public
countResults() : int
Return values
int —Number of results in the last query
countTableEntries()
Returns the number of datasets in a table.
public
countTableEntries(string $table) : int
Parameters
- $table : string
-
Name of the table in the database
Return values
int —Number of datasets in the specified table
createLoginToken()
Creates a login token for a user
public
createLoginToken(int $userId, int $exec_userId) : string
Parameters
- $userId : int
-
Id of the user
- $exec_userId : int
-
Id of the executing user
Return values
string —The login token
dbDelete()
Create a new DeleteQuery instance for the CakePHP\Database Connection.
public
dbDelete([string|null $table = null ][, array<string|int, mixed> $conditions = [] ][, array<string, string> $types = [] ]) : DeleteQuery
Parameters
- $table : string|null = null
-
The table to delete rows from.
- $conditions : array<string|int, mixed> = []
-
Conditions to be set for the delete statement.
- $types : array<string, string> = []
-
Associative array containing the types to be used for casting.
Return values
DeleteQuery —dbInsert()
Create a new InsertQuery instance for the CakePHP\Database Connection.
public
dbInsert([string|null $table = null ][, array<string|int, mixed> $values = [] ][, array<int|string, string> $types = [] ]) : InsertQuery
Parameters
- $table : string|null = null
-
The table to insert rows into.
- $values : array<string|int, mixed> = []
-
Associative array of column => value to be inserted.
- $types : array<int|string, string> = []
-
Associative array containing the types to be used for casting.
Return values
InsertQuery —dbSelect()
Create a new SelectQuery instance for the CakePHP\Database Connection.
public
dbSelect([ExpressionInterface|callable|array<string|int, mixed>|string $fields = [] ][, array<string|int, mixed>|string $table = [] ][, array<string, string> $types = [] ]) : SelectQuery
Parameters
- $fields : ExpressionInterface|callable|array<string|int, mixed>|string = []
-
fields to be added to the list.
- $table : array<string|int, mixed>|string = []
-
The table or list of tables to query.
- $types : array<string, string> = []
-
Associative array containing the types to be used for casting.
Return values
SelectQuery —dbUpdate()
Create a new UpdateQuery instance for the CakePHP\Database Connection.
public
dbUpdate([ExpressionInterface|string|null $table = null ][, array<string|int, mixed> $values = [] ][, array<string|int, mixed> $conditions = [] ][, array<string, string> $types = [] ]) : UpdateQuery
Parameters
- $table : ExpressionInterface|string|null = null
-
The table to update rows of.
- $values : array<string|int, mixed> = []
-
Values to be updated.
- $conditions : array<string|int, mixed> = []
-
Conditions to be set for the update statement.
- $types : array<string, string> = []
-
Associative array containing the types to be used for casting.
Return values
UpdateQuery —disableResetCode()
Disabled a password reset code
public
disableResetCode(int $id) : mixed
Parameters
- $id : int
-
Id of the reset code in the database
Return values
mixed —exec()
Executes a query as a prepared statement.
public
exec(string $query[, string $types = "" ][, mixed $params = null ]) : z_db
Parameters
- $query : string
-
The query written as a prepared statement (with question marks).
- $types : string = ""
-
The types for the individual parameters (i for int, s for string...).
- $params : mixed = null
Return values
z_db —Returning this for chaining
getFullTable()
Selects a full table or specified fields of it and returns the result as two-dimensional array.
public
getFullTable(string $table[, string $fields = "*" ]) : array<string|int, array<string|int, mixed>>
Parameters
- $table : string
-
Name of the table in the database
- $fields : string = "*"
-
Fields to select. Formatted as in an SQL query ("*", "a, b, c"...)
Return values
array<string|int, array<string|int, mixed>> —A two-dimensional array with the results of the select statement
getInsertId()
Returns the last insert id. Ignores inserts done by log.
public
getInsertId() : int
Return values
int —The ID of the dataset created in the last insert
getLogCategoryIdByName()
Gets the ID of a log category. If a category does not exist, this function will create it.
public
getLogCategoryIdByName(string $name) : int
Parameters
- $name : string
-
Name of the category
Return values
int —ID of the log category
getModel()
Returns a model
public
getModel() : z_model
Return values
z_model —The model
getQueryBuilder()
public
getQueryBuilder() : mixed
Return values
mixed —getResetCode()
Gets the dataset of a reset code
public
getResetCode(string $code, string $timespan) : mixed
Parameters
- $code : string
-
The reset code
- $timespan : string
-
A sql conform formatted time
Return values
mixed —getResult()
Returns the result of the last query.
public
getResult() : null|bool|mysqli_result
Return values
null|bool|mysqli_result —Result of the last query
getTableWhere()
Selects a full table or specified fields, filtered with an additional where statement. It returns the result as a two-dimensional array.
public
getTableWhere(string $table, string $fields, string $where) : array<string|int, array<string|int, mixed>>
Parameters
- $table : string
-
Name of the table in the database
- $fields : string
-
Fields to select. Formatted as in an SQL query ("*", "a, b, c"...)
- $where : string
-
The where statement in the query. Formatted as in an SQL query ("a = 4 AND c = 4"...);
Return values
array<string|int, array<string|int, mixed>> —Two-dimensional array with table data
getUserByLogin()
Gets an user by its email
public
getUserByLogin(string $email) : bool|array<string|int, mixed>|null
Parameters
- $email : string
-
The email of the user to get
Return values
bool|array<string|int, mixed>|null —The dataset of the user
heartbeat()
Runs a very lightweight query to keep the connection alive.
public
heartbeat([bool $waitForTimeout = true ]) : void
Parameters
- $waitForTimeout : bool = true
-
If set to true, only ping if no other query has been made within the timeout period
Return values
void —invalidateSession()
Invalidates and removes a login token (session) for a user
public
invalidateSession(string $token) : void
Parameters
- $token : string
-
The session identifier
Return values
void —logAction()
Logs an action.
public
logAction(int $categoryId, string $text[, int $value = null ]) : mixed
Does not increase the insertId.
Parameters
- $categoryId : int
-
ID of the category in the database
- $text : string
-
Text
- $value : int = null
-
Optional value
Return values
mixed —logActionByCategory()
Logs an action.
public
logActionByCategory(string $categoryName, string $text[, int $value = null ]) : mixed
Does not increase the insertId. If the category does not exist, it will be created.
Parameters
- $categoryName : string
-
Name of the category in the database.
- $text : string
-
Text
- $value : int = null
-
Optional value
Return values
mixed —newLoginTry()
Rescords a new login try of a user
public
newLoginTry(int $userId) : mixed
Counting to detect security issues
Parameters
- $userId : int
-
Id of the user
Return values
mixed —resultToArray()
Converts the result of the last query into an array and returns it.
public
resultToArray() : array<string|int, array<string|int, mixed>>
Return values
array<string|int, array<string|int, mixed>> —Results of the last query as two-dimensional array
resultToLine()
Returns one line of the last query.
public
resultToLine() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null —Line of the last result
sendTooManyLoginsEmailByUserId()
sendTooManyLoginsEmailByUserId
public
sendTooManyLoginsEmailByUserId(int $userId) : bool
Parameters
- $userId : int
-
Id of the user
Return values
bool —RES
updatePassword()
Updates the password of an user
public
updatePassword(int $id, string $password) : void
Parameters
- $id : int
-
The id of the user
- $password : string
-
The raw user password
Return values
void —validateCookie()
Validate a login token retrieved from the users Cookie
public
validateCookie(string $token) : array<string|int, mixed>|false
Parameters
- $token : string
-
The login token that is saved in the clients cookie
Return values
array<string|int, mixed>|false —The user data from the database or false if the token is wrong