Skip to content

Commit

Permalink
Refactor ERROR_INTERNAL/ACL_NOT_SET/NOT_LOGGED_IN
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Dec 14, 2024
1 parent 3a688d0 commit b16682d
Show file tree
Hide file tree
Showing 34 changed files with 88 additions and 135 deletions.
4 changes: 3 additions & 1 deletion src/Controllers/Document/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use \BNETDocs\Libraries\Comment;
use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Models\Document\View as ViewModel;

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

/**
Expand All @@ -32,6 +33,7 @@ public function invoke(?array $args): bool
&& !($this->model->active_user && $this->model->active_user->isStaff()))
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
$this->model->error = $this->model->active_user ? ViewModel::ERROR_ACL_NOT_SET : ViewModel::ERROR_NOT_LOGGED_IN;
$this->model->document = null;
return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Controllers/EventLog/Index.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\Event;
use \BNETDocs\Models\EventLog\Index as IndexModel;

class Index extends \BNETDocs\Controllers\Base
{
Expand All @@ -13,7 +14,7 @@ class Index extends \BNETDocs\Controllers\Base

public function __construct()
{
$this->model = new \BNETDocs\Models\EventLog\Index();
$this->model = new IndexModel();
}

public function invoke(?array $args): bool
Expand All @@ -24,6 +25,7 @@ public function invoke(?array $args): bool
if (!$this->model->acl_allowed)
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
$this->model->error = $this->model->active_user ? IndexModel::ERROR_ACL_NOT_SET : IndexModel::ERROR_NOT_LOGGED_IN;
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Controllers/EventLog/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace BNETDocs\Controllers\EventLog;

use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Models\EventLog\View as ViewModel;

class View extends \BNETDocs\Controllers\Base
{
public function __construct()
{
$this->model = new \BNETDocs\Models\EventLog\View();
$this->model = new ViewModel();
}

public function invoke(?array $args): bool
Expand All @@ -19,6 +20,7 @@ public function invoke(?array $args): bool
if (!$this->model->acl_allowed)
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
$this->model->error = $this->model->active_user ? ViewModel::ERROR_ACL_NOT_SET : ViewModel::ERROR_NOT_LOGGED_IN;
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Controllers/Packet/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

use \BNETDocs\Libraries\Comment;
use \BNETDocs\Libraries\Core\HttpCode;
use BNETDocs\Models\Packet\View as ViewModel;

class View extends \BNETDocs\Controllers\Base
{
public function __construct()
{
$this->model = new \BNETDocs\Models\Packet\View();
$this->model = new ViewModel();
}

public function invoke(?array $args): bool
Expand All @@ -21,6 +22,7 @@ public function invoke(?array $args): bool
if (!$this->model->packet)
{
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
$this->model->error = ViewModel::ERROR_NOT_FOUND;
return true;
}

Expand Down
4 changes: 1 addition & 3 deletions src/Controllers/Server/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ public function __construct()

public function invoke(?array $args): bool
{
$this->model->server_edit = false;

if (!($this->model->active_user && $this->model->active_user->getOption(\BNETDocs\Libraries\User\User::OPTION_ACL_SERVER_CREATE)))
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
$this->model->error = FormModel::ERROR_ACCESS_DENIED;
$this->model->error = $this->model->active_user ? FormModel::ERROR_ACL_NOT_SET : FormModel::ERROR_NOT_LOGGED_IN;
return true;
}

Expand Down
14 changes: 8 additions & 6 deletions src/Controllers/Server/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Libraries\Core\Router;
use \BNETDocs\Libraries\EventLog\Logger;
use \BNETDocs\Models\Server\Delete as DeleteModel;
use \BNETDocs\Models\Server\Form as FormModel;

class Delete extends \BNETDocs\Controllers\Base
{
Expand All @@ -15,23 +15,25 @@ class Delete extends \BNETDocs\Controllers\Base

public function __construct()
{
$this->model = new DeleteModel();
$this->model = new FormModel();
}

public function invoke(?array $args): bool
{
$this->model->server_delete = true;

if (!($this->model->active_user && $this->model->active_user->getOption(\BNETDocs\Libraries\User\User::OPTION_ACL_SERVER_DELETE)))
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
$this->model->error = $this->model->active_user ? DeleteModel::ERROR_ACL_NOT_SET : DeleteModel::ERROR_NOT_LOGGED_IN;
$this->model->error = $this->model->active_user ? FormModel::ERROR_ACL_NOT_SET : FormModel::ERROR_NOT_LOGGED_IN;
return true;
}

$id = Router::query()['id'] ?? null;
if (!is_numeric($id))
{
$this->model->_responseCode = HttpCode::HTTP_BAD_REQUEST;
$this->model->error = DeleteModel::ERROR_NOT_FOUND;
$this->model->error = FormModel::ERROR_NOT_FOUND;
return true;
}
$id = (int) $id;
Expand All @@ -42,7 +44,7 @@ public function invoke(?array $args): bool
if (!$this->model->server)
{
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
$this->model->error = DeleteModel::ERROR_NOT_FOUND;
$this->model->error = FormModel::ERROR_NOT_FOUND;
return true;
}

Expand All @@ -53,7 +55,7 @@ public function invoke(?array $args): bool

protected function handlePost(): void
{
$this->model->error = $this->model->server->deallocate() ? false : DeleteModel::ERROR_INTERNAL;
$this->model->error = $this->model->server->deallocate() ? false : FormModel::ERROR_INTERNAL;
if ($this->model->error === false)
{
$event = Logger::initEvent(
Expand Down
6 changes: 3 additions & 3 deletions src/Controllers/Server/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function invoke(?array $args): bool
if (!($this->model->active_user && $this->model->active_user->getOption(\BNETDocs\Libraries\User\User::OPTION_ACL_SERVER_MODIFY)))
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
$this->model->error = FormModel::ERROR_ACCESS_DENIED;
$this->model->error = $this->model->active_user ? FormModel::ERROR_ACL_NOT_SET : FormModel::ERROR_NOT_LOGGED_IN;
return true;
}

Expand Down Expand Up @@ -74,9 +74,9 @@ protected function handlePost(): void
$this->model->server->setDisabled((bool) ($this->model->form_fields['disabled'] ?? null));
$this->model->server->setOnline((bool) ($this->model->form_fields['online'] ?? null));

$this->model->error = $this->model->server->commit() ? FormModel::ERROR_SUCCESS : FormModel::ERROR_INTERNAL;
$this->model->error = $this->model->server->commit() ? false : FormModel::ERROR_INTERNAL;

if ($this->model->error === FormModel::ERROR_SUCCESS)
if ($this->model->error === false)
{
$event = Logger::initEvent(
\BNETDocs\Libraries\EventLog\EventTypes::SERVER_EDITED,
Expand Down
28 changes: 14 additions & 14 deletions src/Controllers/User/ResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function invoke(?array $args): bool

protected function doPasswordReset(): mixed
{
if (empty($this->model->email)) return ResetPasswordModel::E_EMPTY_EMAIL;
if (empty($this->model->email)) return ResetPasswordModel::ERROR_EMPTY_EMAIL;

try
{
Expand All @@ -65,10 +65,10 @@ protected function doPasswordReset(): mixed
}
catch (UnexpectedValueException)
{
return ResetPasswordModel::E_BAD_EMAIL;
return ResetPasswordModel::ERROR_BAD_EMAIL;
}

if (!$this->model->user) return ResetPasswordModel::E_USER_NOT_FOUND;
if (!$this->model->user) return ResetPasswordModel::ERROR_USER_NOT_FOUND;

if (empty($this->model->token))
{
Expand All @@ -79,35 +79,35 @@ protected function doPasswordReset(): mixed
)
);

return $this->model->user->commit() ? self::sendEmail() : ResetPasswordModel::E_INTERNAL_ERROR;
return $this->model->user->commit() ? self::sendEmail() : ResetPasswordModel::ERROR_INTERNAL;
}

if ($this->model->token !== $this->model->user->getVerifierToken())
return ResetPasswordModel::E_BAD_TOKEN;
return ResetPasswordModel::ERROR_BAD_TOKEN;

if ($this->model->pw1 !== $this->model->pw2)
return ResetPasswordModel::E_PASSWORD_MISMATCH;
return ResetPasswordModel::ERROR_PASSWORD_MISMATCH;

$req = Config::get('bnetdocs.user_register_requirements') ?? [];
$pwlen = strlen($this->model->pw1);

if (is_numeric($req->password_length_max) && $pwlen > $req->password_length_max)
return ResetPasswordModel::E_PASSWORD_TOO_LONG;
return ResetPasswordModel::ERROR_PASSWORD_TOO_LONG;

if (is_numeric($req->password_length_min) && $pwlen < $req->password_length_min)
return ResetPasswordModel::E_PASSWORD_TOO_SHORT;
return ResetPasswordModel::ERROR_PASSWORD_TOO_SHORT;

if (!$req->password_allow_email && stripos($this->model->pw1, $this->model->user->getEmail()))
return ResetPasswordModel::E_PASSWORD_CONTAINS_EMAIL;
return ResetPasswordModel::ERROR_PASSWORD_CONTAINS_EMAIL;

if (!$req->password_allow_username && stripos($this->model->pw1, $this->model->user->getUsername()))
return ResetPasswordModel::E_PASSWORD_CONTAINS_USERNAME;
return ResetPasswordModel::ERROR_PASSWORD_CONTAINS_USERNAME;

if ($this->model->user->isDisabled()) return ResetPasswordModel::E_USER_DISABLED;
if ($this->model->user->isDisabled()) return ResetPasswordModel::ERROR_USER_DISABLED;

$this->model->user->setPassword($this->model->pw1);
$this->model->user->setVerified(true);
return $this->model->user->commit() ? ResetPasswordModel::E_SUCCESS : ResetPasswordModel::E_INTERNAL_ERROR;
return $this->model->user->commit() ? false : ResetPasswordModel::ERROR_INTERNAL;
}

protected function sendEmail(): mixed
Expand Down Expand Up @@ -186,9 +186,9 @@ protected function sendEmail(): mixed
}
catch (\PHPMailer\PHPMailer\Exception)
{
return ResetPasswordModel::E_INTERNAL_ERROR;
return ResetPasswordModel::ERROR_INTERNAL;
}

return ResetPasswordModel::E_SUCCESS;
return false;
}
}
3 changes: 0 additions & 3 deletions src/Models/Comment/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

class Create extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?\BNETDocs\Libraries\Comment $comment = null;
public ?string $origin = null;
Expand Down
3 changes: 0 additions & 3 deletions src/Models/Comment/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

class Delete extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_FOUND = 'NOT_FOUND';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?\BNETDocs\Libraries\Comment $comment = null;
public ?string $content = null;
Expand Down
3 changes: 0 additions & 3 deletions src/Models/Comment/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

class Edit extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_FOUND = 'NOT_FOUND';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?\BNETDocs\Libraries\Comment $comment = null;
public ?string $content = null;
Expand Down
3 changes: 3 additions & 0 deletions src/Models/Core/AccessControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

class AccessControl extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

/**
* Stores whether $this->active_user is permitted to access this controller.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Core/Errorable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class Errorable extends \BNETDocs\Models\Base implements \JsonSerializable
{
public const ERROR_INTERNAL = 'INTERNAL_ERROR';

/**
* Stores error state information between Controller and downstream handlers, useful for Template rendering.
*
Expand Down
3 changes: 0 additions & 3 deletions src/Models/Document/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

class Create extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?string $brief = null;
public ?string $content = null;
Expand Down
3 changes: 0 additions & 3 deletions src/Models/Document/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

class Delete extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_FOUND = 'NOT_FOUND';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?\BNETDocs\Libraries\Document $document = null;
public ?int $id = null;
Expand Down
3 changes: 0 additions & 3 deletions src/Models/Document/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

class Edit extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_FOUND = 'NOT_FOUND';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?string $brief = null;
public ?string $category = null;
Expand Down
3 changes: 0 additions & 3 deletions src/Models/News/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

class Create extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?int $category_id = null;
public string $content = '';
Expand Down
3 changes: 0 additions & 3 deletions src/Models/News/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

class Delete extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_FOUND = 'NOT_FOUND';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?int $id = null;
public ?\BNETDocs\Libraries\News\Post $news_post = null;
Expand Down
3 changes: 0 additions & 3 deletions src/Models/News/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

class Edit extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_FOUND = 'NOT_FOUND';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?int $category = null;
public ?array $comments = null;
Expand Down
Loading

0 comments on commit b16682d

Please sign in to comment.