1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Dashboard |
5
|
|
|
* @author Ian Olson <[email protected]> |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2015, Laraflock |
8
|
|
|
* @link https://github.com/laraflock |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Laraflock\Dashboard\Repositories\Auth; |
12
|
|
|
|
13
|
|
|
use Cartalyst\Sentinel\Activations\IlluminateActivationRepository; |
14
|
|
|
use Cartalyst\Sentinel\Sentinel; |
15
|
|
|
use Cartalyst\Sentinel\Users\EloquentUser; |
16
|
|
|
use Illuminate\Database\QueryException; |
17
|
|
|
use Laraflock\Dashboard\Exceptions\AuthenticationException; |
18
|
|
|
use Laraflock\Dashboard\Exceptions\RolesException; |
19
|
|
|
use Laraflock\Dashboard\Repositories\Base\BaseRepository; |
20
|
|
|
|
21
|
|
|
class AuthRepository extends BaseRepository implements AuthRepositoryInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Activation interface. |
25
|
|
|
* |
26
|
|
|
* @var \Cartalyst\Sentinel\Activations\ActivationRepositoryInterface |
27
|
|
|
*/ |
28
|
|
|
protected $illuminateActivationRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sentinel instance. |
32
|
|
|
* |
33
|
|
|
* @var \Cartalyst\Sentinel\Sentinel |
34
|
|
|
*/ |
35
|
|
|
protected $sentinel; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The constructor. |
39
|
|
|
* |
40
|
|
|
* @param \Cartalyst\Sentinel\Sentinel $sentinel |
41
|
|
|
*/ |
42
|
110 |
|
public function __construct(IlluminateActivationRepository $illuminateActivationRepository, Sentinel $sentinel) |
43
|
|
|
{ |
44
|
110 |
|
$this->illuminateActivationRepository = $illuminateActivationRepository; |
45
|
110 |
|
$this->sentinel = $sentinel; |
46
|
110 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
42 |
|
public function getActiveUser() |
52
|
|
|
{ |
53
|
42 |
|
return $this->sentinel->getUser(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
5 |
|
public function check() |
60
|
|
|
{ |
61
|
5 |
|
return $this->sentinel->check(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
49 |
|
public function authenticate(array $data) |
68
|
|
|
{ |
69
|
42 |
|
$this->rules = [ |
70
|
42 |
|
'email' => 'required|email', |
71
|
42 |
|
'password' => 'required', |
72
|
|
|
]; |
73
|
|
|
|
74
|
49 |
|
$remember = false; |
75
|
|
|
|
76
|
42 |
|
if (isset($data['remember'])) { |
77
|
|
|
$remember = $data['remember']; |
78
|
|
|
} |
79
|
|
|
|
80
|
42 |
|
$this->validate($data); |
81
|
|
|
|
82
|
40 |
|
if (!$user = $this->sentinel->authenticate($data, $remember)) { |
83
|
3 |
|
throw new AuthenticationException(trans('dashboard::dashboard.errors.auth.incorrect')); |
84
|
|
|
} |
85
|
|
|
|
86
|
38 |
|
return $user; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
13 |
|
public function register(array $data, $validate = true) |
93
|
|
|
{ |
94
|
13 |
|
$this->rules = [ |
95
|
13 |
|
'email' => 'required|unique:users', |
96
|
13 |
|
'password' => 'required|confirmed', |
97
|
13 |
|
'password_confirmation' => 'required', |
98
|
|
|
]; |
99
|
|
|
|
100
|
13 |
|
if ($validate) { |
101
|
4 |
|
$this->validate($data); |
102
|
3 |
|
} |
103
|
|
|
|
104
|
12 |
|
if (!config('laraflock.dashboard.activations')) { |
105
|
4 |
|
$this->registerAndActivate($data, false); |
106
|
|
|
|
107
|
2 |
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
try { |
111
|
8 |
|
$user = $this->sentinel->register($data); |
112
|
8 |
|
} catch (QueryException $e) { |
113
|
|
|
throw new AuthenticationException(trans('dashboard::dashboard.errors.auth.create')); |
114
|
|
|
} |
115
|
|
|
|
116
|
8 |
|
if (!$user instanceof EloquentUser) { |
117
|
|
|
throw new AuthenticationException(trans('dashboard::dashboard.errors.auth.create')); |
118
|
|
|
} |
119
|
|
|
|
120
|
8 |
|
if (!isset($data['role'])) { |
121
|
1 |
|
$data['role'] = config('laraflock.dashboard.defaultRole'); |
122
|
1 |
|
} |
123
|
|
|
|
124
|
8 |
View Code Duplication |
if (!$role = $this->sentinel->findRoleBySlug($data['role'])) { |
|
|
|
|
125
|
1 |
|
throw new RolesException(trans('dashboard::dashboard.errors.role.found')); |
126
|
|
|
} |
127
|
|
|
|
128
|
7 |
|
$role->users() |
129
|
7 |
|
->attach($user); |
130
|
|
|
|
131
|
7 |
|
if (!$activation = $this->illuminateActivationRepository->create($user)) { |
132
|
|
|
throw new AuthenticationException(trans('dashboard::dashboard.errors.auth.activation.create')); |
133
|
|
|
} |
134
|
|
|
|
135
|
7 |
|
return $activation; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritDoc} |
140
|
|
|
*/ |
141
|
85 |
|
public function registerAndActivate(array $data, $validate = true) |
142
|
|
|
{ |
143
|
85 |
|
$this->rules = [ |
144
|
85 |
|
'email' => 'required|unique:users', |
145
|
85 |
|
'password' => 'required|confirmed', |
146
|
85 |
|
'password_confirmation' => 'required', |
147
|
|
|
]; |
148
|
|
|
|
149
|
85 |
|
if ($validate) { |
150
|
13 |
|
$this->validate($data); |
151
|
12 |
|
} |
152
|
|
|
|
153
|
|
|
try { |
154
|
85 |
|
$user = $this->sentinel->registerAndActivate($data); |
155
|
85 |
|
} catch (QueryException $e) { |
156
|
2 |
|
throw new AuthenticationException(trans('dashboard::dashboard.errors.auth.create')); |
157
|
|
|
} |
158
|
|
|
|
159
|
85 |
|
if (!isset($data['role'])) { |
160
|
85 |
|
$data['role'] = config('laraflock.dashboard.defaultRole'); |
161
|
85 |
|
} |
162
|
|
|
|
163
|
85 |
View Code Duplication |
if (!$role = $this->sentinel->findRoleBySlug($data['role'])) { |
|
|
|
|
164
|
2 |
|
throw new RolesException(trans('dashboard::dashboard.errors.role.found')); |
165
|
|
|
} |
166
|
|
|
|
167
|
85 |
|
$role->users() |
168
|
85 |
|
->attach($user); |
169
|
|
|
|
170
|
85 |
|
return; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritDoc} |
175
|
|
|
*/ |
176
|
6 |
|
public function activate(array $data, $validate = true) |
177
|
|
|
{ |
178
|
6 |
|
$this->rules = [ |
179
|
6 |
|
'email' => 'required|email', |
180
|
6 |
|
'activation_code' => 'required', |
181
|
|
|
]; |
182
|
|
|
|
183
|
6 |
|
if ($validate) { |
184
|
4 |
|
$this->validate($data); |
185
|
3 |
|
} |
186
|
|
|
|
187
|
5 |
|
$user = $this->findByCredentials(['login' => $data['email']]); |
188
|
|
|
|
189
|
5 |
|
if (!$this->illuminateActivationRepository->complete($user, $data['activation_code'])) { |
190
|
3 |
|
throw new AuthenticationException(trans('dashboard::dashboard.errors.auth.activation.complete')); |
191
|
|
|
} |
192
|
|
|
|
193
|
3 |
|
return true; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritDoc} |
198
|
|
|
*/ |
199
|
6 |
|
public function findByCredentials(array $data) |
200
|
|
|
{ |
201
|
6 |
|
return $this->sentinel->findByCredentials($data); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritDoc} |
206
|
|
|
*/ |
207
|
35 |
|
public function login($user) |
208
|
|
|
{ |
209
|
35 |
|
return $this->sentinel->login($user); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritDoc} |
214
|
|
|
*/ |
215
|
1 |
|
public function logout() |
216
|
|
|
{ |
217
|
1 |
|
return $this->sentinel->logout(); |
218
|
|
|
} |
219
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.