Skip to content

Commit

Permalink
Merge pull request #9 from netis-cms/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
accgit authored Aug 31, 2024
2 parents f2fe5ed + 6312343 commit c9aa6b6
Show file tree
Hide file tree
Showing 156 changed files with 1,021 additions and 1,294 deletions.
11 changes: 1 addition & 10 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
# Disable directory listing for security reasons
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>

# Disable listing files
<Files ~ "\.(neon|latte|log|html)$">
order allow,deny
deny from all
</Files>
Require all denied
5 changes: 0 additions & 5 deletions .sassrc.json

This file was deleted.

3 changes: 1 addition & 2 deletions app/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ public static function boot(): ExtraConfigurator
}

$app->enableTracy($appDir . '/var/log');
$app->setTimeZone('Europe/Prague');
$app->setTempDirectory($appDir . '/var');
$app->createRobotLoader()
->addDirectory(__DIR__)
->register();

// Create DI container from configuration files.
$app->addFindConfig(__DIR__, exclude: 'locales');
$app->addFindConfig(__DIR__, 'Translate');

return $app;
}
Expand Down
9 changes: 7 additions & 2 deletions app/Modules/BaseFactory.php → app/Core/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@

declare(strict_types=1);

namespace App\Modules;
namespace App\Core;

use App\Core\User\User;
use Nette\Application\UI\Form;
use Nette\Localization\Translator;


class BaseFactory
class Factory
{
public function __construct(
private readonly Translator $translator,
private readonly User $user,
) {
}


public function create(): Form
{
$form = new Form();
if ($this->user->isLoggedIn()) {
$form->addProtection();
}
$form->setTranslator($this->translator);
return $form;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Services;
namespace App\Core\Settings;


class Settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

declare(strict_types=1);

namespace App\Services;
namespace App\Core\Settings;

use Drago\Database\Entity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

declare(strict_types=1);

namespace App\Services;
namespace App\Core\Settings;

use Dibi\Connection;
use Drago\Attr\AttributeDetectionException;
use Drago\Attr\Table;
use Drago\Database\Repository;
use Drago\Database\Database;


#[Table(SettingsEntity::Table)]
class SettingsRepository
{
use Repository;
use Database;

public function __construct(
protected Connection $db,
protected Connection $connection,
) {
}

Expand All @@ -26,7 +26,7 @@ public function __construct(
*/
public function getSettings(): array
{
return $this->query()
return $this->read('*')
->fetchPairs(SettingsEntity::ColumnName, SettingsEntity::ColumnValue);
}
}
28 changes: 28 additions & 0 deletions app/Core/Settings/SettingsRequire.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Core\Settings;

use Nette\Application\UI\Presenter;
use Nette\DI\Attributes\Inject;


trait SettingsRequire
{
#[Inject]
public SettingsRepository $settingsRepository;


public function injectSettings(Presenter $presenter): void
{
$presenter->onRender[] = function () use ($presenter) {
$settings = $this->settingsRepository->getSettings();
$settingsRecords = new Settings(
website: $settings['website'],
description: $settings['description'],
);
$presenter->template->settings = $settingsRecords;
};
}
}
8 changes: 4 additions & 4 deletions app/Modules/Backend/Sign/User.php → app/Core/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

declare(strict_types=1);

namespace App\Modules\Backend\Sign;
namespace App\Core\User;


class User extends \Nette\Security\User
{
/**
* @throws UserDataIdentityException
* @throws UserIdentityException
*/
public function getUserData(string $name = null): mixed
{
$data = $this->getIdentity()->getData();
if ($name) {
if (!array_key_exists($name, $data)) {
throw new UserDataIdentityException(
throw new UserIdentityException(
'Undefined array key "' . $name . '" in identity data.',
);
}
Expand All @@ -26,7 +26,7 @@ public function getUserData(string $name = null): mixed


/**
* @throws UserDataIdentityException
* @throws UserIdentityException
*/
public function getUserIdentity(): UserIdentity
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace App\Modules\Backend\Sign;
namespace App\Core\User;

use Drago\Utils\ExtraArrayHash;


class AccountData extends ExtraArrayHash
class UserData extends ExtraArrayHash
{
public const ColumnUsername = 'username';
public const ColumnEmail = 'email';
Expand Down
12 changes: 12 additions & 0 deletions app/Core/User/UserDuplicateEmailException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Core\User;

use Exception;


class UserDuplicateEmailException extends Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Modules\Backend\Sign;
namespace App\Core\User;


class UserIdentity
Expand Down
12 changes: 12 additions & 0 deletions app/Core/User/UserIdentityException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Core\User;

use Exception;


class UserIdentityException extends Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Modules\Backend\Sign;
namespace App\Core\User;

use Dibi\Connection;
use Dibi\Exception;
Expand All @@ -11,7 +11,7 @@
use Drago\Authorization\Conf;
use Drago\Authorization\Control\Access\AccessRolesViewEntity;
use Drago\Authorization\Tracy\PanelCookie;
use Drago\Database\Repository;
use Drago\Database\Database;
use Nette\Security\AuthenticationException;
use Nette\Security\Authenticator;
use Nette\Security\IdentityHandler;
Expand All @@ -20,13 +20,13 @@
use Nette\Security\SimpleIdentity;


#[Table(UsersEntity::Table, UsersEntity::PrimaryKey)]
#[Table(UsersEntity::Table, UsersEntity::PrimaryKey, class: UsersEntity::class)]
class UserRepository implements Authenticator, IdentityHandler
{
use Repository;
use Database;

public function __construct(
protected Connection $db,
protected Connection $connection,
private readonly Passwords $password,
private readonly PanelCookie $panelCookie,
) {
Expand All @@ -44,25 +44,25 @@ public function authenticate(string $user, string $password): SimpleIdentity

// User not found.
if (!$user) {
throw new AuthenticationException('User not found.', self::IDENTITY_NOT_FOUND);
throw new AuthenticationException('User not found.', self::IdentityNotFound);

// Invalid password.
} elseif (!$this->password->verify($password, $user->password)) {
throw new AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
throw new AuthenticationException('The password is incorrect.', self::InvalidCredential);


// Re-hash password.
} elseif ($this->password->needsRehash($user->password)) {
$user->password = $this->password->hash($password);
$this->put($user->toArray());
$this->save($user->toArray());

}
$user->offsetUnset('password');
return new SimpleIdentity($user->id, $this->findUserRoles($user->id), $user);
}


public function sleepIdentity(Identity|IIdentity $identity): SimpleIdentity
public function sleepIdentity(UserToken|IIdentity $identity): SimpleIdentity
{
return new SimpleIdentity($identity->token);
}
Expand Down Expand Up @@ -101,9 +101,8 @@ public function wakeupIdentity(IIdentity $identity): ?SimpleIdentity
*/
private function findUser(string $user): array|UsersEntity|null
{
return $this->query(UsersEntity::ColumnEmail, $user)
->execute()->setRowClass(UsersEntity::class)
->fetch();
return $this->find(UsersEntity::ColumnEmail, $user)
->record();
}


Expand All @@ -113,9 +112,8 @@ private function findUser(string $user): array|UsersEntity|null
*/
private function findUserById(string $id): array|UsersEntity|null
{
return $this->query(UsersEntity::ColumnToken, $id)
->execute()->setRowClass(UsersEntity::class)
->fetch();
return $this->find(UsersEntity::ColumnToken, $id)
->record();
}


Expand All @@ -124,7 +122,8 @@ private function findUserById(string $id): array|UsersEntity|null
*/
private function findUserRoles(int $userId): array|string
{
return $this->db->select('*')->from(AccessRolesViewEntity::Table)
return $this->getConnection()
->select('*')->from(AccessRolesViewEntity::Table)
->where(AccessRolesViewEntity::ColumnUserId, '= ?', $userId)
->fetchPairs(null, AccessRolesViewEntity::ColumnRole) ?: Conf::RoleMember;
}
Expand Down
27 changes: 27 additions & 0 deletions app/Core/User/UserRequireLogged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Core\User;

use Nette\Application\UI\Presenter;


trait UserRequireLogged
{
public function injectRequireLoggedUser(Presenter $presenter, User $user): void
{
$presenter->onStartup[] = function () use ($presenter, $user) {
if ($user->isLoggedIn()) {
return;

} elseif ($user->getLogoutReason() === $user::LogoutInactivity) {
$presenter->flashMessage('You have been signed out due to inactivity. Please sign in again.');
$presenter->redirect(':Backend:Sign:in', ['backlink' => $presenter->storeRequest()]);

} else {
$presenter->redirect(':Backend:Sign:in');
}
};
}
}
Loading

0 comments on commit c9aa6b6

Please sign in to comment.