Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
athphane committed Mar 17, 2024
2 parents 475f1a6 + 2493eae commit 4e02b45
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
1 change: 0 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ jobs:

- name: Install dependencies
run: |
composer config repositories.laravel-settings vcs https://github.com/Javaabu/laravel-settings.git
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.carbon }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
Expand Down
2 changes: 2 additions & 0 deletions src/HelpersServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Javaabu\Helpers\Http\Middleware\AllowOnlySpecificUserType;
use Javaabu\Helpers\Http\Middleware\JsonOnly;

class HelpersServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -217,5 +218,6 @@ public function register()
public function registerMiddlewareAliases(): void
{
app('router')->aliasMiddleware('json', JsonOnly::class);
app('router')->aliasMiddleware('only', AllowOnlySpecificUserType::class);
}
}
36 changes: 36 additions & 0 deletions src/Http/Middleware/AllowOnlySpecificUserType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Javaabu\Helpers\Http\Middleware;

use Closure;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Model;

class AllowOnlySpecificUserType
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $authenticable
* @return mixed
* @throws AuthorizationException
*/
public function handle($request, Closure $next, $authenticable = 'user')
{
$authenticable_class = Model::getActualClassNameForMorph($authenticable);

if (auth()->user() instanceof $authenticable_class) {
return $next($request);
}

if (expects_json($request)) {
throw new AuthorizationException('Disallowed user type');
} else {
$url = with(new $authenticable_class)->loginUrl();
return redirect()->to($url);
}
}

}
8 changes: 6 additions & 2 deletions src/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,18 @@ public function jsonApi($method, $uri, array $data = [], string $access_cookie =
* @param mixed $email
* @param array $scopes
*/
protected function actingAsApiUser($email, $scopes = ['read', 'write'])
protected function actingAsApiUser($email, $scopes = ['read', 'write'], $guard = null)
{
$this->seedDatabase();

//find the user
$user = is_object($email) ? $email : $this->getActiveAdminUser($email);

Passport::actingAs($user, $scopes);
if (! $guard) {
$guard = $user instanceof User ? 'api_admin' : 'api_' . ($user->getMorphClass());
}

Passport::actingAs($user, $scopes, $guard);
}

/**
Expand Down

0 comments on commit 4e02b45

Please sign in to comment.