Working on determining the current company the best way using the API

This commit is contained in:
David Bomba 2019-03-28 16:03:18 +11:00
parent 88f96be135
commit 25b26a2e5d
7 changed files with 40 additions and 34 deletions

View File

@ -3,7 +3,9 @@
namespace App\Filters; namespace App\Filters;
use App\Models\User; use App\Models\User;
use Illuminate\Database\Query\Builder; //use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request; use Illuminate\Http\Request;
/** /**
@ -47,6 +49,7 @@ abstract class QueryFilters
public function __construct(Request $request, Builder $builder) public function __construct(Request $request, Builder $builder)
{ {
$this->request = $request; $this->request = $request;
$this->builder = $builder; $this->builder = $builder;
} }

View File

@ -52,7 +52,7 @@ class ClientController extends Controller
*/ */
public function index() 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) 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 = [ $data = [
'client' => $client, 'client' => $client,
@ -138,7 +138,7 @@ class ClientController extends Controller
public function store(StoreClientRequest $request) 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'); $client->load('contacts', 'primary_contact');

View File

@ -34,11 +34,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 == auth()->user()->company()->id; return $company->id == auth()->user()->getCompany()->id;
}); });
$data['companies'] = $companies->reject(function ($company){ $data['companies'] = $companies->reject(function ($company){
return $company->id == auth()->user()->company()->id; return $company->id == auth()->user()->getCompany()->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', '=', auth()->user()->company()->id); $query->where($this->getTable() .'.company_id', '=', auth()->user()->getCompany()->id);
return $query; return $query;
} }

View File

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Models\Company;
use App\Models\CompanyToken; use App\Models\CompanyToken;
use App\Models\CompanyUser; use App\Models\CompanyUser;
use App\Models\Traits\UserTrait; use App\Models\Traits\UserTrait;
@ -13,6 +14,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Laracasts\Presenter\PresentableTrait; use Laracasts\Presenter\PresentableTrait;
class User extends Authenticatable implements MustVerifyEmail class User extends Authenticatable implements MustVerifyEmail
@ -66,11 +68,6 @@ class User extends Authenticatable implements MustVerifyEmail
return $this->belongsTo(Account::class); return $this->belongsTo(Account::class);
} }
public function token()
{
return $this->tokens()->first();
}
public function tokens() public function tokens()
{ {
return $this->hasMany(CompanyToken::class)->orderBy('id'); 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'); return $this->belongsToMany(Company::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked');
} }
/**
* Returns the current Company public function getCompany()
*
* @return Collection
*/
public function company()
{ {
$ct = CompanyToken::whereToken(request()->header('X-API-TOKEN'))->first(); //$ct = CompanyToken::whereToken(request()->header('X-API-TOKEN'))->first();
//Log::error($this->tokens()->whereRaw("BINARY `token`= ?", [request()->header('X-API-TOKEN')])->first()->company);
return $ct->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() public function user_company()
{ {
// Log::error('user_co 1'.$this->company()->id);
return $this->user_companies->where('company_id', $this->company()->id)->first(); // 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 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 public function isAdmin() : bool
{ {
return (bool) $this->user_company()->is_admin; return $this->user_company()->is_admin;
} }

View File

@ -4,14 +4,12 @@ namespace App\Transformers;
use App\Models\Account; use App\Models\Account;
use App\Models\Payment; use App\Models\Payment;
use App\Utils\Traits\MakesHash;
/** /**
* Class AccountTransformer. * Class AccountTransformer.
*/ */
class AccountTransformer extends EntityTransformer class AccountTransformer extends EntityTransformer
{ {
trait MakesHash;
/** /**
* @SWG\Property(property="account_key", type="string", example="123456") * @SWG\Property(property="account_key", type="string", example="123456")
@ -41,7 +39,7 @@ class AccountTransformer extends EntityTransformer
public function transform(Account $account) public function transform(Account $account)
{ {
return [ return [
'id' => $this->encodePrimaryKey($account->id), 'id' => $account->id,
]; ];
} }

View File

@ -95,13 +95,20 @@ class ClientTest extends TestCase
$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()); $this->assertTrue($user->isAdmin());