|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Security\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Controller\Controller; |
|
6
|
|
|
use Cartalyst\Sentinel\Checkpoints\ThrottlingException; |
|
7
|
|
|
use Respect\Validation\Validator as V; |
|
8
|
|
|
use Slim\Http\Request; |
|
9
|
|
|
use Slim\Http\Response; |
|
10
|
|
|
|
|
11
|
|
|
class AuthController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
public function login(Request $request, Response $response) |
|
14
|
|
|
{ |
|
15
|
|
|
if ($request->isPost()) { |
|
16
|
|
|
$credentials = [ |
|
17
|
|
|
'username' => $request->getParam('username'), |
|
18
|
|
|
'password' => $request->getParam('password') |
|
19
|
|
|
]; |
|
20
|
|
|
$remember = $request->getParam('remember', false); |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
try { |
|
23
|
|
|
if ($this->auth->authenticate($credentials, $remember)) { |
|
24
|
|
|
$this->flash('success', 'You are now logged in.'); |
|
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->view->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->view->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
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: