示例#1
0
 public function testHandleCallback_FailHttp400()
 {
     $ss = array('nkconnect_some_key_otp' => 'aaaa_bbbb_ccc');
     $r = array('nkconnect_state' => 'callback', 'state' => 'aaaa_bbbb_ccc', 'code' => 'auth_code_1234');
     $this->request->expects($this->any())->method('getRequestData')->will($this->returnValue($r));
     $this->request->expects($this->any())->method('getSessionData')->will($this->returnValue($ss));
     $this->request->expects($this->any())->method('unsetSessionData')->will($this->returnValue($this->request));
     $this->http_client->expects($this->once())->method('exec');
     $this->http_client->expects($this->once())->method('getResponseCode')->will($this->returnValue(400));
     $this->http_client->expects($this->once())->method('getResponse')->will($this->returnValue(json_encode(array('error' => 'some_error', 'error_description' => 'Some error description'))));
     $this->object->expects($this->once())->method('authenticated')->will($this->returnValue(false));
     $this->assertFalse($this->object->handleCallback());
     $this->assertArrayHasKey('some_error', $this->object->getErrors());
     $this->assertSame('Some error description', $this->object->getError());
 }
    // Tutaj umieść kod, który wykonany zostanie po udanym zalogowaniu użytkownika na NK, wsztstkie dane użytkownika
    // dostępne są poprzez $auth->user() - możesz także skorzystać z dostępnych serwisów NK, przykłady znajdziesz w
    // pliku services.php.
    // Poniższy kawałek kodu jest prostym przykładem, służy do zademonstrowania *przykładowego* sposobu obsługi
    // użytkowników we własnej bazie danych
    // Sprawdź używając NK id, czy użytkownik znajduje się w bazie danych
    $q = $db->prepare("SELECT * FROM `users` WHERE `nk_person_id` = :nk_person_id");
    $q->bindValue('nk_person_id', $auth->user()->id(), PDO::PARAM_STR);
    $q->execute();
    if (false === ($u = $q->fetch(PDO::FETCH_ASSOC))) {
        // W bazie nie ma użytkownika, zarejestrujmy go
        $q = $db->prepare("INSERT INTO `users` (`nk_person_id`, `name`, `last_login_1`, `last_login_2`, `login_count`) VALUES (:nk_person_id, :name, :now, :now, 1)");
        $q->bindValue('nk_person_id', $auth->user()->id(), PDO::PARAM_STR);
        $q->bindValue('name', $auth->user()->name(), PDO::PARAM_STR);
        $q->bindValue('now', @date("Y-m-d H:i:s"), PDO::PARAM_STR);
        $q->execute();
    } else {
        // Użytkownik istnieje, zaktualizujmy datę ostatniego logowania i podbijmy licznik
        $q = $db->prepare("UPDATE `users` SET `last_login_2` = `last_login_1`, `last_login_1` = :now, `login_count` = (`login_count` + 1) WHERE `id` = :id");
        $q->bindValue('id', $u['id'], PDO::PARAM_INT);
        $q->bindValue('now', @date("Y-m-d H:i:s"), PDO::PARAM_STR);
        $q->execute();
    }
    // Ustaw w sesji informacje o zalogowaniu się użytkownika
    $_SESSION['logged_in'] = true;
    // Przekieruj na stronę początkową
    header("Location: authentication2.php");
} elseif ($error = $auth->getError()) {
    // Wystąpił jakiś błąd podczas procesu autentykacji, wyświetl go lub zrób cokolwiek innego aby go obsłużyć
    echo "<html><head></head><body><div>" . htmlspecialchars($error) . "</div><br /><a href='authentication2.php'>Wróć</a></body></html>";
}