1: <?php
2:
3: 4: 5:
6:
7: namespace app\controllers;
8:
9:
10: use \mako\auth\Gatekeeper;
11: use \mako\http\routing\Controller;
12: use \mako\syringe\ContainerAwareTrait;
13:
14: 15: 16: 17: 18:
19:
20: class Auth extends Controller
21: {
22: use ContainerAwareTrait;
23:
24: 25: 26: 27: 28: 29:
30:
31: public function signIn()
32: {
33: return $this->view->render('auth.sign_in',
34: [
35: 'token' => $this->session->generateToken(),
36: 'errors' => $this->session->getFlash('errors'),
37: 'success' => $this->session->getFlash('success'),
38: ]);
39: }
40:
41: 42: 43: 44: 45: 46:
47:
48: public function authenticate()
49: {
50: $rules =
51: [
52: 'token' => ['required', 'token'],
53: 'email' => ['required', 'email'],
54: ];
55:
56: $validator = $this->validator->create($this->request->data(), $rules);
57:
58: if($validator->isValid($errors))
59: {
60: $status = $this->gatekeeper->login($this->request->data('email'), $this->request->data('password'));
61:
62: if($status === Gatekeeper::LOGIN_ACTIVATING)
63: $errors = [$this->i18n->get('ui.needs_activation')];
64: elseif($status === Gatekeeper::LOGIN_BANNED)
65: $errors = [$this->i18n->get('ui.account_banned')];
66: elseif($status === Gatekeeper::LOGIN_INCORRECT)
67: $errors = [$this->i18n->get('ui.incorrect_login')];
68: }
69:
70: if(!empty($errors))
71: {
72: $this->session->putFlash('errors', $errors);
73:
74: return $this->response->redirect($this->urlBuilder->current());
75: }
76: else
77: {
78:
79: return $this->response->redirect(html_entity_decode($this->session->get('lasturl','home' )));
80:
81:
82: }
83: }
84:
85: 86: 87: 88: 89: 90:
91:
92: public function signOut()
93: {
94: $this->gatekeeper->logout();
95:
96: return $this->response->redirect($this->urlBuilder->toRoute('auth.sign_in'));
97: }
98: }