David Bomba c6e1658ffe
Refactors (#3148)
* Refactor company properties to be presented from settings object instead of company properties

* Working on Email Tests

* Working on emails

* Working on email templats

* Include text version of email

* Refactor Email template builder into trait'

* Fix for custom_value4

* Refactor payment_date -> date && payment_type_id -> type_id

* expose paymentables to API

* expose paymentables to API

* Implement a next_send_date field in invoice/quote tables to allow control over reminder scheduling

* Add custom_values to users,documents and company_gateways tables
2019-12-16 22:34:38 +11:00

85 lines
2.3 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Middleware;
use App\Events\User\UserLoggedIn;
use App\Models\CompanyToken;
use App\Models\User;
use Closure;
class TokenAuth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if( $request->header('X-API-TOKEN') && ($company_token = CompanyToken::with(['user','company'])->whereRaw("BINARY `token`= ?",[$request->header('X-API-TOKEN')])->first() ) )
{
$user = $company_token->user;
$error = [
'message' => 'User inactive',
'errors' => []
];
//user who once existed, but has been soft deleted
if(!$user)
return response()->json(json_encode($error, JSON_PRETTY_PRINT) ,403);
/*
|
| Necessary evil here: As we are authenticating on CompanyToken,
| we need to link the company to the user manually. This allows
| us to decouple a $user and their attached companies completely.
|
*/
$user->setCompany($company_token->company);
//user who once existed, but has been soft deleted
if($user->company_user->is_locked){
$error = [
'message' => 'User access locked',
'errors' => []
];
return response()->json(json_encode($error, JSON_PRETTY_PRINT) ,403);
}
//stateless, don't remember the user.
auth()->login($user, false);
event(new UserLoggedIn($user));
}
else {
$error = [
'message' => 'Invalid token',
'errors' => []
];
return response()->json(json_encode($error, JSON_PRETTY_PRINT) ,403);
}
return $next($request);
}
}