1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

move away from session variables

This commit is contained in:
David Bomba 2019-03-27 20:38:28 +11:00
parent 1986714927
commit 74a01f8731
8 changed files with 27 additions and 17 deletions

View File

@ -121,7 +121,7 @@ class ClientController extends Controller
*/ */
public function create(CreateClientRequest $request) public function create(CreateClientRequest $request)
{ {
$client = ClientFactory::create($this->getCurrentCompanyId(), auth()->user()->id); $client = ClientFactory::create(auth()->user()->company(), auth()->user()->id);
$data = [ $data = [
'client' => $client, 'client' => $client,

View File

@ -20,7 +20,8 @@ class TokenAuth
if( $request->header('X-API-TOKEN') && ($user = CompanyToken::whereRaw("BINARY `token`= ?",[$request->header('X-API-TOKEN')])->first()->user ) ) if( $request->header('X-API-TOKEN') && ($user = CompanyToken::whereRaw("BINARY `token`= ?",[$request->header('X-API-TOKEN')])->first()->user ) )
{ {
//$user->with('company');
auth()->login($user); auth()->login($user);
} }

View File

@ -36,11 +36,11 @@ class HeaderComposer
$companies = auth()->user()->companies; $companies = auth()->user()->companies;
$data['current_company'] = $companies->first(function ($company){ $data['current_company'] = $companies->first(function ($company){
return $company->id == $this->getCurrentCompanyId(); return $company->id == auth()->user()->company()->id;
}); });
$data['companies'] = $companies->reject(function ($company){ $data['companies'] = $companies->reject(function ($company){
return $company->id == $this->getCurrentCompanyId(); return $company->id == auth()->user()->company->id;
}); });
return $data; return $data;

View File

@ -30,7 +30,7 @@ class BaseModel extends Model
public function scopeScope($query) public function scopeScope($query)
{ {
$query->where($this->getTable() .'.company_id', '=', $this->getCurrentCompanyId()); $query->where($this->getTable() .'.company_id', '=', auth()->user()->company()->id);
return $query; return $query;
} }

View File

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Models\CompanyToken;
use App\Models\CompanyUser; use App\Models\CompanyUser;
use App\Models\Traits\UserTrait; use App\Models\Traits\UserTrait;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
@ -62,7 +63,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function token() public function token()
{ {
return $this->tokens->first(); return $this->tokens()->first();
} }
public function tokens() public function tokens()
@ -87,7 +88,11 @@ class User extends Authenticatable implements MustVerifyEmail
*/ */
public function company() public function company()
{ {
return $this->companies()->where('company_id', $this->getCurrentCompanyId())->first(); $ct = CompanyToken::whereToken(request()->header('X-API-TOKEN'))->first();
return $ct->company;
// return $this->companies()->where('company_id', $this->getCurrentCompanyId())->first();
} }
/** /**
@ -105,11 +110,14 @@ class User extends Authenticatable implements MustVerifyEmail
* querying directly on the pivot table relationship * querying directly on the pivot table relationship
* *
* @return Collection * @return Collection
* @deprecated
*/ */
public function user_company() public function user_company()
{ {
$ct = CompanyToken::whereToken(request()->header('X-API-TOKEN'))->first();
return $this->user_companies->where('company_id', $this->getCurrentCompanyId())->first(); return $ct->company;
//return $this->user_companies->where('company_id', $this->getCurrentCompanyId())->first();
} }
@ -121,7 +129,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function companyId() :int public function companyId() :int
{ {
return $this->getCurrentCompanyId(); return $this->company()->id;
} }
@ -133,7 +141,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function permissions() public function permissions()
{ {
$permissions = json_decode($this->user_company()->permissions); $permissions = json_decode($this->company()->permissions);
if (! $permissions) if (! $permissions)
return []; return [];
@ -149,7 +157,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function settings() public function settings()
{ {
return json_decode($this->user_company()->settings); return json_decode($this->company()->settings);
} }
@ -161,7 +169,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function isAdmin() : bool public function isAdmin() : bool
{ {
return (bool) $this->user_company()->is_admin; return (bool) $this->company()->is_admin;
} }

View File

@ -21,11 +21,11 @@ trait MakesHeaderData
$companies = auth()->user()->companies; $companies = auth()->user()->companies;
$data['current_company'] = $companies->first(function ($company){ $data['current_company'] = $companies->first(function ($company){
return $company->id == $this->getCurrentCompanyId(); return $company->id == auth()->user()->company()->id;
}); });
$data['companies'] = $companies->reject(function ($company){ $data['companies'] = $companies->reject(function ($company){
return $company->id == $this->getCurrentCompanyId(); return $company->id == auth()->user()->company()->id;
}); });
return $data; return $data;

View File

@ -29,6 +29,8 @@ Route::group(['middleware' => ['api_secret_check','token_auth']], function () {
Route::resource('clients', 'ClientController'); // name = (clients. index / create / show / update / destroy / edit Route::resource('clients', 'ClientController'); // name = (clients. index / create / show / update / destroy / edit
Route::post('clients/bulk', 'ClientController@bulk')->name('clients.bulk');
Route::resource('invoices', 'InvoiceController'); // name = (invoices. index / create / show / update / destroy / edit Route::resource('invoices', 'InvoiceController'); // name = (invoices. index / create / show / update / destroy / edit
Route::post('invoices/bulk', 'InvoiceController@bulk')->name('invoices.bulk'); Route::post('invoices/bulk', 'InvoiceController@bulk')->name('invoices.bulk');
@ -41,8 +43,6 @@ Route::group(['middleware' => ['api_secret_check','token_auth']], function () {
Route::post('recurring_invoices/bulk', 'RecurringInvoiceController@bulk')->name('recurring_invoices.bulk'); Route::post('recurring_invoices/bulk', 'RecurringInvoiceController@bulk')->name('recurring_invoices.bulk');
Route::post('clients/bulk', 'ClientController@bulk')->name('clients.bulk');
Route::resource('client_statement', 'ClientStatementController@statement'); // name = (client_statement. index / create / show / update / destroy / edit Route::resource('client_statement', 'ClientStatementController@statement'); // name = (client_statement. index / create / show / update / destroy / edit
Route::resource('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit Route::resource('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit

View File

@ -19,11 +19,12 @@ class CollectionMergingTest extends TestCase
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
Session::start(); Session::start();
$this->setCurrentCompanyId(1); $this->setCurrentCompanyId(1);
$this->terms = PaymentTerm::scope()->get(); $this->terms = PaymentTerm::all();
} }
public function testBlankCollectionReturned() public function testBlankCollectionReturned()