some progress
This commit is contained in:
@@ -71,9 +71,9 @@ class Movie extends Model
|
||||
|
||||
public function director()
|
||||
{
|
||||
return $this->belongsToMany(Person::class, 'movie_cast', 'movie_id', 'person_id')
|
||||
->where('movie_cast.job', 'Director')
|
||||
->orWhere('movie_cast.known_for_department', 'Directing')
|
||||
return $this->belongsToMany(Person::class, 'movie_crew', 'movie_id', 'person_id')
|
||||
->where('movie_crew.job', 'Director')
|
||||
->orWhere('movie_crew.known_for_department', 'Directing')
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Permission extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'description',
|
||||
];
|
||||
|
||||
public function roles()
|
||||
{
|
||||
return $this->belongsToMany(Role::class, 'permission_role');
|
||||
}
|
||||
|
||||
public function users()
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'permission_user');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Role extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'description',
|
||||
];
|
||||
|
||||
public function users()
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'role_user');
|
||||
}
|
||||
|
||||
public function permissions()
|
||||
{
|
||||
return $this->belongsToMany(Permission::class, 'permission_role');
|
||||
}
|
||||
|
||||
public function hasPermission($permission)
|
||||
{
|
||||
if (is_string($permission)) {
|
||||
return $this->permissions->contains('slug', $permission);
|
||||
}
|
||||
|
||||
return $this->permissions->contains('id', $permission->id);
|
||||
}
|
||||
|
||||
public function givePermissionTo($permission)
|
||||
{
|
||||
return $this->permissions()->save($permission);
|
||||
}
|
||||
}
|
||||
@@ -52,4 +52,69 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->belongsToMany(MediaList::class, 'user_media_lists', 'user_id', 'media_list_id');
|
||||
}
|
||||
|
||||
public function roles()
|
||||
{
|
||||
return $this->belongsToMany(Role::class, 'role_user');
|
||||
}
|
||||
|
||||
public function permissions()
|
||||
{
|
||||
return $this->belongsToMany(Permission::class, 'permission_user');
|
||||
}
|
||||
|
||||
public function hasRole($role)
|
||||
{
|
||||
if (is_string($role)) {
|
||||
return $this->roles->contains('slug', $role);
|
||||
}
|
||||
|
||||
return $this->roles->contains('id', $role->id);
|
||||
}
|
||||
|
||||
public function hasPermission($permission)
|
||||
{
|
||||
if (is_string($permission)) {
|
||||
// Check direct permissions
|
||||
if ($this->permissions->contains('slug', $permission)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check role permissions
|
||||
foreach ($this->roles as $role) {
|
||||
if ($role->hasPermission($permission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->permissions->contains('id', $permission->id);
|
||||
}
|
||||
|
||||
public function hasAnyRole($roles)
|
||||
{
|
||||
if (is_string($roles)) {
|
||||
return $this->roles->contains('slug', $roles);
|
||||
}
|
||||
|
||||
foreach ($roles as $role) {
|
||||
if ($this->hasRole($role)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function assignRole($role)
|
||||
{
|
||||
return $this->roles()->save($role);
|
||||
}
|
||||
|
||||
public function givePermissionTo($permission)
|
||||
{
|
||||
return $this->permissions()->save($permission);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user