Skip to content

Commit e5a9757

Browse files
committed
Refactor User classes and add jsonSerialize()
1 parent 39a71ac commit e5a9757

File tree

14 files changed

+234
-134
lines changed

14 files changed

+234
-134
lines changed

src/Controllers/User/Login.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function invoke(?array $args): bool
2626
}
2727

2828
$this->model->_responseCode = HttpCode::HTTP_OK;
29-
$this->model->error = LoginModel::ERROR_NONE;
29+
$this->model->error = null;
3030

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

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

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

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

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

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

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

7575
$event = Logger::initEvent(
7676
\BNETDocs\Libraries\EventLog\EventTypes::USER_LOGIN,

src/Controllers/User/Verify.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use \BNETDocs\Libraries\Core\HttpCode;
66
use \BNETDocs\Libraries\EventLog\Logger;
7+
use \BNETDocs\Models\User\Verify as VerifyModel;
78

89
class Verify extends \BNETDocs\Controllers\Base
910
{
@@ -12,7 +13,7 @@ class Verify extends \BNETDocs\Controllers\Base
1213
*/
1314
public function __construct()
1415
{
15-
$this->model = new \BNETDocs\Models\User\Verify();
16+
$this->model = new VerifyModel();
1617
}
1718

1819
/**
@@ -36,18 +37,17 @@ public function invoke(?array $args): bool
3637
$user_token = $this->model->user ? $this->model->user->getVerifierToken() : null;
3738
if (!$this->model->user || $user_token !== $this->model->token)
3839
{
39-
$this->model->error = 'INVALID_TOKEN';
40+
$this->model->error = VerifyModel::ERROR_INVALID_TOKEN;
4041
$this->model->_responseCode = HttpCode::HTTP_BAD_REQUEST;
4142
return true;
4243
}
4344

4445
try
4546
{
4647
$this->model->user->setVerified(true, true);
47-
$this->model->user->commit();
48-
$this->model->error = false;
48+
$this->model->error = $this->model->user->commit() ? false : VerifyModel::ERROR_INTERNAL;
4949
}
50-
catch (\Throwable) { $this->model->error = 'INTERNAL_ERROR'; }
50+
catch (\Throwable) { $this->model->error = VerifyModel::ERROR_INTERNAL; }
5151

5252
if (!$this->model->error)
5353
{

src/Models/User/ChangePassword.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
namespace BNETDocs\Models\User;
44

5-
class ChangePassword extends \BNETDocs\Models\ActiveUser
5+
class ChangePassword extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
7-
public ?string $error_extra = null;
7+
public ?string $error_extra = null;
8+
9+
public function jsonSerialize(): mixed
10+
{
11+
return \array_merge(parent::jsonSerialize(), ['error_extra' => $this->error_extra]);
12+
}
813
}

src/Models/User/CreatePassword.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
namespace BNETDocs\Models\User;
44

5-
class CreatePassword extends \BNETDocs\Models\ActiveUser
5+
class CreatePassword extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
7-
public ?string $input;
8-
public ?string $output;
7+
public ?string $input = null;
8+
public ?string $output = null;
9+
10+
public function jsonSerialize(): mixed
11+
{
12+
return \array_merge(parent::jsonSerialize(), [
13+
'input' => $this->input,
14+
'output' => $this->output,
15+
]);
16+
}
917
}

src/Models/User/Index.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
namespace BNETDocs\Models\User;
44

5-
class Index extends \BNETDocs\Models\ActiveUser
5+
class Index extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
7-
public $limit;
8-
public $order;
9-
public $page;
10-
public $pages;
11-
public $sum_users;
12-
public $users;
7+
public int $limit = 0;
8+
public string|array|null $order = null;
9+
public int $page = 0;
10+
public int $pages = 0;
11+
public int $sum_users = 0;
12+
public ?array $users = null;
13+
14+
public function jsonSerialize(): mixed
15+
{
16+
return \array_merge(parent::jsonSerialize(), [
17+
'limit' => $this->limit,
18+
'order' => $this->order,
19+
'page' => $this->page,
20+
'pages' => $this->pages,
21+
'sum_users' => $this->sum_users,
22+
'users' => $this->users,
23+
]);
24+
}
1325
}

src/Models/User/Login.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@
22

33
namespace BNETDocs\Models\User;
44

5-
class Login extends \BNETDocs\Models\ActiveUser
5+
class Login extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
77
public const ERROR_ALREADY_LOGGED_IN = 'ALREADY_LOGGED_IN';
88
public const ERROR_EMPTY_EMAIL = 'EMPTY_EMAIL';
99
public const ERROR_EMPTY_PASSWORD = 'EMPTY_PASSWORD';
1010
public const ERROR_INCORRECT_PASSWORD = 'INCORRECT_PASSWORD';
11-
public const ERROR_INTERNAL = 'INTERNAL';
12-
public const ERROR_NONE = 'NONE';
13-
public const ERROR_SUCCESS = 'SUCCESS';
11+
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
1412
public const ERROR_SYSTEM_DISABLED = 'SYSTEM_DISABLED';
1513
public const ERROR_USER_DISABLED = 'USER_DISABLED';
1614
public const ERROR_USER_NOT_FOUND = 'USER_NOT_FOUND';
1715
public const ERROR_USER_NOT_VERIFIED = 'USER_NOT_VERIFIED';
1816

1917
public ?string $email = null;
20-
public mixed $error = self::ERROR_INTERNAL;
2118
public ?string $password = null;
2219
public ?\BNETDocs\Libraries\User\User $user = null;
20+
21+
public function jsonSerialize(): mixed
22+
{
23+
return \array_merge(parent::jsonSerialize(), [
24+
'email' => $this->email,
25+
'password' => $this->password,
26+
'user' => $this->user,
27+
]);
28+
}
2329
}

src/Models/User/Logout.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22

33
namespace BNETDocs\Models\User;
44

5-
class Logout extends \BNETDocs\Models\ActiveUser
6-
{
7-
public mixed $error = true;
8-
}
5+
class Logout extends \BNETDocs\Models\ActiveUser {}

src/Models/User/Register.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
namespace BNETDocs\Models\User;
44

5-
class Register extends \BNETDocs\Models\ActiveUser
5+
class Register extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
77
public ?string $email = null;
88
public ?string $error_extra = null;
99
public ?\BNETDocs\Libraries\Core\Recaptcha $recaptcha = null;
1010
public ?string $username = null;
1111
public int $username_max_len = 0;
12+
13+
public function jsonSerialize(): mixed
14+
{
15+
return \array_merge(parent::jsonSerialize(), [
16+
'email' => $this->email,
17+
'error_extra' => $this->error_extra,
18+
'recaptcha' => $this->recaptcha,
19+
'username' => $this->username,
20+
'username_max_len' => $this->username_max_len,
21+
]);
22+
}
1223
}

src/Models/User/ResetPassword.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,35 @@
22

33
namespace BNETDocs\Models\User;
44

5-
class ResetPassword extends \BNETDocs\Models\ActiveUser
5+
class ResetPassword extends \BNETDocs\Models\Core\HttpForm implements \JsonSerializable
66
{
7-
public const E_BAD_EMAIL = 'BAD_EMAIL';
8-
public const E_BAD_TOKEN = 'BAD_TOKEN';
9-
public const E_EMPTY_EMAIL = 'EMPTY_EMAIL';
10-
public const E_SUCCESS = 'SUCCESS';
11-
public const E_INTERNAL_ERROR = 'INTERNAL_ERROR';
12-
public const E_PASSWORD_CONTAINS_EMAIL = 'PASSWORD_CONTAINS_EMAIL';
13-
public const E_PASSWORD_CONTAINS_USERNAME = 'PASSWORD_CONTAINS_USERNAME';
14-
public const E_PASSWORD_MISMATCH = 'PASSWORD_MISMATCH';
15-
public const E_PASSWORD_TOO_LONG = 'PASSWORD_TOO_LONG';
16-
public const E_PASSWORD_TOO_SHORT = 'PASSWORD_TOO_SHORT';
17-
public const E_USER_DISABLED = 'USER_DISABLED';
18-
public const E_USER_NOT_FOUND = 'USER_NOT_FOUND';
7+
public const E_BAD_EMAIL = 'BAD_EMAIL';
8+
public const E_BAD_TOKEN = 'BAD_TOKEN';
9+
public const E_EMPTY_EMAIL = 'EMPTY_EMAIL';
10+
public const E_SUCCESS = 'SUCCESS';
11+
public const E_INTERNAL_ERROR = 'INTERNAL_ERROR';
12+
public const E_PASSWORD_CONTAINS_EMAIL = 'PASSWORD_CONTAINS_EMAIL';
13+
public const E_PASSWORD_CONTAINS_USERNAME = 'PASSWORD_CONTAINS_USERNAME';
14+
public const E_PASSWORD_MISMATCH = 'PASSWORD_MISMATCH';
15+
public const E_PASSWORD_TOO_LONG = 'PASSWORD_TOO_LONG';
16+
public const E_PASSWORD_TOO_SHORT = 'PASSWORD_TOO_SHORT';
17+
public const E_USER_DISABLED = 'USER_DISABLED';
18+
public const E_USER_NOT_FOUND = 'USER_NOT_FOUND';
1919

20-
public $email;
21-
public $form_fields;
22-
public $pw1;
23-
public $pw2;
24-
public $token;
25-
public $user;
20+
public ?string $email = null;
21+
public ?string $pw1 = null;
22+
public ?string $pw2 = null;
23+
public ?string $token = null;
24+
public ?\BNETDocs\Libraries\User\User $user = null;
25+
26+
public function jsonSerialize(): mixed
27+
{
28+
return \array_merge(parent::jsonSerialize(), [
29+
'email' => $this->email,
30+
'pw1' => $this->pw1,
31+
'pw2' => $this->pw2,
32+
'token' => $this->token,
33+
'user' => $this->user,
34+
]);
35+
}
2636
}

0 commit comments

Comments
 (0)