|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* NOTICE OF LICENSE |
|
5
|
|
|
* |
|
6
|
|
|
* Part of the Rinvex Fort Package. |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to The MIT License (MIT) |
|
9
|
|
|
* that is bundled with this package in the LICENSE file. |
|
10
|
|
|
* |
|
11
|
|
|
* Package: Rinvex Fort Package |
|
12
|
|
|
* License: The MIT License (MIT) |
|
13
|
|
|
* Link: https://rinvex.com |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Traits; |
|
17
|
|
|
|
|
18
|
|
|
use Rinvex\Fort\Models\Ability; |
|
19
|
|
|
|
|
20
|
|
|
trait HasAbilities |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Attach the given abilities to the model. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string|array $action |
|
26
|
|
|
* @param string|array $resource |
|
27
|
|
|
* |
|
28
|
|
|
* @return $this |
|
29
|
|
|
*/ |
|
30
|
|
|
public function grantAbilities($action, $resource) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setAbilities($action, $resource, 'syncWithoutDetaching'); |
|
33
|
|
|
|
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Sync the given abilities to the model. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string|array $action |
|
41
|
|
|
* @param string|array $resource |
|
42
|
|
|
* |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function syncAbilities($action, $resource) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->setAbilities($action, $resource, 'sync'); |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Detach the given abilities from the model. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string|array $action |
|
56
|
|
|
* @param string|array $resource |
|
57
|
|
|
* |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
|
|
public function revokeAbilities($action, $resource) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->setAbilities($action, $resource, 'detach'); |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set the given ability(s) to the model. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string|array $action |
|
71
|
|
|
* @param string|array $resource |
|
72
|
|
|
* @param string $process |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function setAbilities($action, $resource, string $process) |
|
77
|
|
|
{ |
|
78
|
|
|
// Guess event name |
|
79
|
|
|
$event = $process == 'syncWithoutDetaching' ? 'attach' : $process; |
|
80
|
|
|
|
|
81
|
|
|
// If the "attaching/syncing/detaching" event returns false we'll cancel this operation and |
|
82
|
|
|
// return false, indicating that the attaching/syncing/detaching failed. This provides a |
|
83
|
|
|
// chance for any listeners to cancel save operations if validations fail or whatever. |
|
84
|
|
|
if ($this->fireModelEvent($event.'ing') === false) { |
|
|
|
|
|
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// Ability model |
|
89
|
|
|
$model = Ability::query(); |
|
90
|
|
|
|
|
91
|
|
|
if (is_string($action) && $action !== '*') { |
|
92
|
|
|
$model->where('action', $action); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (is_array($action)) { |
|
96
|
|
|
$model->whereIn('action', $action); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (is_string($resource) && $resource !== '*') { |
|
100
|
|
|
$model->where('resource', $resource); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (is_array($resource)) { |
|
104
|
|
|
$model->whereIn('resource', $resource); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// Find the given abilities |
|
108
|
|
|
$abilities = $model->get(); |
|
109
|
|
|
|
|
110
|
|
|
// Sync abilities |
|
111
|
|
|
$this->abilities()->$process($abilities); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
// Fire the roles attached/synced/detached event |
|
114
|
|
|
$this->fireModelEvent($event.'ed', false); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.