User
extends Crud
in package
User records: look up accounts for authentication.
Table of Contents
Constants
- RESERVED : mixed = ['me']
- Usernames no account may take, compared case-insensitively: `me` backs the `/user/me` alias for one's own page.
- ROLES : mixed = ['owner', 'admin', 'user']
- The roles a user account may hold.
Methods
- behind() : bool
- Whether the database lacks migrations the code has.
- connect() : PDO
- Open a connection to the database the `DB_*` settings name.
- dsn() : string
- Build a MySQL DSN.
- findByEmail() : array{id: int, username: string, email: string, hash: string, role: string}|null
- Find a user by email address.
- hashOf() : string|null
- Find a user's password hash.
- options() : array<int, bool|int|string>
- The PDO options of a connection.
- passwordError() : string|null
- The reason to refuse a password, if there is one.
- replaceHash() : bool
- Replace a user's password hash, unless it changed since it was read.
- sslConflict() : string
- The `DB_SSL_*` settings another one leaves without effect, as a warning for the operator.
- sslHint() : string
- What to check when a connection over TLS fails.
- validate() : array<string, string>
- Validate the fields needed to create a user account.
- db() : PDO
- The shared database connection, opened on first use.
- read() : array<int, array<string, mixed>>
- Prepare and run a read query, then return its rows.
Constants
RESERVED
Usernames no account may take, compared case-insensitively: `me` backs the `/user/me` alias for one's own page.
public
mixed
RESERVED
= ['me']
ROLES
The roles a user account may hold.
public
mixed
ROLES
= ['owner', 'admin', 'user']
Methods
behind()
Whether the database lacks migrations the code has.
public
static behind() : bool
Tags
Return values
boolconnect()
Open a connection to the database the `DB_*` settings name.
public
static connect() : PDO
The charset is utf8mb4 and the timezone is the server's.
Tags
Return values
PDO —The connection, with options().
dsn()
Build a MySQL DSN.
public
static dsn(string $host, int $port, string $name) : string
Parameters
- $host : string
-
Host name or IP address, or the path of a Unix socket (it starts with
/). - $port : int
-
TCP port, left out of the DSN when it is MySQL's default (3306) and ignored for a socket.
- $name : string
-
Database name.
Return values
string —The PDO DSN, with the utf8mb4 charset.
findByEmail()
Find a user by email address.
public
findByEmail(string $email) : array{id: int, username: string, email: string, hash: string, role: string}|null
Parameters
- $email : string
-
Normalised (lower-case) email to look up.
Return values
array{id: int, username: string, email: string, hash: string, role: string}|null —The user row, or null if no user has that email.
hashOf()
Find a user's password hash.
public
hashOf(int $id) : string|null
Parameters
- $id : int
-
The user's id.
Return values
string|null —The hash, or null if no user has that id.
options()
The PDO options of a connection.
public
static options(string $host, bool $ssl, bool $verify, string $ca) : array<int, bool|int|string>
Errors throw, and prepared statements are native, so integer columns come back as int. Over TCP the connection
is encrypted, unless $ssl is off; over a Unix socket TLS does not work.
Parameters
- $host : string
-
Host name or IP address, or a socket path;
localhostconnects over the default socket. - $ssl : bool
-
Whether to encrypt over TCP.
- $verify : bool
-
Whether to verify the server's certificate, name included.
- $ca : string
-
CA file to verify it against; '' for the CAs the system trusts.
Return values
array<int, bool|int|string> —The options.
passwordError()
The reason to refuse a password, if there is one.
public
static passwordError(string $password) : string|null
Parameters
- $password : string
-
Plain password (checked before hashing).
Return values
string|null —The error message, or null for an acceptable password.
replaceHash()
Replace a user's password hash, unless it changed since it was read.
public
replaceHash(int $id, string $old, string $new) : bool
Parameters
- $id : int
-
The user's id.
- $old : string
-
The hash as it was read.
- $new : string
-
The hash to store.
Return values
bool —Whether the hash was replaced.
sslConflict()
The `DB_SSL_*` settings another one leaves without effect, as a warning for the operator.
public
static sslConflict(string $host, bool $ssl, bool $verify, string $ca) : string
Parameters
- $host : string
-
Host name or IP address, or a socket path, as for
options(). - $ssl : bool
-
Whether to encrypt over TCP.
- $verify : bool
-
Whether to verify the server's certificate.
- $ca : string
-
CA file to verify it against.
Return values
string —The warning; '' when every setting takes effect.
sslHint()
What to check when a connection over TLS fails.
public
static sslHint(string $error, array<int, bool|int|string> $options) : string
mysqlnd gives one message for every certificate that does not verify, readable CA or not, and reports a server without TLS as gone away.
Parameters
- $error : string
-
The connection's error message.
- $options : array<int, bool|int|string>
-
Its options,
options()'s.
Return values
string —The setting to check, as a sentence; '' when TLS is not why.
validate()
Validate the fields needed to create a user account.
public
static validate(string $email, string $username, string $password, string $role) : array<string, string>
Parameters
- $email : string
-
Email address (identity + login).
- $username : string
-
Public display name.
- $password : string
-
Plain password (checked before hashing).
- $role : string
-
One of
self::ROLES.
Return values
array<string, string> —Error message per invalid field; empty if valid.
db()
The shared database connection, opened on first use.
protected
final static db() : PDO
Building, validating or formatting a model therefore needs no database. With DEBUG on, statements are created as DebugStatement so every query execution is timed.
Tags
Return values
PDO —The shared connection, opened by connect().
read()
Prepare and run a read query, then return its rows.
protected
final read(string $sql[, array<int, array{0: string, 1: mixed, 2: int, 3: int}>|false $param = false ]) : array<int, array<string, mixed>>
Parameters
- $sql : string
-
SQL query with bind placeholders.
- $param : array<int, array{0: string, 1: mixed, 2: int, 3: int}>|false = false
-
Bind parameters (each: [name, value, PDO type, length]), or false for none.
Return values
array<int, array<string, mixed>> —The result rows.