1 | <?php |
||
14 | trait WithPermissions |
||
15 | { |
||
16 | /** |
||
17 | * Get all roles the user is attached to. |
||
18 | * |
||
19 | * @return EloquentCollection |
||
20 | */ |
||
21 | public function roles() |
||
25 | |||
26 | /** |
||
27 | * Get all permissions the user has access to through roles. |
||
28 | * |
||
29 | * @return Collection |
||
30 | */ |
||
31 | public function permissions() |
||
40 | |||
41 | /** |
||
42 | * Check whether the user has access to a specific permission. |
||
43 | * If ID is provided a check for local access and inherit access |
||
44 | * will be performed automatically too. |
||
45 | * |
||
46 | * @param $permission |
||
47 | * @param null $id |
||
48 | * @return bool |
||
49 | */ |
||
50 | public function hasAccess($permission, $id = null) |
||
63 | |||
64 | /** |
||
65 | * Check whether the user has access to a permission at any |
||
66 | * given point. If user does not have global access but |
||
67 | * local access in 1 or more occurrences the hasAccessAny |
||
68 | * will return true. |
||
69 | * |
||
70 | * @param $permission |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function hasAccessAny($permission) |
||
88 | |||
89 | /** |
||
90 | * Determines whether the user has global access to the |
||
91 | * given permission. |
||
92 | * |
||
93 | * @param $permission |
||
94 | * @return bool |
||
95 | */ |
||
96 | private function hasGlobalAccess($permission) |
||
100 | |||
101 | /** |
||
102 | * Determines whether the user has local access to the |
||
103 | * permission with the given ID. |
||
104 | * |
||
105 | * @param $permission |
||
106 | * @param $id |
||
107 | * @return bool |
||
108 | */ |
||
109 | private function hasLocalAccess($permission, $id) |
||
128 | |||
129 | /** |
||
130 | * Determining whether the user is granted access to a |
||
131 | * permission indirectly through inheritance where a |
||
132 | * superior permission grants access down a tree |
||
133 | * structure. |
||
134 | * |
||
135 | * @param $permission |
||
136 | * @param $id |
||
137 | * @return bool |
||
138 | */ |
||
139 | private function hasInheritAccess($permission, $id) |
||
149 | } |
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
Idable
provides a methodequalsId
that 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.