Skip to content

Commit

Permalink
Refactor User classes and add jsonSerialize()
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Dec 2, 2024
1 parent 39a71ac commit e5a9757
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 134 deletions.
10 changes: 5 additions & 5 deletions src/Controllers/User/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function invoke(?array $args): bool
}

$this->model->_responseCode = HttpCode::HTTP_OK;
$this->model->error = LoginModel::ERROR_NONE;
$this->model->error = null;

$q = Router::query();
$this->model->email = $q['email'] ?? null;
Expand All @@ -41,10 +41,10 @@ public function invoke(?array $args): bool
else if (Common::$config->bnetdocs->user_login_disabled)
$this->model->error = LoginModel::ERROR_SYSTEM_DISABLED;

if ($this->model->error !== LoginModel::ERROR_NONE) return true;
if ($this->model->error) return true;

try { $this->model->user = new User(User::findIdByEmail($this->model->email)); }
catch (\UnexpectedValueException) { $this->model->user = null; }
catch (\BNETDocs\Exceptions\UserNotFoundException) { $this->model->user = null; }

if (!$this->model->user)
$this->model->error = LoginModel::ERROR_USER_NOT_FOUND;
Expand All @@ -55,7 +55,7 @@ public function invoke(?array $args): bool
else if (!$this->model->user->isVerified())
$this->model->error = LoginModel::ERROR_USER_NOT_VERIFIED;

if ($this->model->error !== LoginModel::ERROR_NONE) return true;
if ($this->model->error) return true;

// Upgrade old password (we checked it matches earlier above)
if (substr($this->model->user->getPasswordHash(), 0, 1) !== '$')
Expand All @@ -70,7 +70,7 @@ public function invoke(?array $args): bool
}

\BNETDocs\Libraries\User\Authentication::login($this->model->user);
$this->model->error = LoginModel::ERROR_SUCCESS;
$this->model->error = false;

$event = Logger::initEvent(
\BNETDocs\Libraries\EventLog\EventTypes::USER_LOGIN,
Expand Down
10 changes: 5 additions & 5 deletions src/Controllers/User/Verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Libraries\EventLog\Logger;
use \BNETDocs\Models\User\Verify as VerifyModel;

class Verify extends \BNETDocs\Controllers\Base
{
Expand All @@ -12,7 +13,7 @@ class Verify extends \BNETDocs\Controllers\Base
*/
public function __construct()
{
$this->model = new \BNETDocs\Models\User\Verify();
$this->model = new VerifyModel();
}

/**
Expand All @@ -36,18 +37,17 @@ public function invoke(?array $args): bool
$user_token = $this->model->user ? $this->model->user->getVerifierToken() : null;
if (!$this->model->user || $user_token !== $this->model->token)
{
$this->model->error = 'INVALID_TOKEN';
$this->model->error = VerifyModel::ERROR_INVALID_TOKEN;
$this->model->_responseCode = HttpCode::HTTP_BAD_REQUEST;
return true;
}

try
{
$this->model->user->setVerified(true, true);
$this->model->user->commit();
$this->model->error = false;
$this->model->error = $this->model->user->commit() ? false : VerifyModel::ERROR_INTERNAL;
}
catch (\Throwable) { $this->model->error = 'INTERNAL_ERROR'; }
catch (\Throwable) { $this->model->error = VerifyModel::ERROR_INTERNAL; }

if (!$this->model->error)
{
Expand Down
9 changes: 7 additions & 2 deletions src/Models/User/ChangePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

namespace BNETDocs\Models\User;

class ChangePassword extends \BNETDocs\Models\ActiveUser
class ChangePassword extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
{
public ?string $error_extra = null;
public ?string $error_extra = null;

public function jsonSerialize(): mixed
{
return \array_merge(parent::jsonSerialize(), ['error_extra' => $this->error_extra]);
}
}
14 changes: 11 additions & 3 deletions src/Models/User/CreatePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

namespace BNETDocs\Models\User;

class CreatePassword extends \BNETDocs\Models\ActiveUser
class CreatePassword extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
{
public ?string $input;
public ?string $output;
public ?string $input = null;
public ?string $output = null;

public function jsonSerialize(): mixed
{
return \array_merge(parent::jsonSerialize(), [
'input' => $this->input,
'output' => $this->output,
]);
}
}
26 changes: 19 additions & 7 deletions src/Models/User/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

namespace BNETDocs\Models\User;

class Index extends \BNETDocs\Models\ActiveUser
class Index extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
{
public $limit;
public $order;
public $page;
public $pages;
public $sum_users;
public $users;
public int $limit = 0;
public string|array|null $order = null;
public int $page = 0;
public int $pages = 0;
public int $sum_users = 0;
public ?array $users = null;

public function jsonSerialize(): mixed
{
return \array_merge(parent::jsonSerialize(), [
'limit' => $this->limit,
'order' => $this->order,
'page' => $this->page,
'pages' => $this->pages,
'sum_users' => $this->sum_users,
'users' => $this->users,
]);
}
}
16 changes: 11 additions & 5 deletions src/Models/User/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@

namespace BNETDocs\Models\User;

class Login extends \BNETDocs\Models\ActiveUser
class Login extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
{
public const ERROR_ALREADY_LOGGED_IN = 'ALREADY_LOGGED_IN';
public const ERROR_EMPTY_EMAIL = 'EMPTY_EMAIL';
public const ERROR_EMPTY_PASSWORD = 'EMPTY_PASSWORD';
public const ERROR_INCORRECT_PASSWORD = 'INCORRECT_PASSWORD';
public const ERROR_INTERNAL = 'INTERNAL';
public const ERROR_NONE = 'NONE';
public const ERROR_SUCCESS = 'SUCCESS';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_SYSTEM_DISABLED = 'SYSTEM_DISABLED';
public const ERROR_USER_DISABLED = 'USER_DISABLED';
public const ERROR_USER_NOT_FOUND = 'USER_NOT_FOUND';
public const ERROR_USER_NOT_VERIFIED = 'USER_NOT_VERIFIED';

public ?string $email = null;
public mixed $error = self::ERROR_INTERNAL;
public ?string $password = null;
public ?\BNETDocs\Libraries\User\User $user = null;

public function jsonSerialize(): mixed
{
return \array_merge(parent::jsonSerialize(), [
'email' => $this->email,
'password' => $this->password,
'user' => $this->user,
]);
}
}
5 changes: 1 addition & 4 deletions src/Models/User/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@

namespace BNETDocs\Models\User;

class Logout extends \BNETDocs\Models\ActiveUser
{
public mixed $error = true;
}
class Logout extends \BNETDocs\Models\ActiveUser {}
13 changes: 12 additions & 1 deletion src/Models/User/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@

namespace BNETDocs\Models\User;

class Register extends \BNETDocs\Models\ActiveUser
class Register extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
{
public ?string $email = null;
public ?string $error_extra = null;
public ?\BNETDocs\Libraries\Core\Recaptcha $recaptcha = null;
public ?string $username = null;
public int $username_max_len = 0;

public function jsonSerialize(): mixed
{
return \array_merge(parent::jsonSerialize(), [
'email' => $this->email,
'error_extra' => $this->error_extra,
'recaptcha' => $this->recaptcha,
'username' => $this->username,
'username_max_len' => $this->username_max_len,
]);
}
}
48 changes: 29 additions & 19 deletions src/Models/User/ResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@

namespace BNETDocs\Models\User;

class ResetPassword extends \BNETDocs\Models\ActiveUser
class ResetPassword extends \BNETDocs\Models\Core\HttpForm implements \JsonSerializable
{
public const E_BAD_EMAIL = 'BAD_EMAIL';
public const E_BAD_TOKEN = 'BAD_TOKEN';
public const E_EMPTY_EMAIL = 'EMPTY_EMAIL';
public const E_SUCCESS = 'SUCCESS';
public const E_INTERNAL_ERROR = 'INTERNAL_ERROR';
public const E_PASSWORD_CONTAINS_EMAIL = 'PASSWORD_CONTAINS_EMAIL';
public const E_PASSWORD_CONTAINS_USERNAME = 'PASSWORD_CONTAINS_USERNAME';
public const E_PASSWORD_MISMATCH = 'PASSWORD_MISMATCH';
public const E_PASSWORD_TOO_LONG = 'PASSWORD_TOO_LONG';
public const E_PASSWORD_TOO_SHORT = 'PASSWORD_TOO_SHORT';
public const E_USER_DISABLED = 'USER_DISABLED';
public const E_USER_NOT_FOUND = 'USER_NOT_FOUND';
public const E_BAD_EMAIL = 'BAD_EMAIL';
public const E_BAD_TOKEN = 'BAD_TOKEN';
public const E_EMPTY_EMAIL = 'EMPTY_EMAIL';
public const E_SUCCESS = 'SUCCESS';
public const E_INTERNAL_ERROR = 'INTERNAL_ERROR';
public const E_PASSWORD_CONTAINS_EMAIL = 'PASSWORD_CONTAINS_EMAIL';
public const E_PASSWORD_CONTAINS_USERNAME = 'PASSWORD_CONTAINS_USERNAME';
public const E_PASSWORD_MISMATCH = 'PASSWORD_MISMATCH';
public const E_PASSWORD_TOO_LONG = 'PASSWORD_TOO_LONG';
public const E_PASSWORD_TOO_SHORT = 'PASSWORD_TOO_SHORT';
public const E_USER_DISABLED = 'USER_DISABLED';
public const E_USER_NOT_FOUND = 'USER_NOT_FOUND';

public $email;
public $form_fields;
public $pw1;
public $pw2;
public $token;
public $user;
public ?string $email = null;
public ?string $pw1 = null;
public ?string $pw2 = null;
public ?string $token = null;
public ?\BNETDocs\Libraries\User\User $user = null;

public function jsonSerialize(): mixed
{
return \array_merge(parent::jsonSerialize(), [
'email' => $this->email,
'pw1' => $this->pw1,
'pw2' => $this->pw2,
'token' => $this->token,
'user' => $this->user,
]);
}
}
Loading

0 comments on commit e5a9757

Please sign in to comment.