Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Spatie Permission Model #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Permission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace Eminiarts\NovaPermissions;

use Spatie\Permission\Contracts\Permission as PermissionContract;
use Spatie\Permission\Guard;
use Spatie\Permission\Models\Permission as SpatiePermission;

class Permission extends SpatiePermission
{
/**
* Find or create permission by its name and group (and optionally guardName).
*
* @param string $name
* @param string $group
* @param string|null $guardName
*
* @return \Spatie\Permission\Contracts\Permission
*/
public static function findOrCreateWithGroup(string $name, string $group, $guardName = null): PermissionContract
{
$guardName = $guardName ?? Guard::getDefaultName(static::class);
$permission = static::getPermissions(['name' => $name, 'group' => $group, 'guard_name' => $guardName])->first();

if (! $permission) {
return static::query()->create(['name' => $name, 'group' => $group, 'guard_name' => $guardName]);
}

return $permission;
}
}