Skip to content

Commit

Permalink
Merge branch 'master' into 6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
sgarcia-leantech committed Mar 3, 2021
2 parents 15ffe62 + ef896b7 commit 9ab3bfd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
10 changes: 10 additions & 0 deletions config/laratrust.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@
*/
'magic_is_able_to_method_case' => 'kebab_case',

/*
|--------------------------------------------------------------------------
| Laratrust Permissions as Gates
|--------------------------------------------------------------------------
|
| Determines if you can check if a user has a permission using the "can" method.
|
*/
'permissions_as_gates' => false,

/*
|--------------------------------------------------------------------------
| Laratrust Panel
Expand Down
4 changes: 4 additions & 0 deletions src/LaratrustServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ protected function registerResources()
*/
protected function registerPermissionsToGate()
{
if (!$this->app['config']->get('laratrust.permissions_as_gates')) {
return;
}

app(Gate::class)->before(function (Authorizable $user, string $ability) {
if (method_exists($user, 'hasPermission')) {
return $user->hasPermission($ability) ?: null;
Expand Down
8 changes: 7 additions & 1 deletion tests/Checkers/User/LaratrustUserCanDefaultCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ class LaratrustCanAbilityDefaultCheckerTest extends LaratrustUserCanCheckerTestC
protected function setUp(): void
{
parent::setUp();
}

protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$this->app['config']->set('laratrust.checker', 'default');
$app['config']->set('laratrust.checker', 'default');
$app['config']->set('laratrust.permissions_as_gates', true);
}

public function testCanShouldReturnBoolean()
Expand Down
79 changes: 79 additions & 0 deletions tests/LaratrustPermissionsAsGatesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Laratrust\Tests;

use Illuminate\Contracts\Auth\Access\Gate;
use Laratrust\LaratrustServiceProvider;
use Mockery as m;
use Mockery;

class LaratrustPermissionsAsGatesTest extends LaratrustTestCase
{
public function testRegistersPermissionsAsGates()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
config()->set('laratrust.permissions_as_gates', true);

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$this->mock(Gate::class)->shouldReceive('before')->once();

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$provider = new LaratrustServiceProvider($this->app);
$provider->boot();
}

public function testDoesNotRegisterPermissionsAsGatesIfDisabledInConfig()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
config()->set('laratrust.permissions_as_gates', false);

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$this->mock(Gate::class)->shouldNotReceive('before');

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$provider = new LaratrustServiceProvider($this->app);
$provider->boot();
}

public function testDoesNotRegisterPermissionsAsGatesByDefault()
{
/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$this->mock(Gate::class)->shouldNotReceive('before');

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$provider = new LaratrustServiceProvider($this->app);
$provider->boot();
}
}

0 comments on commit 9ab3bfd

Please sign in to comment.