z_userModel
extends z_model
in package
User Model
This model handles database stuff with the focus on user managment. An instance of this class can be acquired with z_framework::getModel("z_user")
Table of Contents
- $booter : z_framework
- $lastInsertId : int
- $z_db : z_db
- __construct() : mixed
- Creates a z_model instance.
- add() : int
- Creates an user account
- addRoleToUserByRoleId() : mixed
- Add a role to a user
- changeRoleStateByUserIdAndRoleId() : mixed
- Change the state of a role granted to a user, Can be used to add or remove roles
- countResults() : int
- Returns the number of results in the last query.
- countTableEntries() : int
- Returns the number of datasets in a table.
- createRole() : int
- Creates a role
- createVerifyToken() : mixed
- Creates an email verify token and puts it into the database
- deactivateRole() : mixed
- Deactivates a role
- exec() : z_db
- Executes a query as a prepared statement.
- getCount() : int
- Gets the number of registered users
- 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
- getPermissionsByUserId() : array<string|int, string>
- Gets all permissions a specific user has
- getResult() : null|bool|mysqli_result
- Returns the result of the last query.
- getRoleIdByRoleName() : int
- Gets the id of a role by it's name
- getRoles() : array<string|int, array<string|int, mixed>>
- Gets all the roles a user has
- 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.
- getUserByEmail() : bool|array<string|int, mixed>|null
- Returns a user row of the database, selected by the users email address
- getUserById() : bool|array<string|int, mixed>|null
- Returns a user row of the database, selected by the users id
- getUserList() : array<string|int, array<string|int, mixed>>
- Returns all user data from the database
- heartbeat() : void
- Runs a very lightweight query to keep the connection alive.
- logAction() : mixed
- Logs an action.
- logActionByCategory() : mixed
- Logs an action.
- 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.
- updateAccountSettings() : mixed
- Updates the clients settings
- updatePassword() : mixed
- Sets the password for a user
- verifyUser() : bool
- Verifies an users mail address
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 —add()
Creates an user account
public
add(string $email, int $language[, string $passwordString = null ][, mixed $verified = null ]) : int
Parameters
- $email : string
-
Email of the user
- $language : int
-
Id of users language
- $passwordString : string = null
-
The raw users password. Not hashed! It will be hashed in this function
- $verified : mixed = null
Return values
int —The id of the new created user
addRoleToUserByRoleId()
Add a role to a user
public
addRoleToUserByRoleId(int $userId, int $roleId) : mixed
Parameters
- $userId : int
-
The id of the user itended to recieve the role
- $roleId : int
-
The id of the role to be added
Return values
mixed —changeRoleStateByUserIdAndRoleId()
Change the state of a role granted to a user, Can be used to add or remove roles
public
changeRoleStateByUserIdAndRoleId(int $userId, int $roleId[, mixed $shouldHaveRole = true ]) : mixed
Parameters
- $userId : int
-
The id of the user
- $roleId : int
-
The id of the role
- $shouldHaveRole : mixed = true
Return values
mixed —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
createRole()
Creates a role
public
createRole() : int
Return values
int —The id of the new created role
createVerifyToken()
Creates an email verify token and puts it into the database
public
createVerifyToken(mixed $userId) : mixed
Parameters
- $userId : mixed
Return values
mixed —deactivateRole()
Deactivates a role
public
deactivateRole(int $roleId) : mixed
After a role is deactivated the users with it will loose the role specific permissions as long as they don't have another role with these.
Parameters
- $roleId : int
-
The id of the role to deactivate
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
getCount()
Gets the number of registered users
public
getCount() : int
Return values
int —The number of registered users
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
getPermissionsByUserId()
Gets all permissions a specific user has
public
getPermissionsByUserId(int $userId) : array<string|int, string>
Parameters
- $userId : int
-
Id of the target user
Return values
array<string|int, string> —Array filled with permissions
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
getRoleIdByRoleName()
Gets the id of a role by it's name
public
getRoleIdByRoleName(string $name) : int
Parameters
- $name : string
-
The name of the role
Return values
int —The id of the role
getRoles()
Gets all the roles a user has
public
getRoles(int $userId) : array<string|int, array<string|int, mixed>>
Parameters
- $userId : int
-
The id of the target user
Return values
array<string|int, array<string|int, mixed>> —The datasets of the user_role table
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
getUserByEmail()
Returns a user row of the database, selected by the users email address
public
getUserByEmail(string $email) : bool|array<string|int, mixed>|null
Parameters
- $email : string
-
Email of the user from who we want the data about
Return values
bool|array<string|int, mixed>|null —The dataset
getUserById()
Returns a user row of the database, selected by the users id
public
getUserById(int $userid) : bool|array<string|int, mixed>|null
Parameters
- $userid : int
-
ID of the user we want the data about
Return values
bool|array<string|int, mixed>|null —The dataset
getUserList()
Returns all user data from the database
public
getUserList() : array<string|int, array<string|int, mixed>>
Return values
array<string|int, array<string|int, mixed>> —The table as a two dimensional array
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 —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 —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
updateAccountSettings()
Updates the clients settings
public
updateAccountSettings(int $id, string $email, int $language) : mixed
Parameters
- $id : int
-
Id of the target user
- $email : string
-
The new email
- $language : int
-
The new language id
Return values
mixed —updatePassword()
Sets the password for a user
public
updatePassword(int $id, string $pw) : mixed
Parameters
- $id : int
-
Id of the user which password should be changed
- $pw : string
-
The raw unhashed new password
Return values
mixed —verifyUser()
Verifies an users mail address
public
verifyUser(mixed $token) : bool
Parameters
- $token : mixed
Return values
bool —True if the user was successfully verified or already was verified