Language switch
Old articles in French will be kept that way as there is no point in translating them, and will remain for historical purposes.
Enjoy, and see you soon (I hope) for new articles !
<a href="{{ twitter(my_url, 'Tweet this text') }}">Share on Twitter</a>
<a href="{{ facebook(my_url) }}">Share on Facebook</a>
$ git clone [email protected]:dddware/dbot.git
$ cd dbot
$ npm install dbot-list dbot-dfill dbot-dpaste dbot-h5p dbot-url
$ npm start # démarre le bot
User
basique, ainsi que tout ce qu'il faut pour mettre rapidement en place un process d'authentification basé sur les utilisateurs enregistrés par son biais.app/filters.php
:Route::filter(
'auth.admin',
function ($route, $request) {
$user = $request->getUser();
$password = $request->getPassword();
if (($user != Config::get('auth.admin.user')) || ($password != Config::get('auth.admin.password'))) {
// La méthode qui nous intéresse n'est pas publique, on va tricher
// Une manière plus propre de procéder serait d'étendre la classe concernée
$method = new ReflectionMethod('Illuminate\Auth\Guard', 'getBasicResponse');
$method->setAccessible(true);
$response = $method->invoke(new Illuminate\Auth\Guard());
return $response;
}
}
);
app/config/auth.php
comme suit (en bonus, vous pourrez facilement faire varier ces identifiants selon l'environnement d'exécution) :return array(
// ...
'admin' => array(
'user' => 'mylogin',
'password' => 'mypassword'
)
);
Route::get(
'/admin',
array(
'before' => 'auth.admin',
function () {
// L'administrateur est authentifié
}
)
);
class AdminController extends BaseController
{
public function __construct()
{
$this->beforeFilter('auth.admin');
}
}
composer.json
rudimentaire avec les packages employés :{
"require": {
"laravel/framework": "4.1.*",
"phpunit/phpunit": "3.8.*@dev",
"guilhermeguitte/behat-laravel": "dev-master",
"behat/mink": "[email protected]",
"behat/mink-extension": "*",
"behat/mink-selenium2-driver": "*"
},
...
}
.feature
correspondant :Feature: Log in to the app
In order to be able to access my account
I need to be able to fill in a login form with my credentials
Background:
Given I am on "/"
Scenario: Type in a valid account's credentials
Given "[email protected]" has an account with "prout" as the password
When I fill in "Adresse e-mail" with "[email protected]"
And I fill in "Mot de passe" with "prout"
And I press "Connexion"
Then I should be on "/account"
Scenario: Type in a invalid account's credentials
When I fill in "Adresse e-mail" with "[email protected]"
And I fill in "Mot de passe" with "nothing"
And I press "Connexion"
Then I should be on "/"
And I should see "Votre identification a échoué, veuillez réessayer"
Scenario: Submit the form without filling it
When I press "Connexion"
Then I should be on "/"
And I should see "Adresse e-mail : ce champ est requis"
And I should see "Mot de passe : ce champ est requis"
Scenario: Submit the form with an invalid e-mail address in
When I fill in "Adresse e-mail" with "jesuisinvalidelol"
And I press "Connexion"
Then I should be on "/"
And I should see "Adresse e-mail : ce champ est invalide"
Given /^"([^"]*)" has an account with "([^"]*)" as the password$/
:<?php
use Behat\Behat\Exception\PendingException;
use Behat\MinkExtension\Context\MinkContext;
class LoginContext extends MinkContext
{
/**
* @Given /^"([^"]*)" has an account with "([^"]*)" as the password$/
*/
public function hasAnAccountWithAsThePassword($email, $password)
{
$user = new User();
$user->name = 'toto';
$user->email = $email;
$user->setPassword($password);
$user->save();
}
}
User
hérite d'Eloquent
, qui est un alias d'une classe namespacée, accessible uniquement dans le contexte d'une application LaravelHash
, qui de plus doit être initialisé avec un provider de hash (de mon temps, on appelait ça un dealer)<?php
use Behat\Behat\Exception\PendingException;
use Behat\MinkExtension\Context\MinkContext;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Hashing\HashServiceProvider;
use Illuminate\Support\Facades\Facade;
use Illuminate\Database\Capsule;
class LoginContext extends MinkContext
{
public function __construct()
{
// On instancie une app bidon
$app = new Application();
// On lui évite de faire appel à des propriétés indéfinies
$app->bind(
'path.storage',
function () {
return '';
}
);
// On instancie un fournisseur de chiffrement (belle francisation, tiens)
$hash = new HashServiceProvider($app);
$hash->register();
// On utilise notre fausse app
Facade::setFacadeApplication($app);
// On charge les alias existants dans une instance de la classe qui va bien
$appConfig = require(dirname(dirname(dirname(__DIR__))).'/config/app.php');
$this->aliasLoader = AliasLoader::getInstance($appConfig['aliases']);
// On récupère les infos de connexion à la base de données de test
$dbConfig = require(dirname(dirname(dirname(__DIR__))).'/config/database.php');
$this->db = new Capsule\Manager();
$this->db->addConnection($dbConfig['connections']['sqlite_test']);
}
/**
* @Given /^"([^"]*)" has an account with "([^"]*)" as the password$/
*/
public function hasAnAccountWithAsThePassword($email, $password)
{
// On charge les alias dont on va avoir besoin
$this->aliasLoader->load('Eloquent');
$this->aliasLoader->load('Hash');
// On boot Eloquent avec notre instance de la base de données
$this->db->bootEloquent();
// On peut exécuter des requêtes arbitraires comme ceci :
$this->db->getConnection()->delete('DELETE FROM users');
$user = new User();
$user->name = 'toto';
$user->email = $email;
$user->setPassword($password);
$user->save();
// On est bien \o/
}
}