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