Skip to content

Commit

Permalink
Add account extension
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofzylka committed Feb 16, 2024
1 parent 211eb7a commit a6db0d9
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 21 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"krzysztofzylka/console": "^1.0.1",
"krzysztofzylka/env": "^1.0.1",
"krzysztofzylka/html-generator": "^2.0.5",
"krzysztofzylka/hash": "^1.0.2",
"maximebf/debugbar": ">=1.19.1",
"twig/twig": "^3.8",
"dragonmantank/cron-expression": "^v3.3.3"
Expand Down
7 changes: 4 additions & 3 deletions example/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ TIMEZONE=Europe/Warsaw
DEFAULT_CONTROLLER=index
DEFAULT_METHOD=index
DATABASE=true
DATABASE_HOST=localhost
DATABASE_HOST=mysql
DATABASE_USERNAME=root
DATABASE_PASSWORD=root
DATABASE_NAME=microframework
DATABASE_PASSWORD=password
DATABASE_NAME=microframework
DATABASE_PORT=3306
49 changes: 49 additions & 0 deletions example/src/Controller/account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace src\Controller;

use Krzysztofzylka\MicroFramework\Controller;

class account extends Controller
{
/**
* @return void
* @throws MicroFrameworkException
* @throws NotFoundException
*/
public function register(): void
{
if ($this->data) {
$register = \Krzysztofzylka\MicroFramework\Extension\Authorization\Account::register($this->data['account']['email'], $this->data['account']['password']);

if ($register) {
$this->response->toast('Poprawnie zarejestrowano użytkownika');
} else {
$this->response->toast('Błąd rejestracji', '', 'ERR');
}
}

$this->loadView();
}

/**
* @return void
* @throws MicroFrameworkException
* @throws NotFoundException
*/
public function login(): void
{
if ($this->data) {
$register = \Krzysztofzylka\MicroFramework\Extension\Authorization\Account::login($this->data['account']['email'], $this->data['account']['password']);

if ($register) {
$this->response->toast('Poprawnie zalogowano');
} else {
$this->response->toast('Błąd logowania', '', 'ERR');
}
}

$this->loadView();
}

}
17 changes: 0 additions & 17 deletions example/src/Controller/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function index(): void
$this->dialogboxTitle = 'Test dialogbox';
$this->loadModel('test');
$this->set('variable', 'Test variable');
DebugBar::addMessage($this->Test->findAll(), 'Find');
$this->set('time', time());
$this->loadView();
}
Expand Down Expand Up @@ -61,24 +60,8 @@ public function form(): void

