Skip to content

Commit

Permalink
check permission based on logged in user guard
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniyal-Javani committed Feb 23, 2020
1 parent 3d40b0c commit 54dde60
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/HasPermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,17 @@ public function it_can_check_if_there_is_any_of_the_direct_permissions_given()
$this->assertTrue($this->testUser->hasAnyDirectPermission('edit-news', 'edit-blog'));
$this->assertFalse($this->testUser->hasAnyDirectPermission('edit-blog', 'Edit News', ['Edit News']));
}

/** @test */
public function it_can_check_permission_based_on_logged_in_user_guard()
{
$this->testUser->givePermissionTo(\Spatie\Permission\Models\Permission::create([
'name' => 'do_that',
'guard_name' => 'api',
]));
$response = $this->actingAs($this->testUser, 'api')->json('GET', '/check-api-guard-permission');
$response->assertJson([
'status' => true
]);
}
}
18 changes: 18 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Spatie\Permission\Contracts\Permission;
use Orchestra\Testbench\TestCase as Orchestra;
use Spatie\Permission\PermissionServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

abstract class TestCase extends Orchestra
{
Expand Down Expand Up @@ -45,6 +47,8 @@ public function setUp(): void
$this->testAdmin = Admin::first();
$this->testAdminRole = app(Role::class)->find(3);
$this->testAdminPermission = app(Permission::class)->find(4);

$this->setUpRoutes();
}

/**
Expand Down Expand Up @@ -142,4 +146,18 @@ public function createCacheTable()
$table->integer('expiration');
});
}

/**
* Create routes to tests authentication with guards.
*
* @return void
*/
public function setUpRoutes()
{
Route::middleware('auth:api')->get('/check-api-guard-permission', function (Request $request) {
return [
'status' => $request->user()->hasPermissionTo('do_that')
];
});
}
}

0 comments on commit 54dde60

Please sign in to comment.