/
AuthController.php
128 lines (95 loc) · 3.62 KB
/
AuthController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace App\Controller;
use App\Controller\Controller;
use App\Model\Database\UserGateway;
use App\Model\Validations\AuthValidator as Validator;
use App\Model\Authorizer;
use App\Model\Helper;
use App\Model\Entity\User;
use App\View\View;
class AuthController extends Controller
{
protected $database;
protected $authorizer;
protected $view;
public function __construct(UserGateway $database, Authorizer $authorizer, View $view)
{
$this->database = $database;
$this->authorizer = $authorizer;
$this->view = $view;
}
public function register()
{
if ($this->getLogged()) {
$this->redirect();
die();
}
$post = array();
$errors = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$post['login'] = (isset($_POST['login']) and is_scalar($_POST['login'])) ? $_POST['login'] : '';
$post['name'] = (isset($_POST['name']) and is_scalar($_POST['name'])) ? $_POST['name'] : '';
$post['password'] = (isset($_POST['password']) and is_scalar($_POST['password'])) ? $_POST['password'] : '';
$post['retryPassword'] = (isset($_POST['retryPassword']) and is_scalar($_POST['retryPassword'])) ? $_POST['retryPassword']: '';
$post['login'] = trim($post['login']);
$post['name'] = trim($post['name']);
$post['password'] = trim($post['password']);
$post['retryPassword'] = trim($post['retryPassword']);
$errors = Validator::validateRegistrationPost($post);
if ($this->database->getUserByColumn('login', $post['login'])) {
$errors['login'] = "Login already exist";
}
if (empty($errors)) {
$this->authorizer->register($post['login'], $post['name'], $post['password']);
$this->login();
$this->redirect();
die();
}
}
$this->view->renderRegistrationPage(compact('post', 'errors'));
}
public function login()
{
if ($this->getLogged()) {
$this->redirect();
die();
}
$post = array();
$errors = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$post['login'] = (isset($_POST['login']) and is_scalar($_POST['login'])) ? $_POST['login'] : '';
$post['password'] = (isset($_POST['password']) and is_scalar($_POST['password'])) ? $_POST['password'] : '';
$post['login'] = trim($post['login']);
$post['password'] = trim($post['password']);
$errors = Validator::validateLoginPost($post);
if (empty($errors)) {
$user = $this->authorizer->login($post['login'], $post['password']);
if ($user) {
$this->redirect();
die();
} else {
$errors['login'] = "No matches found";
}
}
}
$this->view->renderLoginPage(compact('post', 'errors'));
}
public function logout() {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (Validator::validateToken($_POST['token']) and $this->getLogged()) {
$this->authorizer->logout();
}
}
$this->redirect();
}
public function getLogged()
{
if (isset($_COOKIE['id']) and isset($_COOKIE['token'])) {
$id = $_COOKIE['id'];
$hash = $_COOKIE['hash'];
$user = $this->authorizer->getLogged($id, $hash);
return $user;
}
return false;
}
}