|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Auth; |
|
4
|
|
|
|
|
5
|
|
|
// controllers |
|
6
|
|
|
use App\Http\Controllers\Admin\helpdesk\SocialMedia\SocialMediaController; |
|
7
|
|
|
use App\Http\Controllers\Common\PhpMailController; |
|
8
|
|
|
// requests |
|
9
|
|
|
use App\Http\Controllers\Controller; |
|
10
|
|
|
use App\Http\Requests\helpdesk\LoginRequest; |
|
11
|
|
|
use App\Http\Requests\helpdesk\OtpVerifyRequest; |
|
12
|
|
|
use App\Http\Requests\helpdesk\RegisterRequest; |
|
13
|
|
|
use App\Model\helpdesk\Settings\CommonSettings; |
|
14
|
|
|
use App\Model\helpdesk\Settings\Plugin; |
|
15
|
|
|
use App\Model\helpdesk\Settings\Security; |
|
16
|
|
|
use App\Model\helpdesk\Ticket\Ticket_Thread; |
|
17
|
|
|
// classes |
|
18
|
|
|
use App\Model\helpdesk\Ticket\Tickets; |
|
19
|
|
|
use App\Model\helpdesk\Utility\Otp; |
|
20
|
|
|
use App\User; |
|
21
|
|
|
use Auth; |
|
22
|
|
|
use DateTime; |
|
23
|
|
|
use DB; |
|
24
|
|
|
use Hash; |
|
25
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; |
|
26
|
|
|
use Input; |
|
27
|
|
|
use Lang; |
|
28
|
|
|
use Socialite; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* --------------------------------------------------- |
|
32
|
|
|
* AuthController |
|
33
|
|
|
* --------------------------------------------------- |
|
34
|
|
|
* This controller handles the registration of new users, as well as the |
|
35
|
|
|
* authentication of existing users. By default, this controller uses |
|
36
|
|
|
* a simple trait to add these behaviors. Why don't you explore it? |
|
37
|
|
|
* |
|
38
|
|
|
* @author Ladybird <[email protected]> |
|
39
|
|
|
*/ |
|
40
|
|
|
class AuthController extends Controller |
|
41
|
|
|
{ |
|
42
|
|
|
use AuthenticatesAndRegistersUsers; |
|
43
|
|
|
/* to redirect after login */ |
|
44
|
|
|
|
|
45
|
|
|
// if auth is agent |
|
46
|
|
|
protected $redirectTo = '/dashboard'; |
|
47
|
|
|
// if auth is user |
|
48
|
|
|
protected $redirectToUser = '/profile'; |
|
49
|
|
|
/* Direct After Logout */ |
|
50
|
|
|
protected $redirectAfterLogout = '/'; |
|
51
|
|
|
protected $loginPath = '/auth/login'; |
|
52
|
|
|
protected $social; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Create a new authentication controller instance. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \Illuminate\Contracts\Auth\Guard $auth |
|
|
|
|
|
|
58
|
|
|
* @param \Illuminate\Contracts\Auth\Registrar $registrar |
|
|
|
|
|
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
|
|
|
|
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(PhpMailController $PhpMailController, SocialMediaController $social) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->PhpMailController = $PhpMailController; |
|
|
|
|
|
|
65
|
|
|
$social->configService(); |
|
66
|
|
|
$this->middleware('guest', ['except' => ['getLogout', 'verifyOTP', 'redirectToProvider']]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function redirectToProvider($provider, $redirect = '') |
|
70
|
|
|
{ |
|
71
|
|
|
if ($redirect !== '') { |
|
72
|
|
|
$this->setSession($provider, $redirect); |
|
73
|
|
|
} |
|
74
|
|
|
//dd(\Config::get('services')); |
|
|
|
|
|
|
75
|
|
|
$s = Socialite::driver($provider)->redirect(); |
|
76
|
|
|
//dd('dscd'); |
|
77
|
|
|
return $s; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function handleProviderCallback($provider) |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
|
|
//notice we are not doing any validation, you should do it |
|
84
|
|
|
$this->changeRedirect(); |
|
85
|
|
|
|
|
86
|
|
|
$user = Socialite::driver($provider)->user(); |
|
87
|
|
|
if ($user) { |
|
88
|
|
|
// stroing data to our use table and logging them in |
|
89
|
|
|
$username = $user->getEmail(); |
|
90
|
|
|
$first_name = $user->getName(); |
|
91
|
|
|
if ($user->nickname) { |
|
92
|
|
|
$username = $user->nickname; |
|
93
|
|
|
} |
|
94
|
|
|
if (!$first_name) { |
|
95
|
|
|
$first_name = $username; |
|
96
|
|
|
} |
|
97
|
|
|
$data = [ |
|
98
|
|
|
'first_name' => $first_name, |
|
99
|
|
|
'email' => $user->getEmail(), |
|
100
|
|
|
'user_name' => $username, |
|
101
|
|
|
'role' => 'user', |
|
102
|
|
|
'active' => 1, |
|
103
|
|
|
]; |
|
104
|
|
|
$user = User::where('email', $data['email'])->first(); |
|
105
|
|
|
if (!$user) { |
|
106
|
|
|
$user = User::where('user_name', $data['user_name'])->first(); |
|
107
|
|
|
} |
|
108
|
|
|
if (!$user) { |
|
109
|
|
|
$user = User::firstOrCreate($data); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
Auth::login($user); |
|
112
|
|
|
} |
|
113
|
|
|
//after login redirecting to home page |
|
114
|
|
|
return redirect('/'); |
|
115
|
|
|
} catch (\Exception $ex) { |
|
116
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the form for registration. |
|
122
|
|
|
* |
|
123
|
|
|
* @return type Response |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getRegister(CommonSettings $settings) |
|
126
|
|
|
{ |
|
127
|
|
|
// Event for login |
|
128
|
|
|
$settings = $settings->select('status')->where('option_name', '=', 'send_otp')->first(); |
|
|
|
|
|
|
129
|
|
|
$email_mandatory = $settings->select('status')->where('option_name', '=', 'email_mandatory')->first(); |
|
130
|
|
|
//dd($settings->status); |
|
|
|
|
|
|
131
|
|
|
\Event::fire(new \App\Events\FormRegisterEvent()); |
|
132
|
|
View Code Duplication |
if (Auth::user()) { |
|
|
|
|
|
|
133
|
|
|
if (Auth::user()->role == 'admin' || Auth::user()->role == 'agent') { |
|
134
|
|
|
return \Redirect::route('dashboard'); |
|
135
|
|
|
} elseif (Auth::user()->role == 'user') { |
|
|
|
|
|
|
136
|
|
|
// return view('auth.register'); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
} else { |
|
139
|
|
|
return view('auth.register', compact('settings', 'email_mandatory')); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Post registration form. |
|
145
|
|
|
* |
|
146
|
|
|
* @param type User $user |
|
147
|
|
|
* @param type RegisterRequest $request |
|
148
|
|
|
* |
|
149
|
|
|
* @return type Response |
|
150
|
|
|
*/ |
|
151
|
|
|
public function postRegister(User $user, RegisterRequest $request) |
|
152
|
|
|
{ |
|
153
|
|
|
try { |
|
154
|
|
|
$request_array = $request->input(); |
|
155
|
|
|
$password = Hash::make($request->input('password')); |
|
156
|
|
|
$user->password = $password; |
|
|
|
|
|
|
157
|
|
|
$name = $request->input('full_name'); |
|
158
|
|
|
$user->first_name = $name; |
|
|
|
|
|
|
159
|
|
View Code Duplication |
if ($request_array['email'] == '') { |
|
|
|
|
|
|
160
|
|
|
$user->email = null; |
|
|
|
|
|
|
161
|
|
|
} else { |
|
162
|
|
|
$user->email = $request->input('email'); |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
View Code Duplication |
if ($request_array['mobile'] == '') { |
|
|
|
|
|
|
165
|
|
|
$user->mobile = null; |
|
|
|
|
|
|
166
|
|
|
} else { |
|
167
|
|
|
$user->mobile = $request->input('mobile'); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
if ($request_array['code'] == '') { |
|
170
|
|
|
$user->country_code = 0; |
|
|
|
|
|
|
171
|
|
|
} else { |
|
172
|
|
|
$user->country_code = $request->input('code'); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
View Code Duplication |
if ($request_array['email'] != '') { |
|
|
|
|
|
|
175
|
|
|
$user->user_name = $request->input('email'); |
|
|
|
|
|
|
176
|
|
|
} else { |
|
177
|
|
|
$user->user_name = $request->input('mobile'); |
|
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
$user->role = 'user'; |
|
|
|
|
|
|
180
|
|
|
$code = str_random(60); |
|
181
|
|
|
$user->remember_token = $code; |
|
|
|
|
|
|
182
|
|
|
$user->save(); |
|
183
|
|
|
$message12 = ''; |
|
184
|
|
|
$settings = CommonSettings::select('status')->where('option_name', '=', 'send_otp')->first(); |
|
185
|
|
|
$sms = Plugin::select('status')->where('name', '=', 'SMS')->first(); |
|
186
|
|
|
// Event for login |
|
187
|
|
|
\Event::fire(new \App\Events\LoginEvent($request)); |
|
188
|
|
|
if ($request->input('email') !== '') { |
|
189
|
|
|
$var = $this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('1', '0'), $to = ['name' => $name, 'email' => $request->input('email')], $message = ['subject' => null, 'scenario' => 'registration'], $template_variables = ['user' => $name, 'email_address' => $request->input('email'), 'password_reset_link' => url('account/activate/'.$code)]); |
|
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
if ($settings->status == 1 || $settings->status == '1') { |
|
192
|
|
|
if (count($sms) > 0) { |
|
193
|
|
|
if ($sms->status == 1 || $sms->status == '1') { |
|
194
|
|
|
$message12 = Lang::get('lang.activate_your_account_click_on_Link_that_send_to_your_mail_and_moble'); |
|
195
|
|
|
} else { |
|
196
|
|
|
$message12 = Lang::get('lang.activate_your_account_click_on_Link_that_send_to_your_mail_sms_plugin_inactive_or_not_setup'); |
|
197
|
|
|
} |
|
198
|
|
|
} else { |
|
199
|
|
|
if ($request->input('email') !== '') { |
|
200
|
|
|
$message12 = Lang::get('lang.activate_your_account_click_on_Link_that_send_to_your_mail'); |
|
201
|
|
|
} else { |
|
202
|
|
|
$message12 = Lang::get('lang.account-created-contact-admin-as-we-were-not-able-to-send-opt'); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} else { |
|
206
|
|
|
$message12 = Lang::get('lang.activate_your_account_click_on_Link_that_send_to_your_mail'); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return redirect('home')->with('success', $message12); |
|
210
|
|
|
} catch (\Exception $e) { |
|
211
|
|
|
return redirect()->back()->with('fails', $e->getMessage()); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Function to activate account. |
|
217
|
|
|
* |
|
218
|
|
|
* @param type $token |
|
219
|
|
|
* |
|
220
|
|
|
* @return type redirect |
|
221
|
|
|
*/ |
|
222
|
|
|
public function accountActivate($token) |
|
223
|
|
|
{ |
|
224
|
|
|
$user = User::where('remember_token', '=', $token)->first(); |
|
225
|
|
|
if ($user) { |
|
226
|
|
|
$user->active = 1; |
|
227
|
|
|
$user->remember_token = null; |
|
228
|
|
|
$user->save(); |
|
229
|
|
|
$this->openTicketAfterVerification($user->id); |
|
230
|
|
|
|
|
231
|
|
|
return redirect('/auth/login')->with('status', 'Acount activated. Login to start'); |
|
232
|
|
|
} else { |
|
233
|
|
|
return redirect('/auth/login')->with('fails', 'Invalid Token'); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Get mail function. |
|
239
|
|
|
* |
|
240
|
|
|
* @param type $token |
|
241
|
|
|
* @param type User $user |
|
242
|
|
|
* |
|
243
|
|
|
* @return type Response |
|
244
|
|
|
*/ |
|
245
|
|
|
public function getMail($token, User $user) |
|
246
|
|
|
{ |
|
247
|
|
|
$user = $user->where('remember_token', $token)->where('active', 0)->first(); |
|
|
|
|
|
|
248
|
|
|
if ($user) { |
|
249
|
|
|
$user->active = 1; |
|
250
|
|
|
$user->save(); |
|
251
|
|
|
|
|
252
|
|
|
return redirect('auth/login'); |
|
253
|
|
|
} else { |
|
254
|
|
|
return redirect('auth/login'); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Get login page. |
|
260
|
|
|
* |
|
261
|
|
|
* @return type Response |
|
262
|
|
|
*/ |
|
263
|
|
|
public function getLogin() |
|
264
|
|
|
{ |
|
265
|
|
|
$directory = base_path(); |
|
266
|
|
|
if (file_exists($directory.DIRECTORY_SEPARATOR.'.env')) { |
|
267
|
|
View Code Duplication |
if (Auth::user()) { |
|
|
|
|
|
|
268
|
|
|
if (Auth::user()->role == 'admin' || Auth::user()->role == 'agent') { |
|
269
|
|
|
return \Redirect::route('dashboard'); |
|
|
|
|
|
|
270
|
|
|
} elseif (Auth::user()->role == 'user') { |
|
271
|
|
|
return \Redirect::route('home'); |
|
|
|
|
|
|
272
|
|
|
} |
|
273
|
|
|
} else { |
|
274
|
|
|
return view('auth.login'); |
|
|
|
|
|
|
275
|
|
|
} |
|
276
|
|
|
} else { |
|
277
|
|
|
return Redirect::route('licence'); |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Post of login page. |
|
283
|
|
|
* |
|
284
|
|
|
* @param type LoginRequest $request |
|
285
|
|
|
* |
|
286
|
|
|
* @return type Response |
|
287
|
|
|
*/ |
|
288
|
|
|
public function postLogin(LoginRequest $request) |
|
289
|
|
|
{ |
|
290
|
|
|
// dd($request->input()); |
|
|
|
|
|
|
291
|
|
|
\Event::fire('auth.login.event', []); //added 5/5/2016 |
|
|
|
|
|
|
292
|
|
|
// Set login attempts and login time |
|
293
|
|
|
$value = $_SERVER['REMOTE_ADDR']; |
|
294
|
|
|
$usernameinput = $request->input('email'); |
|
295
|
|
|
$password = $request->input('password'); |
|
296
|
|
|
if ($request->input('referer')) { |
|
297
|
|
|
$referer = 'form'; |
|
298
|
|
|
} else { |
|
299
|
|
|
$referer = '/'; |
|
300
|
|
|
} |
|
301
|
|
|
$field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'user_name'; |
|
302
|
|
|
$result = $this->confirmIPAddress($value, $usernameinput); |
|
303
|
|
|
|
|
304
|
|
|
// If attempts > 3 and time < 30 minutes |
|
305
|
|
|
$security = Security::whereId('1')->first(); |
|
306
|
|
|
if ($result == 1) { |
|
307
|
|
|
return redirect()->back()->withErrors('email', 'Incorrect details')->with(['error' => $security->lockout_message, 'referer' => $referer]); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
$check_active = User::where('email', '=', $request->input('email'))->orwhere('user_name', '=', $request->input('email'))->first(); |
|
311
|
|
|
if (!$check_active) { //check if user exists or not |
|
312
|
|
|
//if user deos not exist then return back with error that user is not registered |
|
313
|
|
|
return redirect()->back() |
|
314
|
|
|
->withInput($request->only('email', 'remember')) |
|
315
|
|
|
->withErrors([ |
|
316
|
|
|
'email' => $this->getFailedLoginMessage(), |
|
317
|
|
|
'password' => $this->getFailedLoginMessage(), |
|
318
|
|
|
])->with(['error' => Lang::get('lang.not-registered'), |
|
319
|
|
|
'referer' => $referer, ]); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
//if user exists |
|
323
|
|
|
$settings = CommonSettings::select('status')->where('option_name', '=', 'send_otp')->first(); |
|
324
|
|
|
|
|
325
|
|
|
if ($settings->status == '1' || $settings->status == 1) { // check for otp verification setting |
|
326
|
|
|
// setting is enabled |
|
327
|
|
|
$sms = Plugin::select('status')->where('name', '=', 'SMS')->first(); |
|
328
|
|
|
if ($sms) { //check sms plugin installed or not |
|
329
|
|
|
// plugin is installed |
|
330
|
|
|
if ($sms->status == 1 || $sms->status === '1') { //check plugin is active or not |
|
331
|
|
|
// plugin is active |
|
332
|
|
|
if (!$check_active->active) { //check account is active or not |
|
333
|
|
|
// account is not active show verify otp window |
|
334
|
|
|
if ($check_active->mobile) { //check user has mobile or not |
|
335
|
|
|
// user has mobile number return verify OTP screen |
|
336
|
|
|
return \Redirect::route('otp-verification') |
|
337
|
|
|
->withInput($request->input()) |
|
|
|
|
|
|
338
|
|
|
->with(['values' => $request->input(), |
|
339
|
|
|
'referer' => $referer, |
|
340
|
|
|
'name' => $check_active->first_name, |
|
341
|
|
|
'number' => $check_active->mobile, |
|
342
|
|
|
'code' => $check_active->country_code, ]); |
|
343
|
|
|
} else { |
|
344
|
|
|
goto a; //attenmpt login (be careful while using goto statements) |
|
345
|
|
|
} |
|
346
|
|
|
} else { |
|
347
|
|
|
goto a; //attenmpt login (be careful while using goto statements) |
|
348
|
|
|
} |
|
349
|
|
|
} else { |
|
350
|
|
|
goto a; //attenmpt login (be careful while using goto statements) |
|
351
|
|
|
} |
|
352
|
|
|
} else { |
|
353
|
|
|
goto a; //attenmpt login (be careful while using goto statements) |
|
354
|
|
|
} |
|
355
|
|
|
} else { |
|
356
|
|
|
// setting is disabled |
|
357
|
|
|
a: if (!$check_active->active) { //check account is active or not |
|
358
|
|
|
// if accoutn is not active return back with error message that account is inactive |
|
359
|
|
|
return redirect()->back() |
|
360
|
|
|
->withInput($request->only('email', 'remember')) |
|
361
|
|
|
->withErrors([ |
|
362
|
|
|
'email' => $this->getFailedLoginMessage(), |
|
363
|
|
|
'password' => $this->getFailedLoginMessage(), |
|
364
|
|
|
])->with(['error' => Lang::get('lang.this_account_is_currently_inactive'), |
|
365
|
|
|
'referer' => $referer, ]); |
|
366
|
|
|
} else { |
|
367
|
|
|
// try login |
|
368
|
|
|
$loginAttempts = 1; |
|
369
|
|
|
// If session has login attempts, retrieve attempts counter and attempts time |
|
370
|
|
|
if (\Session::has('loginAttempts')) { |
|
371
|
|
|
$loginAttempts = \Session::get('loginAttempts'); |
|
372
|
|
|
$loginAttemptTime = \Session::get('loginAttemptTime'); |
|
373
|
|
|
$this->addLoginAttempt($value, $usernameinput); |
|
374
|
|
|
// $credentials = $request->only('email', 'password'); |
|
|
|
|
|
|
375
|
|
|
$usernameinput = $request->input('email'); |
|
376
|
|
|
$password = $request->input('password'); |
|
377
|
|
|
$field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'user_name'; |
|
378
|
|
|
// If attempts > 3 and time < 10 minutes |
|
379
|
|
|
if ($loginAttempts > $security->backlist_threshold && (time() - $loginAttemptTime <= ($security->lockout_period * 60))) { |
|
380
|
|
|
return redirect()->back()->withErrors('email', 'incorrect email')->with('error', $security->lockout_message); |
|
381
|
|
|
} |
|
382
|
|
|
// If time > 10 minutes, reset attempts counter and time in session |
|
383
|
|
|
if (time() - $loginAttemptTime > ($security->lockout_period * 60)) { |
|
384
|
|
|
\Session::put('loginAttempts', 1); |
|
385
|
|
|
\Session::put('loginAttemptTime', time()); |
|
386
|
|
|
} |
|
387
|
|
|
} else { // If no login attempts stored, init login attempts and time |
|
388
|
|
|
\Session::put('loginAttempts', $loginAttempts); |
|
389
|
|
|
\Session::put('loginAttemptTime', time()); |
|
390
|
|
|
$this->clearLoginAttempts($value, $usernameinput); |
|
391
|
|
|
} |
|
392
|
|
|
// If auth ok, redirect to restricted area |
|
393
|
|
|
\Session::put('loginAttempts', $loginAttempts + 1); |
|
394
|
|
|
if (Auth::Attempt([$field => $usernameinput, 'password' => $password], $request->has('remember'))) { |
|
395
|
|
|
if (Auth::user()->role == 'user') { |
|
396
|
|
|
if ($request->input('referer')) { |
|
397
|
|
|
return \Redirect::route($request->input('referer')); |
|
|
|
|
|
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
return \Redirect::route('/'); |
|
401
|
|
|
} else { |
|
402
|
|
|
return redirect()->intended($this->redirectPath()); |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
} |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
return redirect()->back() |
|
409
|
|
|
->withInput($request->only('email', 'remember')) |
|
410
|
|
|
->withErrors([ |
|
411
|
|
|
'email' => $this->getFailedLoginMessage(), |
|
412
|
|
|
'password' => $this->getFailedLoginMessage(), |
|
413
|
|
|
])->with(['error' => Lang::get('lang.invalid'), |
|
414
|
|
|
'referer' => $referer, ]); |
|
415
|
|
|
// Increment login attempts |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Add login attempt. |
|
420
|
|
|
* |
|
421
|
|
|
* @param type IPaddress $value |
|
422
|
|
|
* |
|
423
|
|
|
* @return type Response |
|
424
|
|
|
*/ |
|
425
|
|
|
public function addLoginAttempt($value, $field) |
|
426
|
|
|
{ |
|
427
|
|
|
$result = DB::table('login_attempts')->where('IP', '=', $value)->first(); |
|
428
|
|
|
$data = $result; |
|
429
|
|
|
$security = Security::whereId('1')->first(); |
|
430
|
|
|
$apt = $security->backlist_threshold; |
|
431
|
|
|
if ($data) { |
|
432
|
|
|
$attempts = $data->Attempts + 1; |
|
433
|
|
|
if ($attempts == $apt) { |
|
434
|
|
|
// $result = DB::select('UPDATE login_attempts SET Attempts='.$attempts.", LastLogin=NOW() WHERE IP = '$value' OR User = '$field'"); |
|
|
|
|
|
|
435
|
|
|
$result = DB::table('login_attempts')->where('IP', '=', $value)->orWhere('User', '=', $field)->update(['Attempts' => $attempts, 'LastLogin' => date('Y-m-d H:i:s')]); |
|
|
|
|
|
|
436
|
|
|
} else { |
|
437
|
|
|
$result = DB::table('login_attempts')->where('IP', '=', $value)->orWhere('User', '=', $field)->update(['Attempts' => $attempts]); |
|
|
|
|
|
|
438
|
|
|
// $result = DB::select("UPDATE login_attempts SET Attempts=".$attempts." WHERE IP = '$value' OR User = '$field'"); |
|
|
|
|
|
|
439
|
|
|
} |
|
440
|
|
|
} else { |
|
441
|
|
|
// $result = DB::select("INSERT INTO login_attempts (Attempts,User,IP,LastLogin) values (1,'$field','$value', NOW())"); |
|
|
|
|
|
|
442
|
|
|
$result = DB::table('login_attempts')->update(['Attempts' => 1, 'User' => $field, 'IP' => $value, 'LastLogin' => date('Y-m-d H:i:s')]); |
|
|
|
|
|
|
443
|
|
|
} |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* Clear login attempt. |
|
448
|
|
|
* |
|
449
|
|
|
* @param type IPaddress $value |
|
450
|
|
|
* |
|
451
|
|
|
* @return type Response |
|
452
|
|
|
*/ |
|
453
|
|
|
public function clearLoginAttempts($value, $field) |
|
454
|
|
|
{ |
|
455
|
|
|
$data = DB::table('login_attempts')->where('IP', '=', $value)->orWhere('User', '=', $field)->update(['attempts' => '0']); |
|
456
|
|
|
|
|
457
|
|
|
return $data; |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* Confiem IP. |
|
462
|
|
|
* |
|
463
|
|
|
* @param type IPaddress $value |
|
464
|
|
|
* |
|
465
|
|
|
* @return type Response |
|
466
|
|
|
*/ |
|
467
|
|
|
public function confirmIPAddress($value, $field) |
|
468
|
|
|
{ |
|
469
|
|
|
$security = Security::whereId('1')->first(); |
|
470
|
|
|
$time = $security->lockout_period; |
|
471
|
|
|
$max_attempts = $security->backlist_threshold; |
|
472
|
|
|
$table = 'login_attempts'; |
|
473
|
|
|
$result = DB::select('SELECT Attempts, (CASE when LastLogin is not NULL and DATE_ADD(LastLogin, INTERVAL '.$time.' MINUTE)>NOW() then 1 else 0 end) as Denied '. |
|
474
|
|
|
' FROM '.$table." WHERE IP = '$value' OR User = '$field'"); |
|
475
|
|
|
$data = $result; |
|
476
|
|
|
//Verify that at least one login attempt is in database |
|
477
|
|
|
if (!$data) { |
|
478
|
|
|
return 0; |
|
479
|
|
|
} |
|
480
|
|
|
if ($data[0]->Attempts >= $max_attempts) { |
|
481
|
|
|
if ($data[0]->Denied == 1) { |
|
482
|
|
|
return 1; |
|
483
|
|
|
} else { |
|
484
|
|
|
$this->clearLoginAttempts($value, $field); |
|
485
|
|
|
|
|
486
|
|
|
return 0; |
|
487
|
|
|
} |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
return 0; |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
/** |
|
494
|
|
|
* Get Failed login message. |
|
495
|
|
|
* |
|
496
|
|
|
* @return type string |
|
497
|
|
|
*/ |
|
498
|
|
|
protected function getFailedLoginMessage() |
|
499
|
|
|
{ |
|
500
|
|
|
return Lang::get('lang.this_field_do_not_match_our_records'); |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
/** |
|
504
|
|
|
*@category function to show verify OTP page |
|
505
|
|
|
* |
|
506
|
|
|
*@param null |
|
507
|
|
|
* |
|
508
|
|
|
*@return response|view |
|
509
|
|
|
*/ |
|
510
|
|
|
public function getVerifyOTP() |
|
511
|
|
|
{ |
|
512
|
|
|
if (\Session::has('values')) { |
|
513
|
|
|
return view('auth.otp-verify'); |
|
514
|
|
|
} else { |
|
515
|
|
|
return redirect('auth/login'); |
|
516
|
|
|
} |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
/** |
|
520
|
|
|
*@category function to verify OTP |
|
521
|
|
|
* |
|
522
|
|
|
*@param $request |
|
523
|
|
|
* |
|
524
|
|
|
*@return int|string |
|
525
|
|
|
*/ |
|
526
|
|
|
public function verifyOTP(LoginRequest $request) |
|
527
|
|
|
{ |
|
528
|
|
|
$user = User::select('id', 'mobile', 'user_name')->where('email', '=', $request->input('email')) |
|
529
|
|
|
->orWhere('user_name', '=', $request->input('email'))->first(); |
|
530
|
|
|
$otp_length = strlen($request->input('otp')); |
|
531
|
|
|
if (!\Schema::hasTable('user_verification')) { |
|
532
|
|
|
$message = Lang::get('lang.opt-can-not-be-verified'); |
|
533
|
|
|
} else { |
|
534
|
|
|
$otp = Otp::select('otp', 'updated_at')->where('user_id', '=', $user->id) |
|
535
|
|
|
->first(); |
|
536
|
|
|
if ($otp != null) { |
|
537
|
|
|
if (($otp_length == 6 && !preg_match('/[a-z]/i', $request->input('otp')))) { |
|
538
|
|
|
$otp2 = Hash::make($request->input('otp')); |
|
|
|
|
|
|
539
|
|
|
$date1 = date_format($otp->updated_at, 'Y-m-d h:i:sa'); |
|
540
|
|
|
$date2 = date('Y-m-d h:i:sa'); |
|
541
|
|
|
$time1 = new DateTime($date2); |
|
542
|
|
|
$time2 = new DateTime($date1); |
|
543
|
|
|
$interval = $time1->diff($time2); |
|
544
|
|
|
if ($interval->i > 30 || $interval->h > 0) { |
|
545
|
|
|
$message = Lang::get('lang.otp-expired'); |
|
546
|
|
|
} else { |
|
547
|
|
|
if (Hash::check($request->input('otp'), $otp->otp)) { |
|
548
|
|
|
Otp::where('user_id', '=', $user->id) |
|
549
|
|
|
->update(['otp' => '']); |
|
550
|
|
|
User::where('id', '=', $user->id) |
|
551
|
|
|
->update(['active' => 1]); |
|
552
|
|
|
$this->openTicketAfterVerification($user->id); |
|
553
|
|
|
|
|
554
|
|
|
return $this->postLogin($request); |
|
555
|
|
|
} else { |
|
556
|
|
|
$message = Lang::get('lang.otp-not-matched'); |
|
557
|
|
|
} |
|
558
|
|
|
} |
|
559
|
|
|
} else { |
|
560
|
|
|
$message = Lang::get('lang.otp-invalid'); |
|
561
|
|
|
} |
|
562
|
|
|
} else { |
|
563
|
|
|
$message = Lang::get('lang.otp-not-matched'); |
|
564
|
|
|
} |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
return \Redirect::route('otp-verification') |
|
568
|
|
|
->withInput($request->input()) |
|
|
|
|
|
|
569
|
|
|
->with(['values' => $request->input(), |
|
570
|
|
|
'number' => $user->mobile, |
|
571
|
|
|
'name' => $user->user_name, |
|
572
|
|
|
'fails' => $message, ]); |
|
573
|
|
|
} |
|
574
|
|
|
|
|
575
|
|
|
public function resendOTP(OtpVerifyRequest $request) |
|
576
|
|
|
{ |
|
577
|
|
|
if (!\Schema::hasTable('user_verification') || !\Schema::hasTable('sms')) { |
|
578
|
|
|
$message = Lang::get('lang.opt-can-not-be-verified'); |
|
579
|
|
|
|
|
580
|
|
|
return $message; |
|
581
|
|
|
} else { |
|
582
|
|
|
$sms = DB::table('sms')->get(); |
|
583
|
|
|
if (count($sms) > 0) { |
|
584
|
|
|
\Event::fire(new \App\Events\LoginEvent($request)); |
|
585
|
|
|
|
|
586
|
|
|
return 1; |
|
587
|
|
|
} else { |
|
588
|
|
|
$message = Lang::get('lang.opt-can-not-be-verified'); |
|
589
|
|
|
|
|
590
|
|
|
return $message; |
|
591
|
|
|
} |
|
592
|
|
|
} |
|
593
|
|
|
} |
|
594
|
|
|
|
|
595
|
|
|
/** |
|
596
|
|
|
* @category function to change ticket status when user verifies his account |
|
597
|
|
|
* |
|
598
|
|
|
* @param int $id => user_id |
|
599
|
|
|
* |
|
600
|
|
|
* @return null |
|
601
|
|
|
* |
|
602
|
|
|
* @author [email protected] |
|
603
|
|
|
*/ |
|
604
|
|
|
public function openTicketAfterVerification($id) |
|
605
|
|
|
{ |
|
606
|
|
|
// dd($id); |
|
|
|
|
|
|
607
|
|
|
$ticket = Tickets::select('id') |
|
608
|
|
|
->where(['user_id' => $id, 'status' => 6]) |
|
609
|
|
|
->get(); |
|
610
|
|
|
Tickets::where(['user_id' => $id, 'status' => 6]) |
|
611
|
|
|
->update(['status' => 1]); |
|
612
|
|
|
if ($ticket != null) { |
|
613
|
|
|
foreach ($ticket as $value) { |
|
614
|
|
|
$ticket_id = $value->id; |
|
615
|
|
|
Ticket_Thread::where('ticket_id', '=', $ticket_id) |
|
616
|
|
|
->update(['updated_at' => date('Y-m-d H:i:s')]); |
|
617
|
|
|
} |
|
618
|
|
|
} |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
public function changeRedirect() |
|
622
|
|
|
{ |
|
623
|
|
|
$provider = \Session::get('provider'); |
|
624
|
|
|
$url = \Session::get($provider.'redirect'); |
|
625
|
|
|
\Config::set("services.$provider.redirect", $url); |
|
626
|
|
|
} |
|
627
|
|
|
|
|
628
|
|
|
public function setSession($provider, $redirect) |
|
629
|
|
|
{ |
|
630
|
|
|
$url = url($redirect); |
|
631
|
|
|
\Session::set('provider', $provider); |
|
632
|
|
|
\Session::set($provider.'redirect', $url); |
|
633
|
|
|
$this->changeRedirect(); |
|
634
|
|
|
} |
|
635
|
|
|
} |
|
636
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.