my changes
This commit is contained in:
parent
28301e4312
commit
8dc1f1b048
263 changed files with 36882 additions and 4453 deletions
122
app/Role.php
122
app/Role.php
|
@ -1,14 +1,122 @@
|
|||
<?php namespace App;
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Spatie\Permission\Contracts\Role as RoleContract;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Spatie\Permission\Exceptions\RoleDoesNotExist;
|
||||
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
|
||||
use Spatie\Permission\Exceptions\RoleAlreadyExists;
|
||||
use Spatie\Permission\Guard;
|
||||
|
||||
class Role extends Model {
|
||||
use Spatie\Permission\Traits\HasPermissions;
|
||||
use Spatie\Permission\Traits\RefreshesPermissionCache;
|
||||
|
||||
public function users()
|
||||
{
|
||||
|
||||
return $this->belongsToMany('App\User');
|
||||
class Role extends Model implements RoleContract
|
||||
{
|
||||
use HasPermissions;
|
||||
use RefreshesPermissionCache;
|
||||
|
||||
}
|
||||
//protected $table = 'user_roles';
|
||||
public $timestamps = false;
|
||||
|
||||
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.roles'));
|
||||
}
|
||||
|
||||
public static function create(array $attributes = [])
|
||||
{
|
||||
$attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
|
||||
|
||||
if (static::where('name', $attributes['name'])
|
||||
->where('guard_name', $attributes['guard_name'])->first()) {
|
||||
throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
|
||||
}
|
||||
|
||||
if (isNotLumen() && app()::VERSION < '5.4') {
|
||||
return parent::create($attributes);
|
||||
}
|
||||
|
||||
return static::query()->create($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* A role may be given various permissions.
|
||||
*/
|
||||
public function permissions() : BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
\App\Permission::class,
|
||||
config('permission.table_names.role_has_permissions'),
|
||||
'role_id',
|
||||
'permission_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A role belongs to some users of the model associated with its guard.
|
||||
*/
|
||||
public function users() : BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
\App\User::class,
|
||||
config('permission.table_names.model_has_roles'),
|
||||
'role_id',
|
||||
'account_id'
|
||||
);
|
||||
}
|
||||
|
||||
public static function findByName(string $name, $guardName = null): RoleContract
|
||||
{
|
||||
$guardName = $guardName ?? Guard::getDefaultName(static::class);
|
||||
|
||||
$role = static::where('name', $name)->where('guard_name', $guardName)->first();
|
||||
if (! $role) {
|
||||
throw RoleDoesNotExist::named($name);
|
||||
}
|
||||
return $role;
|
||||
}
|
||||
|
||||
public static function findById(int $id, $guardName = null): RoleContract
|
||||
{
|
||||
$guardName = $guardName ?? Guard::getDefaultName(static::class);
|
||||
$role = static::where('id', $id)->where('guard_name', $guardName)->first();
|
||||
if (! $role) {
|
||||
throw RoleDoesNotExist::withId($id);
|
||||
}
|
||||
return $role;
|
||||
}
|
||||
|
||||
public static function findOrCreate(string $name, $guardName = null): RoleContract
|
||||
{
|
||||
$guardName = $guardName ?? Guard::getDefaultName(static::class);
|
||||
$role = static::where('name', $name)->where('guard_name', $guardName)->first();
|
||||
if (! $role) {
|
||||
return static::create(['name' => $name, 'guard_name' => $guardName]);
|
||||
}
|
||||
return $role;
|
||||
}
|
||||
|
||||
public function hasPermissionTo($permission): bool
|
||||
{
|
||||
if (is_string($permission)) {
|
||||
$permission = app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
|
||||
}
|
||||
|
||||
if (is_int($permission)) {
|
||||
$permission = app(Permission::class)->findById($permission, $this->getDefaultGuardName());
|
||||
}
|
||||
|
||||
if (! $this->getGuardNames()->contains($permission->guard_name)) {
|
||||
throw GuardDoesNotMatch::create($permission->guard_name, $this->getGuardNames());
|
||||
}
|
||||
|
||||
return $this->permissions->contains('id', $permission->id);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue