-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab5e304
commit 151094d
Showing
4 changed files
with
273 additions
and
250 deletions.
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
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,156 @@ | ||
<?php | ||
|
||
namespace Silber\Bouncer\Database; | ||
|
||
use App\User; | ||
use Silber\Bouncer\Database\Constraints\AbilitiesForModel; | ||
|
||
trait IsAbility | ||
{ | ||
/** | ||
* Constructor. | ||
* | ||
* @param array $attributes | ||
*/ | ||
public function __construct(array $attributes = []) | ||
{ | ||
$this->table = Models::table('abilities'); | ||
|
||
parent::__construct($attributes); | ||
} | ||
|
||
/** | ||
* Create a new ability for a specific model. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model|string $model | ||
* @param string $name | ||
* @return static | ||
*/ | ||
public static function createForModel($model, $name) | ||
{ | ||
if ($model == '*') { | ||
return static::createForModelWildcard($name); | ||
} | ||
|
||
return static::forceCreate([ | ||
'name' => $name, | ||
'entity_type' => $model->getMorphClass(), | ||
'entity_id' => $model->exists ? $model->getKey() : null, | ||
]); | ||
} | ||
|
||
/** | ||
* Create a new ability for all models. | ||
* | ||
* @param string $name | ||
* @return static | ||
*/ | ||
public static function createForModelWildcard($name) | ||
{ | ||
return static::forceCreate([ | ||
'name' => $name, | ||
'entity_type' => '*', | ||
]); | ||
} | ||
|
||
/** | ||
* The roles relationship. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany | ||
*/ | ||
public function roles() | ||
{ | ||
return $this->morphedByMany( | ||
Models::classname(Role::class), | ||
'entity', | ||
Models::table('permissions') | ||
); | ||
} | ||
|
||
/** | ||
* The users relationship. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany | ||
*/ | ||
public function users() | ||
{ | ||
return $this->morphedByMany( | ||
Models::classname(User::class), | ||
'entity', | ||
Models::table('permissions') | ||
); | ||
} | ||
|
||
/** | ||
* Get the identifier for this ability. | ||
* | ||
* @return string | ||
*/ | ||
final public function getIdentifierAttribute() | ||
{ | ||
$slug = $this->attributes['name']; | ||
|
||
if ($this->attributes['entity_type']) { | ||
$slug .= '-'.$this->attributes['entity_type']; | ||
} | ||
|
||
if ($this->attributes['entity_id']) { | ||
$slug .= '-'.$this->attributes['entity_id']; | ||
} | ||
|
||
return strtolower($slug); | ||
} | ||
|
||
/** | ||
* Get the ability's "slug" attribute. | ||
* | ||
* @return string | ||
*/ | ||
public function getSlugAttribute() | ||
{ | ||
return $this->getIdentifierAttribute(); | ||
} | ||
|
||
/** | ||
* Constrain a query to having the given name. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query | ||
* @return string|array $name | ||
* @return bool $strict | ||
* @return void | ||
*/ | ||
public function scopeByName($query, $name, $strict = false) | ||
{ | ||
$names = (array) $name; | ||
|
||
if ( ! $strict) { | ||
$names[] = '*'; | ||
} | ||
|
||
$query->whereIn("{$this->table}.name", $names); | ||
} | ||
|
||
/** | ||
* Constrain a query to simple abilities. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query | ||
* @return void | ||
*/ | ||
public function scopeSimpleAbility($query) | ||
{ | ||
$query->whereNull("{$this->table}.entity_type"); | ||
} | ||
|
||
/** | ||
* Constrain a query to an ability for a specific model. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query | ||
* @param \Illuminate\Database\Eloquent\Model|string $model | ||
* @param bool $strict | ||
* @return void | ||
*/ | ||
public function scopeForModel($query, $model, $strict = false) | ||
{ | ||
(new AbilitiesForModel)->constrain($query, $model, $strict); | ||
} | ||
} |
Oops, something went wrong.