Skip to content

Commit

Permalink
upgrade composer deps
Browse files Browse the repository at this point in the history
  • Loading branch information
petrparolek committed Nov 10, 2024
1 parent 07851ea commit b4e3767
Show file tree
Hide file tree
Showing 19 changed files with 900 additions and 1,278 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: ['7.3', '7.4', '8.0', '8.1']
php-versions: ['8.1', '8.2', '8.3']
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions app/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App;

use Nette\Configurator;
use Nette\Bootstrap\Configurator;
use Tester\Environment;

class Bootstrap
Expand All @@ -29,11 +29,11 @@ public static function boot(): Configurator
return $configurator;
}


public static function bootForTests(): Configurator
{
$configurator = self::boot();
Environment::setup();

return $configurator;
}

Expand Down
4 changes: 2 additions & 2 deletions app/Forms/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace App\Forms;

use Nette;
use Nette\Application\UI\Form;
use Nette\SmartObject;

final class FormFactory
{

use Nette\SmartObject;
use SmartObject;

public function create(): Form
{
Expand Down
15 changes: 7 additions & 8 deletions app/Forms/SignInFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@

namespace App\Forms;

use Nette;
use Nette\Application\UI\Form;
use Nette\Security\AuthenticationException;
use Nette\Security\User;
use Nette\SmartObject;
use stdClass;

final class SignInFormFactory
{

use Nette\SmartObject;
use SmartObject;

/** @var FormFactory */
private $factory;
private FormFactory $factory;

/** @var User */
private $user;
private User $user;

public function __construct(FormFactory $factory, User $user)
{
$this->factory = $factory;
$this->user = $user;
}


public function create(callable $onSuccess): Form
{
$form = $this->factory->create();
Expand All @@ -42,8 +40,9 @@ public function create(callable $onSuccess): Form
try {
$this->user->setExpiration($values->remember ? '14 days' : '20 minutes');
$this->user->login($values->username, $values->password);
} catch (Nette\Security\AuthenticationException $e) {
} catch (AuthenticationException $e) {
$form->addError('The username or password you entered is incorrect.');

return;
}

Expand Down
20 changes: 10 additions & 10 deletions app/Forms/SignUpFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

namespace App\Forms;

use App\Model;
use Nette;
use App\Model\DuplicateNameException;
use App\Model\UserManager;
use Nette\Application\UI\Form;
use Nette\SmartObject;
use stdClass;

final class SignUpFormFactory
{

use Nette\SmartObject;
use SmartObject;

private const PASSWORD_MIN_LENGTH = 7;

/** @var FormFactory */
private $factory;
private FormFactory $factory;

/** @var Model\UserManager */
private $userManager;
private UserManager $userManager;

public function __construct(FormFactory $factory, Model\UserManager $userManager)
public function __construct(FormFactory $factory, UserManager $userManager)
{
$this->factory = $factory;
$this->userManager = $userManager;
Expand All @@ -38,15 +37,16 @@ public function create(callable $onSuccess): Form
$form->addPassword('password', 'Create a password:')
->setOption('description', sprintf('at least %d characters', self::PASSWORD_MIN_LENGTH))
->setRequired('Please create a password.')
->addRule($form::MIN_LENGTH, null, self::PASSWORD_MIN_LENGTH);
->addRule($form::MinLength, null, self::PASSWORD_MIN_LENGTH);

$form->addSubmit('send', 'Sign up');

$form->onSuccess[] = function (Form $form, stdClass $values) use ($onSuccess): void {
try {
$this->userManager->add($values->username, $values->email, $values->password);
} catch (Model\DuplicateNameException $e) {
} catch (DuplicateNameException $e) {
$form->addError('Username is already taken.');

return;
}

Expand Down
54 changes: 29 additions & 25 deletions app/Model/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,37 @@

namespace App\Model;

use Nette;
use Nette\Database\Explorer;
use Nette\Database\Table\ActiveRow;
use Nette\Database\UniqueConstraintViolationException;
use Nette\Security\AuthenticationException;
use Nette\Security\Authenticator;
use Nette\Security\IIdentity;
use Nette\Security\Passwords;
use Nette\Security\SimpleIdentity;
use Nette\SmartObject;
use Nette\Utils\Validators;

/**
* Users management.
*/
final class UserManager implements Nette\Security\Authenticator
final class UserManager implements Authenticator
{

use Nette\SmartObject;
use SmartObject;

private const
TABLE_NAME = 'users',
COLUMN_ID = 'id',
COLUMN_NAME = 'username',
COLUMN_PASSWORD_HASH = 'password',
COLUMN_EMAIL = 'email',
COLUMN_ROLE = 'role';
private const TABLE_NAME = 'users';
private const COLUMN_ID = 'id';
private const COLUMN_NAME = 'username';
private const COLUMN_PASSWORD_HASH = 'password';
private const COLUMN_EMAIL = 'email';
private const COLUMN_ROLE = 'role';

/** @var Nette\Database\Context */
private $database;
private Explorer $database;

/** @var Passwords */
private $passwords;
private Passwords $passwords;

public function __construct(Nette\Database\Context $database, Passwords $passwords)
public function __construct(Explorer $database, Passwords $passwords)
{
$this->database = $database;
$this->passwords = $passwords;
Expand All @@ -36,20 +41,18 @@ public function __construct(Nette\Database\Context $database, Passwords $passwor
/**
* Performs an authentication.
*
* @throws Nette\Security\AuthenticationException
* @throws AuthenticationException
*/
public function authenticate(string $username, string $password): Nette\Security\IIdentity
public function authenticate(string $username, string $password): IIdentity
{
$row = $this->database->table(self::TABLE_NAME)
->where(self::COLUMN_NAME, $username)
->fetch();

if (!$row instanceof Nette\Database\Table\ActiveRow) {
throw new Nette\Security\AuthenticationException('The username is incorrect.', self::IDENTITY_NOT_FOUND);

if (!$row instanceof ActiveRow) {
throw new AuthenticationException('The username is incorrect.', self::IdentityNotFound);
} elseif (!$this->passwords->verify($password, $row[self::COLUMN_PASSWORD_HASH])) {
throw new Nette\Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);

throw new AuthenticationException('The password is incorrect.', self::InvalidCredential);
} elseif ($this->passwords->needsRehash($row[self::COLUMN_PASSWORD_HASH])) {
$row->update([
self::COLUMN_PASSWORD_HASH => $this->passwords->hash($password),
Expand All @@ -58,7 +61,8 @@ public function authenticate(string $username, string $password): Nette\Security

$arr = $row->toArray();
unset($arr[self::COLUMN_PASSWORD_HASH]);
return new Nette\Security\SimpleIdentity($row[self::COLUMN_ID], $row[self::COLUMN_ROLE], $arr);

return new SimpleIdentity($row[self::COLUMN_ID], $row[self::COLUMN_ROLE], $arr);
}

/**
Expand All @@ -68,14 +72,14 @@ public function authenticate(string $username, string $password): Nette\Security
*/
public function add(string $username, string $email, string $password): void
{
Nette\Utils\Validators::assert($email, 'email');
Validators::assert($email, 'email');
try {
$this->database->table(self::TABLE_NAME)->insert([
self::COLUMN_NAME => $username,
self::COLUMN_PASSWORD_HASH => $this->passwords->hash($password),
self::COLUMN_EMAIL => $email,
]);
} catch (Nette\Database\UniqueConstraintViolationException $e) {
} catch (UniqueConstraintViolationException $e) {
throw new DuplicateNameException();
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/Presenters/BasePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace App\Presenters;

use Nette;
use Nette\Application\UI\Presenter;

/**
* Base presenter for all application presenters.
*/
abstract class BasePresenter extends Nette\Application\UI\Presenter
abstract class BasePresenter extends Presenter
{

}
13 changes: 8 additions & 5 deletions app/Presenters/Error4xxPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

namespace App\Presenters;

use Nette;
use Nette\Application\BadRequestException;
use Nette\Application\Request;
use Nette\Bridges\ApplicationLatte\Template;

/**
* @property-read Nette\Bridges\ApplicationLatte\Template $template
* @property-read Nette\Application\Request $request
* @property-read Template $template
* @property-read Request $request
*/
final class Error4xxPresenter extends BasePresenter
{

public function startup(): void
{
parent::startup();
if (!$this->request->isMethod(Nette\Application\Request::FORWARD)) {

if (!$this->request->isMethod(Request::FORWARD)) {
$this->error();
}
}

public function renderDefault(Nette\Application\BadRequestException $exception): void
public function renderDefault(BadRequestException $exception): void
{
// load template 403.latte or 404.latte or ... 4xx.latte
$file = __DIR__ . '/templates/Error/' . $exception->getCode() . '.latte';
Expand Down
32 changes: 19 additions & 13 deletions app/Presenters/ErrorPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,45 @@

namespace App\Presenters;

use Nette;
use Nette\Application\Responses;
use Nette\Http;
use Nette\Application\BadRequestException;
use Nette\Application\Helpers;
use Nette\Application\IPresenter;
use Nette\Application\Request;
use Nette\Application\Response;
use Nette\Application\Responses\CallbackResponse;
use Nette\Application\Responses\ForwardResponse;
use Nette\Http\IRequest;
use Nette\Http\IResponse;
use Nette\SmartObject;
use Tracy\ILogger;

final class ErrorPresenter implements Nette\Application\IPresenter
final class ErrorPresenter implements IPresenter
{

use Nette\SmartObject;
use SmartObject;

/** @var ILogger */
private $logger;
private ILogger $logger;

public function __construct(ILogger $logger)
{
$this->logger = $logger;
}

public function run(Nette\Application\Request $request): Nette\Application\Response
public function run(Request $request): Response
{
$e = $request->getParameter('exception');
if ($e instanceof Nette\Application\BadRequestException) {
if ($e instanceof BadRequestException) {
// $this->logger->log("HTTP code {$e->getCode()}: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", 'access');
[$module, , $sep] = Nette\Application\Helpers::splitName($request->getPresenterName());
[$module, , $sep] = Helpers::splitName($request->getPresenterName());
$errorPresenter = $module . $sep . 'Error4xx';

return new Responses\ForwardResponse($request->setPresenterName($errorPresenter));
return new ForwardResponse($request->setPresenterName($errorPresenter));
}

$this->logger->log($e, ILogger::EXCEPTION);

return new Responses\CallbackResponse(
static function (Http\IRequest $httpRequest, Http\IResponse $httpResponse): void {
return new CallbackResponse(
static function (IRequest $httpRequest, IResponse $httpResponse): void {
if (preg_match('#^text/html(?:;|$)#', (string) $httpResponse->getHeader('Content-Type')) > 0) {
require __DIR__ . '/templates/Error/500.phtml';
}
Expand Down
16 changes: 8 additions & 8 deletions app/Presenters/SignPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

namespace App\Presenters;

use App\Forms;
use App\Forms\SignInFormFactory;
use App\Forms\SignUpFormFactory;
use Nette\Application\UI\Form;

final class SignPresenter extends BasePresenter
{

/** @var string @persistent */
public $backlink = '';
/** @persistent */
public string $backlink = '';

/** @var Forms\SignInFormFactory */
private $signInFactory;
private SignInFormFactory $signInFactory;

/** @var Forms\SignUpFormFactory */
private $signUpFactory;
private SignUpFormFactory $signUpFactory;

public function __construct(Forms\SignInFormFactory $signInFactory, Forms\SignUpFormFactory $signUpFactory)
public function __construct(SignInFormFactory $signInFactory, SignUpFormFactory $signUpFactory)
{
parent::__construct();

$this->signInFactory = $signInFactory;
$this->signUpFactory = $signUpFactory;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Presenters/templates/Homepage/default.latte
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<section id="template">
<h2>This page template located at <span>{strstr($this->getName(), app)}</span></h2>

<pre><code class="jush">{file_get_contents($this->getName())|replacere:'#[\w+/]{60,}#',''}</code></pre>
<pre><code class="jush">{file_get_contents($this->getName())|replaceRe:'#[\w+/]{60,}#',''}</code></pre>
</section>

<section id="layout">
Expand Down
Loading

0 comments on commit b4e3767

Please sign in to comment.