1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 14:12:44 +01:00
invoiceninja/app/Http/Middleware/Authenticate.php

109 lines
2.8 KiB
PHP
Raw Normal View History

2015-03-12 01:44:39 +01:00
<?php namespace App\Http\Middleware;
use Closure;
use Auth;
use Session;
use App\Models\Invitation;
use App\Models\Contact;
use App\Models\Account;
2015-03-12 01:44:39 +01:00
class Authenticate {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $guard = 'user')
2015-03-12 01:44:39 +01:00
{
$authenticated = Auth::guard($guard)->check();
2016-05-24 23:45:38 +02:00
if($guard=='client'){
if(!empty($request->invitation_key)){
$contact_key = session('contact_key');
if($contact_key) {
$contact = $this->getContact($contact_key);
$invitation = $this->getInvitation($request->invitation_key);
if (!$invitation) {
return response()->view('error', [
'error' => trans('texts.invoice_not_found'),
'hideHeader' => true,
]);
}
2016-05-24 23:02:28 +02:00
2016-05-24 23:45:38 +02:00
if ($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:02:28 +02:00
}
2016-05-24 23:45:38 +02:00
2016-05-24 23:02:28 +02:00
if (!empty($request->contact_key)) {
$contact_key = $request->contact_key;
Session::put('contact_key', $contact_key);
} else {
$contact_key = session('contact_key');
}
if ($contact_key) {
$contact = $this->getContact($contact_key);
} elseif (!empty($request->invitation_key)) {
$invitation = $this->getInvitation($request->invitation_key);
2016-05-24 23:45:38 +02:00
$contact = $invitation->contact;
Session::put('contact_key', $contact->contact_key);
2016-05-24 23:02:28 +02:00
} else {
return \Redirect::to('client/sessionexpired');
}
2016-05-24 23:45:38 +02:00
$account = $contact->account;
2016-05-24 23:02:28 +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;
}
// Does this account require portal passwords?
if($account && (!$account->enable_portal_password || !$account->hasFeature(FEATURE_CLIENT_PORTAL_PASSWORD))){
$authenticated = true;
}
2016-05-24 23:02:28 +02:00
if(!$authenticated && $contact && !$contact->password){
$authenticated = true;
}
}
if (!$authenticated)
2015-03-12 01:44:39 +01: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
}
}
return $next($request);
}
protected function getInvitation($key){
2016-03-14 19:49:38 +01:00
$invitation = Invitation::withTrashed()->where('invitation_key', '=', $key)->first();
if ($invitation && !$invitation->is_deleted) {
return $invitation;
}
else return null;
}
2016-05-24 23:02:28 +02:00
protected function getContact($key){
$contact = Contact::withTrashed()->where('contact_key', '=', $key)->first();
if ($contact && !$contact->is_deleted) {
return $contact;
}
else return null;
}
2015-03-12 01:44:39 +01:00
}