Skip to content

Internals Admin Model AdminModel

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

admin\model\AdminModel

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/admin/model/AdminModel.php · Snapshot 8411f4287706.

Admin models and authentication manage the framework administrator record and PHP session. Permission levels are compared numerically with lower values granting broader access. Passwords currently use SHA-512.

Imports and dependencies

use /**
 * The Model class typically serves as the base class for all data-driven models
 * in the application, providing core functionalities for data manipulation,
 * validation, and interaction with data storage mechanisms such as databases.
 *
 * Responsibilities of this class may include:
 * - Defining and managing properties that represent data fields.
 * - Handling data retrieval and storage via external sources such as databases.
 * - Providing methods for data validation and transformation within the model.
 * - Acting as a common interface for various entity-specific models in the system.
 *
 * This class is expected to be extended by specific implementations tailored to
 * the application's needs and should follow common design patterns for managing
 * and structuring data logic.
 */
    model\Model;
use /**
 * The SessionHandler class provides a way to manage session operations in a standardized manner.
 * It implements custom session storage handlers for reading, writing, updating, and deleting session data.
 *
 * This class works as an interface between PHP's session management system and any custom session storage implementation.
 */
    session\SessionHandler;

AdminModel

Declaration and architectural role

class AdminModel extends Model

Source description (behavioral corrections are recorded in Known discrepancies):

Class AdminModel

Represents an administrative user with various properties and methods for managing user data, including authentication, session handling, and role-based permissions.

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$id public ?int $id = null; Used by setID().
$username public ?string $username; Used by __construct(), setUsername().
$profile_picture public ?string $profile_picture; Used by __construct(), setProfilePicture().
$permission_level public ?int $permission_level; Used by __construct(), setPermLevel(), setRole().
$token public ?string $token = null; Used by setToken(), createToken().
$firstname public ?string $firstname; Used by setFirstname().
$lastname public ?string $lastname; Used by setLastname().
$email public ?string $email; Used by setEmail().
$session private SessionHandler $session; Used by __construct().
$role public ?string $role; Used by __construct(), setRole().
$password public ?string $password; Used by __construct(), setPassword(), createToken(), changePassword().
$roles private array $roles = [ "Admin", "Moderator", "Author", ]; Used by __construct(), setRole().
Constructor-promoted public function __construct(?int $id = null Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct(?int $id = null, bool $manual = false)
hiddenAttributes protected function hiddenAttributes(): array
setID public function setID(int $id)
setUsername public function setUsername(string $username): self
setProfilePicture public function setProfilePicture(?string $profile_picture): self
setPermLevel public function setPermLevel(int $permission_level): self
setPassword public function setPassword(string $password): self
setEmail public function setEmail(string $email): self
setRole public function setRole(): self
setToken public function setToken(string $token): self
setFirstname public function setFirstname(string $firstname): self
setLastname public function setLastname(string $lastname): self
createToken public function createToken(): self
changePassword public function changePassword(string $oldPassword, string $newPassword): bool
register public function register(): int|bool

__construct

Source line 65

public function __construct(?int $id = null, bool $manual = false)

Parameters

Parameter Type, modifiers and default Purpose
$id ?int $id = null Record or runtime identifier selected by this operation.
$manual bool $manual = false Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->session, $this->username, $this->profile_picture, $this->permission_level, $this->role, $this->password.

Implementation, validation guards and interaction order
public function __construct(?int $id = null, bool $manual = false)
    {
        parent::__construct();

        if (is_null($id) && !$manual) {
            $this->session = new SessionHandler();
            $this->session->SessionHandler();
            $id = (int)$_SESSION['admin']['id'];
        }

        if (!$manual) {
            $this->selectByID($id, "awt_admin");
            $this->username = $this->getParam("username");
            $this->profile_picture = $this->getParam("profile_picture");
            $this->permission_level = $this->getParam("permission_level");
            $this->role = $this->roles[$this->permission_level];
            $this->password = $this->getParam("password");

        }

        $this->paramBlackList("role");
    }

hiddenAttributes

Source line 88

protected function hiddenAttributes(): array

Return expressions (including local callbacks): ['password', 'token'].

Implementation, validation guards and interaction order
protected function hiddenAttributes(): array { return ['password', 'token']; }

setID

Source line 90

public function setID(int $id)

Parameters

Parameter Type, modifiers and default Purpose
$id int $id Record or runtime identifier selected by this operation.

State writes: $this->id, $this->model_id.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setID(int $id)
    {
        $this->id = $id;
        $this->model_id = $id;
        return $this;
    }

setUsername

Source line 97

public function setUsername(string $username): self

Parameters

Parameter Type, modifiers and default Purpose
$username string $username Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->username.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setUsername(string $username): self
    {
        $this->username = $username;
        return $this;
    }

setProfilePicture

Source line 103

public function setProfilePicture(?string $profile_picture): self

Parameters

Parameter Type, modifiers and default Purpose
$profile_picture ?string $profile_picture Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->profile_picture.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setProfilePicture(?string $profile_picture): self
    {
        $this->profile_picture = $profile_picture;
        return $this;
    }

setPermLevel

Source line 109

public function setPermLevel(int $permission_level): self

Parameters

Parameter Type, modifiers and default Purpose
$permission_level int $permission_level Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->permission_level.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setPermLevel(int $permission_level): self
    {
        $this->permission_level = $permission_level;
        $this->setRole();
        return $this;
    }

setPassword

Source line 116

public function setPassword(string $password): self

Parameters

Parameter Type, modifiers and default Purpose
$password string $password Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->password.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setPassword(string $password): self
    {
        $password = hash("SHA512", $password);
        $this->password = $password;

        return $this;
    }

setEmail

Source line 124

public function setEmail(string $email): self

Parameters

Parameter Type, modifiers and default Purpose
$email string $email Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->email.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setEmail(string $email): self
    {
        $this->email = $email;
        return $this;
    }

setRole

Source line 130

public function setRole(): self

State writes: $this->role.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setRole(): self
    {
        $this->role = $this->roles[$this->permission_level];
        return $this;
    }

setToken

Source line 136

public function setToken(string $token): self

Parameters

Parameter Type, modifiers and default Purpose
$token string $token Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->token.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setToken(string $token): self
    {
        $this->token = $token;
        return $this;
    }

setFirstname

Source line 142

public function setFirstname(string $firstname): self

Parameters

Parameter Type, modifiers and default Purpose
$firstname string $firstname Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->firstname.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setFirstname(string $firstname): self
    {
        $this->firstname = $firstname;
        return $this;
    }

setLastname

Source line 148

public function setLastname(string $lastname): self

Parameters

Parameter Type, modifiers and default Purpose
$lastname string $lastname Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->lastname.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setLastname(string $lastname): self
    {
        $this->lastname = $lastname;
        return $this;
    }

createToken

Source line 154

public function createToken(): self

State writes: $this->token.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function createToken(): self
    {
        $this->token = hash("SHA512", time() . $this->password);

        return $this;
    }

changePassword

Source line 161

public function changePassword(string $oldPassword, string $newPassword): bool

Parameters

Parameter Type, modifiers and default Purpose
$oldPassword string $oldPassword Operation input; the exact use, validation and propagation are shown in the implementation trace.
$newPassword string $newPassword Operation input; the exact use, validation and propagation are shown in the implementation trace.

Return expressions (including local callbacks): $this->setPassword($newPassword)->save(); false.

Implementation, validation guards and interaction order
public function changePassword(string $oldPassword, string $newPassword): bool
    {
        if ($this->password === hash("SHA512", $oldPassword)) {
            return $this->setPassword($newPassword)->save();
        }
        return false;
    }

register

Source line 169

public function register(): int|bool

State writes: $this->model_source.

Return expressions (including local callbacks): $this->saveModel(); false.

Failure handling catches \PDOException.

Implementation, validation guards and interaction order
public function register(): int|bool
    {
        try {
            $this->model_source = "awt_admin";
            return $this->saveModel();
        } catch (\PDOException $e) {
            return false;
        }
    }

Related types

Clone this wiki locally