public function table(): void
{
// (new CreateTable())
// ->setName('test')
// ->addIdColumn()
// ->addSimpleVarcharColumn('a', 100)
// ->addSimpleVarcharColumn('b')
// ->addDateModifyColumn()
// ->addDateCreatedColumn()
// ->execute();

$this->loadModel('test');

// for ($i=0; $i<=600; $i++) {
// $this->Test->setId(null)->save([
// 'a' => Generator::uniqId(50),
// 'b' => Generator::uniqId(50)
// ]);
// }

$table = $this->loader->table();
$table->addAction(
'Generate new data',
Expand Down
7 changes: 7 additions & 0 deletions example/src/View/account/login.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<form method="POST" action="/account/login">
Login:<br />
<input type="text" name="account[email]" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" />
Hasło:<br />
<input type="text" name="account[password]" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" />
<button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Submit</button>
</form>
7 changes: 7 additions & 0 deletions example/src/View/account/register.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<form method="POST" action="/account/register">
Login:<br />
<input type="text" name="account[email]" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" />
Hasło:<br />
<input type="text" name="account[password]" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" />
<button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Submit</button>
</form>
5 changes: 4 additions & 1 deletion src/Default/.env
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ DATABASE_NAME=
DATABASE_USERNAME=
DATABASE_PASSWORD=
DATABASE_CHARSET=utf8
DATABASE_PORT=3306
DATABASE_PORT=3306
AUTHORIZATION_REGISTER=TRUE
AUTHORIZATION_SESSION_NAME=micro-framework-authorization
AUTHORIZATION_HASH_ALGORITHM=pbkdf2
191 changes: 191 additions & 0 deletions src/Extension/Authorization/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php

namespace Krzysztofzylka\MicroFramework\Extension\Authorization;

use Exception;
use krzysztofzylka\DatabaseManager\Exception\DatabaseManagerException;
use krzysztofzylka\DatabaseManager\Table;
use Krzysztofzylka\Hash\Hash;
use Krzysztofzylka\MicroFramework\Extension\DebugBar\DebugBar;
use Krzysztofzylka\MicroFramework\Extension\Log\Log;

class Account
{

/**
* Table instance
* @var Table
*/
private static Table $tableInstance;

/**
* Is auth user
* @var bool
*/
private static bool $isAuth = false;

/**
* Account id
* @var ?int
*/
private static ?int $accountId = null;

/**
* Account data
* @var array
*/
private static array $account = [];

/**
* Init account
* @return void
* @throws DatabaseManagerException
*/
public static function init(): void
{
self::$tableInstance = new Table('account');

if (isset($_SESSION[$_ENV['AUTHORIZATION_SESSION_NAME']])) {
self::setAccountId($_SESSION[$_ENV['AUTHORIZATION_SESSION_NAME']]);
self::setAuth(true);
self::setAccount(self::getAccountId());
DebugBar::addFrameworkMessage('Account logged by session as ' . self::getAccountId(), 'Account');
}
}

/**
* Get account id
* @return int|null
*/
public static function getAccountId(): ?int
{
return self::$accountId;
}

/**
* Set account id
* @param int|null $accountId
* @return void
*/
public static function setAccountId(?int $accountId): void
{
self::$accountId = $accountId;
}

/**
* Is auth user
* @return bool
*/
public static function isAuth(): bool
{
return self::$isAuth;
}

/**
* Set auth user
* @param bool $isAuth
* @return void
*/
public static function setAuth(bool $isAuth): void
{
self::$isAuth = $isAuth;
}

/**
* Get account data
* @return array
*/
public static function getAccount(): array
{
return self::$account;
}

/**
* Set account data
* @param int|array $account
* @return void
* @throws DatabaseManagerException
*/
public static function setAccount(int|array $account): void
{
if (is_int($account)) {
$find = self::$tableInstance->find(['account.id' => $account]);

if ($find) {
$account = $find;
}
}

self::$account = $account;
}

/**
* Register user
* @param string $login
* @param string $password
* @return bool
* @throws DatabaseManagerException
*/
public static function register(string $login, string $password): bool
{
try {
$findAccount = self::$tableInstance->findIsset(['account.email' => $login]);

if ($findAccount) {
return false;
}

$password = Hash::hash($password, $_ENV['AUTHORIZATION_HASH_ALGORITHM']);

self::$tableInstance->insert(['email' => $login, 'password' => $password]);

return true;
} catch (Exception $exception) {
Log::throwableLog($exception, 'Register error');

throw new $exception;
}
}

/**
* Login user
* @param string $login
* @param string $password
* @return bool
* @throws DatabaseManagerException
*/
public static function login(string $login, string $password): bool
{
try {
$findAccount = self::$tableInstance->find(['account.email' => $login], ['account.id', 'account.password']);

if (!$findAccount || ($findAccount && !Hash::checkHash($findAccount['account']['password'], $password))) {
return false;
}

$_SESSION[$_ENV['AUTHORIZATION_SESSION_NAME']] = $findAccount['account']['id'];
Account::setAccountId($_SESSION[$_ENV['AUTHORIZATION_SESSION_NAME']] );
Account::setAccount($_SESSION[$_ENV['AUTHORIZATION_SESSION_NAME']] );
Account::setAuth(true);

return true;
} catch (Exception $exception) {
Log::throwableLog($exception, 'Login error');

throw new $exception;
}
}

/**
* Logout user
* @return void
*/
public static function logout(): void
{
unset($_SESSION[$_ENV['AUTHORIZATION_SESSION_NAME']]);
Account::setAccountId(null);
Account::setAccount([]);
Account::setAuth(false);
}

}
5 changes: 5 additions & 0 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Krzysztofzylka\MicroFramework\Exception\MicroFrameworkException;
use Krzysztofzylka\MicroFramework\Exception\NotFoundException;
use Krzysztofzylka\MicroFramework\Extension\Ajax\Ajax;
use Krzysztofzylka\MicroFramework\Extension\Authorization\Account;
use Krzysztofzylka\MicroFramework\Extension\DebugBar\DebugBar;
use Krzysztofzylka\MicroFramework\Extension\Log\Log;
use Krzysztofzylka\Request\Request;
Expand Down Expand Up @@ -95,6 +96,10 @@ public function __construct(string $projectPath)
$this->autoload();
$this->connectDatabase();

if ($_ENV['DATABASE']) {
Account::init();
}

DebugBar::timeStart('component', 'Init components');
$this->loaderInstance->initComponents();
DebugBar::timeStop('component');
Expand Down

0 comments on commit a6db0d9

Please sign in to comment.