Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hasRole for comparison against multiple roles #1209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions wire/core/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,33 @@ public function __construct(Template $tpl = null) {
*
*/
public function hasRole($role) {

$roles = $this->get('roles');
$has = false;

if(empty($roles)) {
// do nothing

} else if(is_object($role) && $role instanceof Page) {
$has = $roles->has($role);


$roles = $this->get('roles');
$has = false;

if(empty($roles)) {
// do nothing

} else if(is_object($role) && $role instanceof Page) {
$has = $roles->has($role);

} else if(is_object($role) && $role instanceof PageArray) {
foreach ($role as $page) {
if ($this->hasRole($page)) {
$has = true;
break;
}
}

} else if(strpos($role, '|') !== false) {
$multiroles = explode('|', $role);
foreach ($multiroles as $singlerole) {
if ($this->hasRole($singlerole)) {
$has = true;
break;
}
}

} else if(ctype_digit("$role")) {
$role = (int) $role;
foreach($roles as $r) {
Expand All @@ -63,7 +80,7 @@ public function hasRole($role) {
}

} else if(is_string($role)) {
foreach($roles as $r) {
foreach($roles as $r) {
if($r->name === $role) {
$has = true;
break;
Expand Down