2019-03-26 05:46:08 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
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;
|
2022-03-13 10:18:15 +01:00
|
|
|
use App\Utils\TruthSource;
|
2019-03-26 05:46:08 +01:00
|
|
|
use Closure;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use stdClass;
|
2019-03-26 05:46:08 +01:00
|
|
|
|
|
|
|
class TokenAuth
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
2019-03-26 05:46:08 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2022-03-22 05:13:57 +01:00
|
|
|
if ($request->header('X-API-TOKEN') && ($company_token = CompanyToken::with(['user', 'company'])->where('token', $request->header('X-API-TOKEN'))->first())) {
|
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-10-28 11:10:49 +01: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
|
2020-09-06 11:38:10 +02: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
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted() && $company_token->is_system == 0 && ! $user->account->isPaid()) {
|
2021-11-09 22:23:22 +01:00
|
|
|
$error = [
|
|
|
|
'message' => 'Feature not available with free / unpaid account.',
|
|
|
|
'errors' => new stdClass,
|
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-11-09 22:23:22 +01:00
|
|
|
return response()->json($error, 403);
|
|
|
|
}
|
|
|
|
|
2022-03-13 10:18:15 +01:00
|
|
|
$truth = app()->make(TruthSource::class);
|
|
|
|
|
|
|
|
$truth->setCompanyUser($company_token->cu);
|
|
|
|
$truth->setUser($company_token->user);
|
|
|
|
$truth->setCompany($company_token->company);
|
2022-03-13 10:24:58 +01:00
|
|
|
$truth->setCompanyToken($company_token);
|
2022-06-21 11:57:17 +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.
|
|
|
|
|
|
|
|
|
*/
|
2020-09-06 11:38:10 +02:00
|
|
|
|
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
|
2022-03-13 09:48:57 +01:00
|
|
|
if ($company_token->cu->is_locked) {
|
2019-09-24 00:37:38 +02:00
|
|
|
$error = [
|
|
|
|
'message' => 'User access locked',
|
2020-10-28 11:10:49 +01: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
|
|
|
}
|
2020-09-06 11:38:10 +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);
|
2021-05-24 02:41:23 +02:00
|
|
|
auth()->user()->setCompany($company_token->company);
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-09-24 00:37:38 +02:00
|
|
|
$error = [
|
|
|
|
'message' => 'Invalid token',
|
2020-10-28 11:10:49 +01: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);
|
|
|
|
}
|
|
|
|
}
|