2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2015-03-12 01:44:39 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use App\Models\Contact;
|
|
|
|
use App\Models\Invitation;
|
2016-03-05 04:22:54 +01:00
|
|
|
use Auth;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Closure;
|
2016-03-05 04:22:54 +01:00
|
|
|
use Session;
|
2015-03-12 01:44:39 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class Authenticate.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
|
|
|
class Authenticate
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @param string $guard
|
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next, $guard = 'user')
|
|
|
|
{
|
|
|
|
$authenticated = Auth::guard($guard)->check();
|
2016-06-04 22:02:18 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
if ($guard == 'client') {
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! empty($request->invitation_key)) {
|
2016-07-03 18:11:58 +02:00
|
|
|
$contact_key = session('contact_key');
|
|
|
|
if ($contact_key) {
|
|
|
|
$contact = $this->getContact($contact_key);
|
|
|
|
$invitation = $this->getInvitation($request->invitation_key);
|
2016-05-24 23:45:38 +02:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $invitation) {
|
2016-07-03 18:11:58 +02:00
|
|
|
return response()->view('error', [
|
|
|
|
'error' => trans('texts.invoice_not_found'),
|
|
|
|
'hideHeader' => true,
|
|
|
|
]);
|
|
|
|
}
|
2016-05-24 23:02:28 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
if ($contact && $contact->id != $invitation->contact_id) {
|
|
|
|
// This is a different client; reauthenticate
|
|
|
|
$authenticated = false;
|
|
|
|
Auth::guard($guard)->logout();
|
|
|
|
}
|
|
|
|
Session::put('contact_key', $invitation->contact->contact_key);
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 23:45:38 +02:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! empty($request->contact_key)) {
|
2016-07-03 18:11:58 +02:00
|
|
|
$contact_key = $request->contact_key;
|
|
|
|
Session::put('contact_key', $contact_key);
|
|
|
|
} else {
|
|
|
|
$contact_key = session('contact_key');
|
|
|
|
}
|
2016-05-24 23:02:28 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
if ($contact_key) {
|
|
|
|
$contact = $this->getContact($contact_key);
|
2017-01-30 20:40:43 +01:00
|
|
|
} elseif (! empty($request->invitation_key)) {
|
2016-07-03 18:11:58 +02:00
|
|
|
$invitation = $this->getInvitation($request->invitation_key);
|
|
|
|
$contact = $invitation->contact;
|
|
|
|
Session::put('contact_key', $contact->contact_key);
|
|
|
|
} else {
|
|
|
|
return \Redirect::to('client/sessionexpired');
|
|
|
|
}
|
|
|
|
$account = $contact->account;
|
2016-05-24 23:45:38 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
if (Auth::guard('user')->check() && Auth::user('user')->account_id === $account->id) {
|
|
|
|
// This is an admin; let them pretend to be a client
|
|
|
|
$authenticated = true;
|
|
|
|
}
|
2016-06-04 22:02:18 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
// Does this account require portal passwords?
|
2017-01-30 20:40:43 +01:00
|
|
|
if ($account && (! $account->enable_portal_password || ! $account->hasFeature(FEATURE_CLIENT_PORTAL_PASSWORD))) {
|
2016-07-03 18:11:58 +02:00
|
|
|
$authenticated = true;
|
|
|
|
}
|
2016-06-04 22:02:18 +02:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $authenticated && $contact && ! $contact->password) {
|
2016-07-03 18:11:58 +02:00
|
|
|
$authenticated = true;
|
|
|
|
}
|
|
|
|
}
|
2016-06-04 22:02:18 +02:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $authenticated) {
|
2016-07-03 18:11:58 +02:00
|
|
|
if ($request->ajax()) {
|
|
|
|
return response('Unauthorized.', 401);
|
|
|
|
} else {
|
|
|
|
return redirect()->guest($guard == 'client' ? '/client/login' : '/login');
|
|
|
|
}
|
|
|
|
}
|
2015-03-12 01:44:39 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
return $next($request);
|
|
|
|
}
|
2016-06-04 22:02:18 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $key
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null|static
|
|
|
|
*/
|
|
|
|
protected function getInvitation($key)
|
|
|
|
{
|
|
|
|
$invitation = Invitation::withTrashed()->where('invitation_key', '=', $key)->first();
|
2017-01-30 20:40:43 +01:00
|
|
|
if ($invitation && ! $invitation->is_deleted) {
|
2016-07-03 18:11:58 +02:00
|
|
|
return $invitation;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 23:02:28 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $key
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null|static
|
|
|
|
*/
|
|
|
|
protected function getContact($key)
|
|
|
|
{
|
|
|
|
$contact = Contact::withTrashed()->where('contact_key', '=', $key)->first();
|
2017-01-30 20:40:43 +01:00
|
|
|
if ($contact && ! $contact->is_deleted) {
|
2016-07-03 18:11:58 +02:00
|
|
|
return $contact;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2015-03-12 01:44:39 +01:00
|
|
|
}
|