Skip to content

Commit

Permalink
improved company identification for web
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Sep 30, 2021
1 parent c96b801 commit 5ade223
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions app/Http/Middleware/IdentifyCompany.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class IdentifyCompany
{
use Users;

public $request;

/**
* Handle an incoming request.
*
Expand All @@ -22,16 +24,18 @@ class IdentifyCompany
*/
public function handle($request, Closure $next, ...$guards)
{
$company_id = $request->isApi()
? $this->getCompanyIdFromApi($request)
: $this->getCompanyIdFromWeb($request);
$this->request = $request;

$company_id = $this->request->isApi()
? $this->getCompanyIdFromApi()
: $this->getCompanyIdFromWeb();

if (empty($company_id)) {
abort(500, 'Missing company');
}

// Check if user can access company
if ($request->isNotSigned($company_id) && $this->isNotUserCompany($company_id)) {
if ($this->request->isNotSigned($company_id) && $this->isNotUserCompany($company_id)) {
throw new AuthenticationException('Unauthenticated.', $guards);
}

Expand All @@ -42,21 +46,38 @@ public function handle($request, Closure $next, ...$guards)
config(['filesystems.disks.' . config('filesystems.default') . '.url' => url('/' . $company_id) . '/uploads']);

// Fix routes
app('url')->defaults(['company_id' => $company_id]);
$request->route()->forgetParameter('company_id');
if ($this->request->isNotApi()) {
app('url')->defaults(['company_id' => $company_id]);
$this->request->route()->forgetParameter('company_id');
}

return $next($request);
return $next($this->request);
}

protected function getCompanyIdFromWeb($request)
protected function getCompanyIdFromWeb()
{
return (int) $request->route('company_id');
return $this->getCompanyIdFromRoute() ?: ($this->getCompanyIdFromQuery() ?: $this->getCompanyIdFromHeader());
}

protected function getCompanyIdFromApi($request)
protected function getCompanyIdFromApi()
{
$company_id = $request->get('company_id', $request->header('X-Company'));
$company_id = $this->getCompanyIdFromQuery() ?: $this->getCompanyIdFromHeader();

return $company_id ?: optional($this->getFirstCompanyOfUser())->id;
}

protected function getCompanyIdFromRoute()
{
return (int) $this->request->route('company_id');
}

protected function getCompanyIdFromQuery()
{
return (int) $this->request->query('company_id');
}

protected function getCompanyIdFromHeader()
{
return (int) $this->request->header('X-Company');
}
}

0 comments on commit 5ade223

Please sign in to comment.