Cache company, company_user and user in container

This commit is contained in:
David Bomba 2022-03-13 20:18:15 +11:00
parent 2b95f2a0d4
commit e2cd1e5c71
5 changed files with 88 additions and 2 deletions

View File

@ -19,6 +19,7 @@ use App\Transformers\EntityTransformer;
use App\Utils\Ninja;
use App\Utils\Statics;
use App\Utils\Traits\AppSetup;
use App\Utils\TruthSource;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
@ -610,6 +611,10 @@ class BaseController extends Controller
protected function listResponse($query)
{
$truth = app()->make(TruthSource::class);
nlog($truth->getCompany());
$this->buildManager();
$transformer = new $this->entity_transformer(request()->input('serializer'));

View File

@ -15,6 +15,7 @@ use App\Events\User\UserLoggedIn;
use App\Models\CompanyToken;
use App\Models\User;
use App\Utils\Ninja;
use App\Utils\TruthSource;
use Closure;
use Illuminate\Http\Request;
use stdClass;
@ -52,6 +53,12 @@ class TokenAuth
return response()->json($error, 403);
}
$truth = app()->make(TruthSource::class);
$truth->setCompanyUser($company_token->cu);
$truth->setUser($company_token->user);
$truth->setCompany($company_token->company);
/*
|
| Necessary evil here: As we are authenticating on CompanyToken,

View File

@ -21,6 +21,7 @@ use App\Services\User\UserService;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\UserSessionAttributes;
use App\Utils\Traits\UserSettings;
use App\Utils\TruthSource;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -30,8 +31,8 @@ use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Laracasts\Presenter\PresentableTrait;
use Illuminate\Support\Facades\Cache;
use Laracasts\Presenter\PresentableTrait;
class User extends Authenticatable implements MustVerifyEmail
{
@ -145,7 +146,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function token()
{
if (request()->header('X-API-TOKEN')) {
return CompanyToken::with(['company','cu'])->where('token', request()->header('X-API-TOKEN'))->first();
return CompanyToken::with(['cu'])->where('token', request()->header('X-API-TOKEN'))->first();
}
@ -180,12 +181,16 @@ class User extends Authenticatable implements MustVerifyEmail
*/
public function getCompany()
{
$truth = app()->make(TruthSource::class);
if ($this->company){
return $this->company;
}
elseif($truth->getCompany()){
return $truth->getCompany();
}
elseif (request()->header('X-API-TOKEN')) {
$company_token = CompanyToken::with(['company'])->where('token', request()->header('X-API-TOKEN'))->first();
@ -229,6 +234,12 @@ class User extends Authenticatable implements MustVerifyEmail
public function co_user()
{
$truth = app()->make(TruthSource::class);
if($truth->getCompanyUser()){
return $truth->getCompany();
}
return $this->token()->cu;
// return $this->company_user();
}
@ -239,6 +250,9 @@ class User extends Authenticatable implements MustVerifyEmail
// $this->id = auth()->user()->id;
// }
// return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'user_id', 'id', 'user_id')
// ->withTrashed();
return $this->token()->cu;
// return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'user_id', 'id', 'user_id')

View File

@ -15,6 +15,7 @@ use App\Http\Middleware\SetDomainNameDb;
use App\Models\Invoice;
use App\Models\Proposal;
use App\Utils\Ninja;
use App\Utils\TruthSource;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Queue\Events\JobProcessing;
@ -71,6 +72,9 @@ class AppServiceProvider extends ServiceProvider
// \Log::error('Event Job '.$event->job->getJobId);
// // \Log::info('Event Job '.$event->job->payload());
// });
app()->instance(TruthSource::class, new TruthSource());
}
/**

56
app/Utils/TruthSource.php Normal file
View File

@ -0,0 +1,56 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Utils;
class TruthSource
{
public $company;
public $user;
public $company_user;
public function setCompanyUser($company_user)
{
$this->company_user = $company_user;
return $this;
}
public function setUser($user){
$this->user = $user;
return $this;
}
public function setCompany($company)
{
$this->company = $company;
return $this;
}
public function getCompany()
{
return $this->company;
}
public function getCompanyUser()
{
return $this->company_user;
}
public function getUser()
{
return $this->user;
}
}