mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Working on determining the current company the best way using the API
This commit is contained in:
parent
88f96be135
commit
25b26a2e5d
@ -3,7 +3,9 @@
|
||||
namespace App\Filters;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
//use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
@ -47,6 +49,7 @@ abstract class QueryFilters
|
||||
public function __construct(Request $request, Builder $builder)
|
||||
{
|
||||
$this->request = $request;
|
||||
|
||||
$this->builder = $builder;
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@ class ClientController extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return response()->json(Client::all());
|
||||
return response()->json(Client::scope());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -118,7 +118,7 @@ class ClientController extends Controller
|
||||
*/
|
||||
public function create(CreateClientRequest $request)
|
||||
{
|
||||
$client = ClientFactory::create(auth()->user()->company()->id, auth()->user()->id);
|
||||
$client = ClientFactory::create(auth()->user()->getCompany()->id, auth()->user()->id);
|
||||
|
||||
$data = [
|
||||
'client' => $client,
|
||||
@ -138,7 +138,7 @@ class ClientController extends Controller
|
||||
public function store(StoreClientRequest $request)
|
||||
{
|
||||
|
||||
$client = StoreClient::dispatchNow($request, ClientFactory::create(auth()->user()->company()->id, auth()->user()->id));
|
||||
$client = StoreClient::dispatchNow($request, ClientFactory::create(auth()->user()->getCompany()->id, auth()->user()->id));
|
||||
|
||||
$client->load('contacts', 'primary_contact');
|
||||
|
||||
|
@ -34,11 +34,11 @@ class HeaderComposer
|
||||
$companies = auth()->user()->companies;
|
||||
|
||||
$data['current_company'] = $companies->first(function ($company){
|
||||
return $company->id == auth()->user()->company()->id;
|
||||
return $company->id == auth()->user()->getCompany()->id;
|
||||
});
|
||||
|
||||
$data['companies'] = $companies->reject(function ($company){
|
||||
return $company->id == auth()->user()->company()->id;
|
||||
return $company->id == auth()->user()->getCompany()->id;
|
||||
});
|
||||
|
||||
return $data;
|
||||
|
@ -30,7 +30,7 @@ class BaseModel extends Model
|
||||
|
||||
public function scopeScope($query)
|
||||
{
|
||||
$query->where($this->getTable() .'.company_id', '=', auth()->user()->company()->id);
|
||||
$query->where($this->getTable() .'.company_id', '=', auth()->user()->getCompany()->id);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Models\CompanyToken;
|
||||
use App\Models\CompanyUser;
|
||||
use App\Models\Traits\UserTrait;
|
||||
@ -13,6 +14,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Laracasts\Presenter\PresentableTrait;
|
||||
|
||||
class User extends Authenticatable implements MustVerifyEmail
|
||||
@ -66,11 +68,6 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
return $this->belongsTo(Account::class);
|
||||
}
|
||||
|
||||
public function token()
|
||||
{
|
||||
return $this->tokens()->first();
|
||||
}
|
||||
|
||||
public function tokens()
|
||||
{
|
||||
return $this->hasMany(CompanyToken::class)->orderBy('id');
|
||||
@ -86,17 +83,14 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
return $this->belongsToMany(Company::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current Company
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function company()
|
||||
|
||||
public function getCompany()
|
||||
{
|
||||
$ct = CompanyToken::whereToken(request()->header('X-API-TOKEN'))->first();
|
||||
|
||||
return $ct->company;
|
||||
|
||||
//$ct = CompanyToken::whereToken(request()->header('X-API-TOKEN'))->first();
|
||||
//Log::error($this->tokens()->whereRaw("BINARY `token`= ?", [request()->header('X-API-TOKEN')])->first()->company);
|
||||
//return $this->tokens()->whereRaw("BINARY `token`= ?", [request()->header('X-API-TOKEN')])->first()->company;
|
||||
Log::error('the request header = '.request()->header('X-API-TOKEN'));
|
||||
return $this->tokens()->first()->company;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -118,8 +112,12 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
*/
|
||||
public function user_company()
|
||||
{
|
||||
|
||||
return $this->user_companies->where('company_id', $this->company()->id)->first();
|
||||
// Log::error('user_co 1'.$this->company()->id);
|
||||
// Log::error('coco');
|
||||
// Log::error('user_co '.$this->company());
|
||||
Log::error('the company id = '.$this->companyId());
|
||||
// return $this->user_companies->whereCompanyId($this->company()->id)->first();
|
||||
return $this->user_companies->where('company_id', $this->companyId())->first();
|
||||
|
||||
}
|
||||
|
||||
@ -131,7 +129,7 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
public function companyId() :int
|
||||
{
|
||||
|
||||
return $this->company()->id;
|
||||
return $this->getCompany()->id;
|
||||
|
||||
}
|
||||
|
||||
@ -171,7 +169,7 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
public function isAdmin() : bool
|
||||
{
|
||||
|
||||
return (bool) $this->user_company()->is_admin;
|
||||
return $this->user_company()->is_admin;
|
||||
|
||||
}
|
||||
|
||||
|
@ -4,14 +4,12 @@ namespace App\Transformers;
|
||||
|
||||
use App\Models\Account;
|
||||
use App\Models\Payment;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
|
||||
/**
|
||||
* Class AccountTransformer.
|
||||
*/
|
||||
class AccountTransformer extends EntityTransformer
|
||||
{
|
||||
trait MakesHash;
|
||||
|
||||
/**
|
||||
* @SWG\Property(property="account_key", type="string", example="123456")
|
||||
@ -41,7 +39,7 @@ class AccountTransformer extends EntityTransformer
|
||||
public function transform(Account $account)
|
||||
{
|
||||
return [
|
||||
'id' => $this->encodePrimaryKey($account->id),
|
||||
'id' => $account->id,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -93,15 +93,22 @@ class ClientTest extends TestCase
|
||||
|
||||
$acc = $response->json();
|
||||
|
||||
$account = Account::find($acc['id']);
|
||||
$account = Account::find($acc['id']);
|
||||
|
||||
$token = $account->default_company->tokens()->first()->token;
|
||||
$company_token = $account->default_company->tokens()->first();
|
||||
$token = $company_token->token;
|
||||
$company = $company_token->company;
|
||||
|
||||
$company = $account->default_company;
|
||||
$user = $company_token->user;
|
||||
|
||||
$company_user = $company->company_users()->first();
|
||||
//$company_user = $company->company_users()->first();
|
||||
|
||||
$user = User::find($company_user->user_id);
|
||||
//$user = User::find($company_user->user_id);
|
||||
$this->assertNotNull($company_token);
|
||||
$this->assertNotNull($token);
|
||||
$this->assertNotNull($user);
|
||||
$this->assertNotNull($company);
|
||||
$this->assertNotNull($user->tokens()->first()->company);
|
||||
|
||||
$this->assertTrue($user->isAdmin());
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user