| @@ 100-126 (lines=27) @@ | ||
| 97 | * |
|
| 98 | * @return bool |
|
| 99 | */ |
|
| 100 | public function hasRole($name, $requireAll = false) |
|
| 101 | { |
|
| 102 | if (is_array($name)) { |
|
| 103 | foreach ($name as $roleName) { |
|
| 104 | $hasRole = $this->hasRole($roleName); |
|
| 105 | ||
| 106 | if ($hasRole && !$requireAll) { |
|
| 107 | return true; |
|
| 108 | } elseif (!$hasRole && $requireAll) { |
|
| 109 | return false; |
|
| 110 | } |
|
| 111 | } |
|
| 112 | ||
| 113 | // If we've made it this far and $requireAll is FALSE, then NONE of the roles were found |
|
| 114 | // If we've made it this far and $requireAll is TRUE, then ALL of the roles were found. |
|
| 115 | // Return the value of $requireAll; |
|
| 116 | return $requireAll; |
|
| 117 | } else { |
|
| 118 | foreach ($this->cachedRoles() as $role) { |
|
| 119 | if ($role->name == $name) { |
|
| 120 | return true; |
|
| 121 | } |
|
| 122 | } |
|
| 123 | } |
|
| 124 | ||
| 125 | return false; |
|
| 126 | } |
|
| 127 | ||
| 128 | /** |
|
| 129 | * Check if user has a permission by its name. |
|
| @@ 138-167 (lines=30) @@ | ||
| 135 | * |
|
| 136 | * @return bool |
|
| 137 | */ |
|
| 138 | public function can($permission, $requireAll = false) |
|
| 139 | { |
|
| 140 | if (is_array($permission)) { |
|
| 141 | foreach ($permission as $permName) { |
|
| 142 | $hasPerm = $this->can($permName); |
|
| 143 | ||
| 144 | if ($hasPerm && !$requireAll) { |
|
| 145 | return true; |
|
| 146 | } elseif (!$hasPerm && $requireAll) { |
|
| 147 | return false; |
|
| 148 | } |
|
| 149 | } |
|
| 150 | ||
| 151 | // If we've made it this far and $requireAll is FALSE, then NONE of the perms were found |
|
| 152 | // If we've made it this far and $requireAll is TRUE, then ALL of the perms were found. |
|
| 153 | // Return the value of $requireAll; |
|
| 154 | return $requireAll; |
|
| 155 | } else { |
|
| 156 | foreach ($this->cachedRoles() as $role) { |
|
| 157 | // Validate against the Permission table |
|
| 158 | foreach ($role->cachedPermissions() as $perm) { |
|
| 159 | if (str_is($permission, $perm->name)) { |
|
| 160 | return true; |
|
| 161 | } |
|
| 162 | } |
|
| 163 | } |
|
| 164 | } |
|
| 165 | ||
| 166 | return false; |
|
| 167 | } |
|
| 168 | ||
| 169 | /** |
|
| 170 | * Checks role(s) and permission(s). |
|