tethys/app/Permission.php
Arno Kaimbacher 8dc1f1b048 my changes
2020-06-10 21:09:10 +02:00

113 lines
3.6 KiB
PHP

<?php
namespace App;
use Spatie\Permission\Guard;
use Illuminate\Database\Eloquent\Model;
use Spatie\Permission\Traits\HasRoles;
use Spatie\Permission\Traits\RefreshesPermissionCache;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Spatie\Permission\PermissionRegistrar;
use Spatie\Permission\Exceptions\PermissionAlreadyExists;
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
use Spatie\Permission\Contracts\Permission as PermissionContract;
class Permission extends Model implements PermissionContract
{
use HasRoles;
use RefreshesPermissionCache;
public $guarded = ['id'];
public function __construct(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
parent::__construct($attributes);
$this->setTable(config('permission.table_names.permissions'));
}
public static function create(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
$permission = static::getPermissions()->filter(function ($permission) use ($attributes) {
return $permission->name === $attributes['name'] && $permission->guard_name === $attributes['guard_name'];
})->first();
if ($permission) {
throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);
}
if (isNotLumen() && app()::VERSION < '5.4') {
return parent::create($attributes);
}
return static::query()->create($attributes);
}
/**
* A permission can be applied to roles.
*/
public function roles() : BelongsToMany
{
return $this->belongsToMany(
\App\Role::class,
config('permission.table_names.role_has_permissions'),
'permission_id',
'role_id'
);
}
public static function findByName(string $name, $guardName = null): PermissionContract
{
$guardName = $guardName ?? Guard::getDefaultName(static::class);
$permission = static::getPermissions()
->filter(function ($permission) use ($name, $guardName) {
return $permission->name === $name && $permission->guard_name === $guardName;
})
->first();
if (! $permission) {
throw PermissionDoesNotExist::create($name, $guardName);
}
return $permission;
}
public static function findById(int $id, $guardName = null): PermissionContract
{
$guardName = $guardName ?? Guard::getDefaultName(static::class);
$permission = static::getPermissions()
->filter(function ($permission) use ($id, $guardName) {
return $permission->id === $id && $permission->guard_name === $guardName;
})
->first();
if (! $permission) {
throw PermissionDoesNotExist::withId($id, $guardName);
}
return $permission;
}
public static function findOrCreate(string $name, $guardName = null): PermissionContract
{
$guardName = $guardName ?? Guard::getDefaultName(static::class);
$permission = static::getPermissions()
->filter(function ($permission) use ($name, $guardName) {
return $permission->name === $name && $permission->guard_name === $guardName;
})
->first();
if (! $permission) {
return static::create(['name' => $name, 'guard_name' => $guardName]);
}
return $permission;
}
protected static function getPermissions(): Collection
{
return app(PermissionRegistrar::class)->getPermissions();
}
}