Check if a user matches the role for a resource
@param $user The user instance.
@param $role The role name.
@param $resource The resource string to authorize.
@return Boolean
188107
protected function matchRole(User $user, $role, $resource) {
    $isDynamicRole = isset($this->dynamicRoles[$role]);
    return (($isDynamicRole && $this->dynamicRoles[$role]->match($user, $resource) === true)
            || (!$isDynamicRole && $user->hasRole($role)));
  }
1290240
public function hasRole($role)
    {
        $roles = $this->roles;
        foreach ($roles as $role_obj)
            if ($this->checkRole($role, $role_obj))
                return true;

        return false;
    }
469182
public function isAllowed($role, $resource, $privilege): bool
    {
        // collect current acl
        $this->listCurrentAcl[$role . $resource . $privilege] = [
            'role'      => $role,
            'resource'  => $resource,
            'privilege' => $privilege,
        ];

        // for policy none
        if ($this->policy == self::POLICY_NONE) {
            return true;
        }

        // auto-fix missing resource in acl
        if ($resource && !isset($this->resource[$resource])) {
            $this->saveResource(['id' => null, 'resource' => $resource]);
        }
        return $this->permission->isAllowed($role, $resource, $privilege);
    }
1231727
public static function get_user_role($user = null)
    {
        $wp_roles = \wp_roles();

        if ($user === null) {
            $user = \wp_get_current_user();
        }

        if (!($user instanceof WP_User)) {
            return false;
        }

        $roles = $user->roles;
        $role = \array_shift($roles);

        return isset($wp_roles->role_objects[ $role ]) ? $wp_roles->role_objects[ $role ] : false;
    }
229756
public function hasRole($role)
    {
        $role = strtoupper($role);

        if ($role == 'ROLE_USER') {
            return true;
        }

        if (in_array($role, $this->roles)) {
            return true;
        }

        return false;
    }
1193664
public function iAmLoggedInAsRole(string $role)
    {
        $found_user = null;
        $users      = $this->getWordpressParameter('users');

        foreach ($users as $user) {
            if (in_array($role, $user['roles'], true)) {
                $found_user = $user;
                break;
            }
        }

        if ($found_user === null) {
            throw new RuntimeException("[W801] User not found for role \"{$role}\"");
        }

        $this->logIn($found_user['username'], $found_user['password']);
    }
615219
public function checkAccess($user_id, $roles)
    {
        if ($this->isRoot($user_id)) {
            return true;
        }
        if (is_array($roles)) {
            return $this->hasRoles($user_id, $roles);
        } elseif (strpos($roles, '|') !== false) {
            return $this->hasAnyRole($user_id, explode('|', $roles));
        } else {
            return $this->hasRoles($user_id, explode('&', $roles));
        }
    }
825337
@Override
	public boolean isUserInRole(String role) {
		boolean inRole = false;

		Authentication authentication = getAuthentication();
		if (authentication != null) {
			Collection<? extends GrantedAuthority> authorities = authentication
					.getAuthorities();
			for (GrantedAuthority authority : authorities) {
				if (role.equals(authority.getAuthority())) {
					inRole = true;
					break;
				}
			}
		}
		return inRole;
	}
851114
public function roles(User $user)
    {
        if ($this->session->hasValue(Roles::SESSION_KEY)) {
            return $this->session->value(Roles::SESSION_KEY);
        }

        $roles = $this->authorizationProvider->roles($user);
        $this->session->putValue(Roles::SESSION_KEY, $roles);
        return $roles;
    }
1287186
public function hasRole(string $role): bool
    {
        return (null !== $this->roles) ? \in_array($role, $this->roles) : false;
    }
1063247
public function hasRole($role, $requireAll = false)
    {
        if ($user = $this->user()) {
            return $user->hasRole($role, $requireAll);
        }

        return false;
    }
1073330
public function checkRole( $role, $parent = null )
    {
        if ( null === $role )
        {
            return null;
        }

        if ( $role instanceof Acl\Role\RoleInterface )
        {
            $role = $role->getRoleId();
        }

        $acl = $this->getAcl();

        if ( ! $acl->hasRole( $role ) )
        {
            if ( null === $parent )
            {
                $parent = $this->getParentId( $role );
            }
            elseif ( $parent instanceof Acl\Role\RoleInterface )
            {
                $parent = $parent->getRoleId();
            }

            $parent = $this->checkRole( $parent );
            $acl->addRole( $role, (array) $parent );

            $matches    = array();
            $structures = array();

            if ( preg_match( '/^(\d+)$/', $role, $matches ) ) //group
            {
                $structures = $this->getMapper()
                                   ->findAllByGroupId( $matches[1] );
            }
            else if ( preg_match( '/^(\d+)\.(\d+)$/', $role, $matches ) ) //user
            {
                $structures = $this->getMapper()
                                   ->findAllByUserId( $matches[2] );

                $resource = 'user.group.' . $matches[1] .
                            '.identity.' . $matches[2];

                $this->checkResource( $resource );
                $acl->allow( $role, $resource );
            }

            foreach ( $structures as $structure )
            {
                $this->checkResource( $structure->resource );
                $acl->allow( $role, $structure->resource, $structure->privilege );
            }
        }

        return $role;
    }
75206
public function actionRole($user, $operation, $role)
    {
        $user = $this->getUser($user);
        $role = Yii::$app->authManager->getRole($role);
        if ($operation == 'assign') {
            try {
                $assignment = Yii::$app->authManager->assign($role, $user);
            } catch (\yii\db\IntegrityException $ex) {
                echo "Failed to assign `" . $role->name . "`.\n";
                echo "Maybe the role has been assigned.\n";
                return static::EXIT_CODE_ERROR;
            }
            if ($assignment) {
                echo "`$role->name`" . " assigned to User (" . $user->getID() . ") successfully.\n";
            } else {
                echo "Failed to assign `" . $role->name . "`.\n";
            }
            return static::EXIT_CODE_NORMAL;
        }
        if ($operation == 'revoke') {
            $assignment = Yii::$app->authManager->revoke($role, $user);
            if ($assignment) {
                echo "`$role->name`" . " revoked from User (" . $user->getID() . ").\n";
            } else {
                echo "Failed to revoke `" . $role->name . "`.\n";
                echo "Maybe the role has not been assigned yet.\n";
            }
            return static::EXIT_CODE_NORMAL;
        }
        echo "Unrecognized operation: $operation.\n";
        echo "The accepted operations are `assign` and `revoke`.\n";
        return static::EXIT_CODE_ERROR;
    }
288380
protected function processRule($role) {
        if ($role === '*') {
            return true;
        }
        if ($role === '?') {
            return auth()->guest() ?: $this->renderFailure(__('Please Under Guest!'));
        }
        if ($role === '@') {
            return !auth()->guest() ?:
                $this->renderFailure(__('Please Login User!'), 401, 401);
        }
        if ($role === '!') {
            return $this->renderFailure(__('The page not found！'), 404, 404);
        }
        return $this->processCustomRule($role);
    }
279314
public function checkRole(array $roles)
    {
        // Find a user which matches the required roles.
        $user = $this->getCustomerUser();

        if (! $this->hasRequiredRole($user, $roles)) {
            $user = $this->getAdminUser();

            if (! $this->hasRequiredRole($user, $roles)) {
                $user = null;
            }
        }

        return $user;
    }
484648
public function checkRole($role)
    {
        return $this->getRoles()->contains(function ($value) use ($role) {
            return $role == $value->id || Str::is($role, $value->slug);
        });
    }
642464
public function hasRole($role)
    {
        if (!$this->roles->where('token', $role)->isEmpty()) {
            return true;
        }

        foreach ($this->groups as $group) {
            if (!$group->roles->where('token', $role)->isEmpty()) {
                return true;
            }
        }

        return false;
    }