1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use Cartalyst\Sentinel\Checkpoints\ThrottlingException; |
6
|
|
|
use Respect\Validation\Validator as V; |
7
|
|
|
use Slim\Http\Request; |
8
|
|
|
use Slim\Http\Response; |
9
|
|
|
|
10
|
|
|
class AuthController extends Controller |
11
|
|
|
{ |
12
|
|
|
public function login(Request $request, Response $response) |
13
|
|
|
{ |
14
|
|
|
if ($request->isPost()) { |
15
|
|
|
$credentials = [ |
16
|
|
|
'username' => $request->getParam('username'), |
17
|
|
|
'password' => $request->getParam('password') |
18
|
|
|
]; |
19
|
|
|
$remember = $request->getParam('remember') ? true : false; |
20
|
|
|
|
21
|
|
|
try { |
22
|
|
|
if ($this->auth->authenticate($credentials, $remember)) { |
23
|
|
|
$this->flash('success', 'You are now logged in.'); |
24
|
|
|
|
25
|
|
|
return $this->redirect($response, 'home'); |
26
|
|
|
} else { |
27
|
|
|
$this->validator->addError('auth', 'Bad username or password'); |
28
|
|
|
} |
29
|
|
|
} catch (ThrottlingException $e) { |
30
|
|
|
$this->validator->addError('auth', 'Too many attempts!'); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->twig->render($response, 'auth/login.twig'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function register(Request $request, Response $response) |
38
|
|
|
{ |
39
|
|
|
if ($request->isPost()) { |
40
|
|
|
$username = $request->getParam('username'); |
41
|
|
|
$email = $request->getParam('email'); |
42
|
|
|
$password = $request->getParam('password'); |
43
|
|
|
|
44
|
|
|
$this->validator->request($request, [ |
45
|
|
|
'username' => V::length(3, 25)->alnum('_')->noWhitespace(), |
46
|
|
|
'email' => V::noWhitespace()->email(), |
47
|
|
|
'password' => [ |
48
|
|
|
'rules' => V::noWhitespace()->length(6, 25), |
49
|
|
|
'messages' => [ |
50
|
|
|
'length' => 'The password length must be between {{minValue}} and {{maxValue}} characters' |
51
|
|
|
] |
52
|
|
|
], |
53
|
|
|
'password_confirm' => [ |
54
|
|
|
'rules' => V::equals($password), |
55
|
|
|
'messages' => [ |
56
|
|
|
'equals' => 'Passwords don\'t match' |
57
|
|
|
] |
58
|
|
|
] |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
if ($this->auth->findByCredentials(['login' => $username])) { |
62
|
|
|
$this->validator->addError('username', 'This username is already used.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->auth->findByCredentials(['login' => $email])) { |
66
|
|
|
$this->validator->addError('email', 'This email is already used.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($this->validator->isValid()) { |
70
|
|
|
$role = $this->auth->findRoleByName('User'); |
71
|
|
|
|
72
|
|
|
$user = $this->auth->registerAndActivate([ |
73
|
|
|
'username' => $username, |
74
|
|
|
'email' => $email, |
75
|
|
|
'password' => $password, |
76
|
|
|
'permissions' => [ |
77
|
|
|
'user.delete' => 0 |
78
|
|
|
] |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
$role->users()->attach($user); |
82
|
|
|
|
83
|
|
|
$this->flash('success', 'Your account has been created.'); |
84
|
|
|
|
85
|
|
|
return $this->redirect($response, 'login'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->twig->render($response, 'auth/register.twig'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function logout(Request $request, Response $response) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$this->auth->logout(); |
95
|
|
|
|
96
|
|
|
return $this->redirect($response, 'home'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.