| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- /**
- * Created by PhpStorm.
- * User: giancarlo
- * Date: 28/06/2018
- * Time: 16:52
- */
- namespace OpenMind\Web;
- use OpenMind\System\User;
- class Login extends Controller
- {
- public function Login($f3)
- {
- if ($f3->exists('SESSION.user_id')) {
- $f3->reroute('/homemenu');
- return;
- }
- $f3->clear('SESSION');
- $f3->clear('COOKIE.username');
- $f3->set('CONTENT', 'login.tpl');
- echo \Template::instance()->render('masterpage.tpl');
- }
- /**
- * Check if login is valid
- *
- * @param string $username
- * @param string $password
- * @return int|null
- */
- private function checkLogin(string $username, string $password): ?int
- {
- $accounts = \F3::get('users.PASSWORD');
- foreach ($accounts as $index => $account) {
- $exist = password_verify("{$username}|{$password}", $account);
- if ($exist)
- return $index;
- }
- return null;
- }
- /**
- * Check if username and password are correct
- *
- * @param $f3
- */
- public function LoginForm($f3)
- {
- $accountData = $this->getSanitizedPostRequest();
- $userId = $this->checkLogin($accountData['username'], $accountData['password']);
- if (is_null($userId)) {
- http_response_code(500);
- echo "Dati di accesso non corretti";
- return;
- }
- $f3->set('SESSION.user_id', $userId);
- $user = new User($userId, $accountData['username']);
- $user->Save();
- $f3->set('USERID', $userId);
- $f3->set('COOKIE.username', $accountData['username']);
- $f3->reroute('/homemenu');
- }
- public function Logout($f3)
- {
- $f3->clear('SESSION');
- $f3->clear('COOKIE.username');
- $f3->reroute('/');
- }
- }
|