2019-03-26 05:46:08 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-03-26 05:46:08 +01:00
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2019-06-05 07:33:48 +02:00
|
|
|
use App\Events\User\UserLoggedIn;
|
2019-03-26 05:46:08 +01:00
|
|
|
use App\Models\CompanyToken;
|
|
|
|
use App\Models\User;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2019-03-26 05:46:08 +01:00
|
|
|
use Closure;
|
|
|
|
|
|
|
|
class TokenAuth
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($request->header('X-API-TOKEN') && ($company_token = CompanyToken::with(['user','company'])->whereRaw("BINARY `token`= ?", [$request->header('X-API-TOKEN')])->first())) {
|
2020-07-28 13:19:51 +02:00
|
|
|
|
2019-06-12 01:15:17 +02:00
|
|
|
$user = $company_token->user;
|
2019-09-24 00:37:38 +02:00
|
|
|
|
|
|
|
$error = [
|
|
|
|
'message' => 'User inactive',
|
2020-08-12 00:59:28 +02:00
|
|
|
'errors' => new \stdClass
|
2019-09-24 00:37:38 +02:00
|
|
|
];
|
2019-06-12 03:00:38 +02:00
|
|
|
//user who once existed, but has been soft deleted
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$user) {
|
2020-02-05 05:06:03 +01:00
|
|
|
return response()->json($error, 403);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-06-12 03:00:38 +02:00
|
|
|
|
2019-06-12 01:15:17 +02:00
|
|
|
/*
|
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
| Necessary evil here: As we are authenticating on CompanyToken,
|
2019-06-12 01:15:17 +02:00
|
|
|
| we need to link the company to the user manually. This allows
|
|
|
|
| us to decouple a $user and their attached companies completely.
|
|
|
|
|
|
|
|
|
*/
|
2019-12-30 22:59:12 +01:00
|
|
|
$user->setCompany($company_token->company);
|
2020-02-24 11:15:30 +01:00
|
|
|
|
|
|
|
config(['ninja.company_id' => $company_token->company->id]);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
app('queue')->createPayloadUsing(function () use ($company_token) {
|
|
|
|
return ['db' => $company_token->company->db];
|
2020-02-24 11:15:30 +01:00
|
|
|
});
|
2019-09-24 00:37:38 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
//user who once existed, but has been soft deleted
|
|
|
|
if ($user->company_user->is_locked) {
|
2019-09-24 00:37:38 +02:00
|
|
|
$error = [
|
|
|
|
'message' => 'User access locked',
|
2020-08-12 00:59:28 +02:00
|
|
|
'errors' => new \stdClass
|
2019-09-24 00:37:38 +02:00
|
|
|
];
|
|
|
|
|
2020-02-05 05:06:03 +01:00
|
|
|
return response()->json($error, 403);
|
2019-09-24 00:37:38 +02:00
|
|
|
}
|
2019-06-12 03:00:38 +02:00
|
|
|
|
2019-06-12 01:15:17 +02:00
|
|
|
//stateless, don't remember the user.
|
2019-12-30 22:59:12 +01:00
|
|
|
auth()->login($user, false);
|
2019-06-12 01:15:17 +02:00
|
|
|
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new UserLoggedIn($user, $company_token->company, Ninja::eventVars()));
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-09-24 00:37:38 +02:00
|
|
|
$error = [
|
|
|
|
'message' => 'Invalid token',
|
2020-08-12 00:59:28 +02:00
|
|
|
'errors' => new \stdClass
|
2019-09-24 00:37:38 +02:00
|
|
|
];
|
|
|
|
|
2020-02-05 05:06:03 +01:00
|
|
|
return response()->json($error, 403);
|
2019-03-26 05:46:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|