1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Acl\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
7
|
|
|
use Yajra\Acl\Models\Role; |
8
|
|
|
|
9
|
|
|
trait HasRole |
10
|
|
|
{ |
11
|
|
|
use InteractsWithRole; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Check if user have access using any of the acl (permissions or roles slug). |
15
|
|
|
* |
16
|
|
|
* @param string|array $acl |
17
|
|
|
* @return boolean |
18
|
|
|
*/ |
19
|
|
|
public function canAccess($acl): bool |
20
|
|
|
{ |
21
|
|
|
return $this->canAtLeast($acl) || $this->hasRole($acl); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Check if user has at least one of the given permissions |
26
|
|
|
* |
27
|
|
|
* @param string|array $permissions |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
|
public function canAtLeast($permissions): bool |
31
|
|
|
{ |
32
|
|
|
$can = false; |
33
|
|
|
|
34
|
|
|
if (auth()->check()) { |
|
|
|
|
35
|
|
|
/** @var Role $role */ |
36
|
|
|
foreach ($this->roles as $role) { |
37
|
|
|
if ($role->canAtLeast($permissions)) { |
38
|
|
|
$can = true; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} else { |
42
|
|
|
try { |
43
|
|
|
$guest = $this->findRoleBySlug('guest'); |
44
|
|
|
|
45
|
|
|
return $guest->canAtLeast($permissions); |
46
|
|
|
} catch (ModelNotFoundException $exception) { |
47
|
|
|
// |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $can; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get all user role permissions. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getPermissions(): array |
61
|
|
|
{ |
62
|
|
|
$permissions = [[], []]; |
63
|
|
|
|
64
|
|
|
foreach ($this->roles as $role) { |
65
|
|
|
$permissions[] = $role->getPermissions(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return call_user_func_array('array_merge', $permissions); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check if the given entity/model is owned by the user. |
73
|
|
|
* |
74
|
|
|
* @param \Illuminate\Database\Eloquent\Model $entity |
75
|
|
|
* @param string $relation |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function owns(Model $entity, $relation = 'user_id'): bool |
79
|
|
|
{ |
80
|
|
|
return $this->getKeyName() === $entity->{$relation}; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: