diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index 55a9354a4342..a7800517e27d 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -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')); diff --git a/app/Http/Middleware/TokenAuth.php b/app/Http/Middleware/TokenAuth.php index b46a8485ade8..743cb9da5e14 100644 --- a/app/Http/Middleware/TokenAuth.php +++ b/app/Http/Middleware/TokenAuth.php @@ -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, diff --git a/app/Models/User.php b/app/Models/User.php index 72ea19940ae4..c8bbb0899677 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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') diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 495ed33f2242..2206abe686fb 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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()); + } /** diff --git a/app/Utils/TruthSource.php b/app/Utils/TruthSource.php new file mode 100644 index 000000000000..ed4f3b3c734d --- /dev/null +++ b/app/Utils/TruthSource.php @@ -0,0 +1,56 @@ +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; + } +}