-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add can, cant and cannot methods to the Role model
- Loading branch information
1 parent
5dfeb21
commit d1b1187
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Silber\Bouncer\Database; | ||
|
||
use Silber\Bouncer\Clipboard; | ||
use Illuminate\Container\Container; | ||
|
||
trait Authorizable | ||
{ | ||
/** | ||
* Determine if the authority has a given ability. | ||
* | ||
* @param string $ability | ||
* @param \Illuminate\Database\Eloquent\Model|null $model | ||
* @return bool | ||
*/ | ||
public function can($ability, $model = null) | ||
{ | ||
return $this->getClipboardInstance()->check($this, $ability, $model); | ||
} | ||
|
||
/** | ||
* Determine if the authority does not have a given ability. | ||
* | ||
* @param string $ability | ||
* @param \Illuminate\Database\Eloquent\Model|null $model | ||
* @return bool | ||
*/ | ||
public function cant($ability, $model = null) | ||
{ | ||
return ! $this->can($ability, $model); | ||
} | ||
|
||
/** | ||
* Determine if the authority does not have a given ability. | ||
* | ||
* @param string $ability | ||
* @param \Illuminate\Database\Eloquent\Model|null $model | ||
* @return bool | ||
*/ | ||
public function cannot($ability, $model = null) | ||
{ | ||
return $this->cant($ability, $model); | ||
} | ||
|
||
/** | ||
* Get an instance of the bouncer's clipboard. | ||
* | ||
* @return \Silber\Bouncer\Clipboard | ||
*/ | ||
protected function getClipboardInstance() | ||
{ | ||
$container = Container::getInstance() ?: new Container; | ||
|
||
return $container->make(Clipboard::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
use Silber\Bouncer\Database\Role; | ||
|
||
class AuthorizableTest extends BaseTestCase | ||
{ | ||
public function testCheckingAbilitiesOnRole() | ||
{ | ||
$role = Role::create(['name' => 'admin']); | ||
|
||
$role->allow('scream'); | ||
|
||
$this->assertTrue($role->can('scream')); | ||
$this->assertTrue($role->cant('shout')); | ||
$this->assertTrue($role->cannot('cry')); | ||
} | ||
} |