-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
211eb7a
commit a6db0d9
Showing
9 changed files
with
268 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters