Piwik\Tracker\Request::authenticateSuperUserOrAdmin PHP Method

authenticateSuperUserOrAdmin() public static method

public static authenticateSuperUserOrAdmin ( $tokenAuth, $idSite )
    public static function authenticateSuperUserOrAdmin($tokenAuth, $idSite)
    {
        if (empty($tokenAuth)) {
            return false;
        }
        Piwik::postEvent('Request.initAuthenticationObject');
        /** @var \Piwik\Auth $auth */
        $auth = StaticContainer::get('Piwik\\Auth');
        $auth->setTokenAuth($tokenAuth);
        $auth->setLogin(null);
        $auth->setPassword(null);
        $auth->setPasswordHash(null);
        $access = $auth->authenticate();
        if (!empty($access) && $access->hasSuperUserAccess()) {
            return true;
        }
        // Now checking the list of admin token_auth cached in the Tracker config file
        if (!empty($idSite) && $idSite > 0) {
            $website = Cache::getCacheWebsiteAttributes($idSite);
            if (array_key_exists('admin_token_auth', $website) && in_array((string) $tokenAuth, $website['admin_token_auth'])) {
                return true;
            }
        }
        Common::printDebug("WARNING! token_auth = {$tokenAuth} is not valid, Super User / Admin was NOT authenticated");
        return false;
    }

Usage Example

Example #1
0
 public function test_authenticateSuperUserOrAdmin_ShouldAlwaysWorkForSuperUser()
 {
     Fixture::createSuperUser(false);
     $token = Fixture::getTokenAuth();
     $isAuthenticated = Request::authenticateSuperUserOrAdmin($token, 1);
     $this->assertTrue($isAuthenticated);
     $isAuthenticated = Request::authenticateSuperUserOrAdmin($token, 2);
     $this->assertTrue($isAuthenticated);
 }
All Usage Examples Of Piwik\Tracker\Request::authenticateSuperUserOrAdmin