Skip to content

Commit

Permalink
Merge branch '2.4'
Browse files Browse the repository at this point in the history
* 2.4:
  fix docblock
  Fixed incompatibility of x509 auth with nginx
  [Process] Setting STDIN while running should not be possible
  [Validator] slovenian translation updated
  [FrameworkBundle] improve English in RouterMatchCommand
  [Validator] Updated Hungarian translations
  [Doctrine Bridge] simplify session handler by using main connection
  [Validator] Fixed typos in German translation
  [Validator] Completed French translations
  [Validator] Completed German translations
  [Validator] Completed Luxembourgish translations
  • Loading branch information
fabpot committed Apr 22, 2014
2 parents 27e1a89 + 0deaceb commit 6658989
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 69 deletions.
61 changes: 22 additions & 39 deletions src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionHandler.php
Expand Up @@ -12,21 +12,12 @@
namespace Symfony\Bridge\Doctrine\HttpFoundation;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
use Doctrine\DBAL\Driver\OCI8\OCI8Connection;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\SQLSrv\SQLSrvConnection;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;

/**
* DBAL based session storage.
*
* This implementation is very similar to Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
* but uses the Doctrine driver connection interface and thus also works with non-PDO-based drivers like mysqli and OCI8.
* but uses a Doctrine connection and thus also works with non-PDO-based drivers like mysqli and OCI8.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
Expand All @@ -35,7 +26,7 @@
class DbalSessionHandler implements \SessionHandlerInterface
{
/**
* @var DriverConnection
* @var Connection
*/
private $con;

Expand All @@ -62,10 +53,10 @@ class DbalSessionHandler implements \SessionHandlerInterface
/**
* Constructor.
*
* @param DriverConnection $con A driver connection, preferably a wrapper Doctrine\DBAL\Connection for lazy connections
* @param string $tableName Table name
* @param Connection $con A connection
* @param string $tableName Table name
*/
public function __construct(DriverConnection $con, $tableName = 'sessions')
public function __construct(Connection $con, $tableName = 'sessions')
{
$this->con = $con;
$this->table = $tableName;
Expand All @@ -74,7 +65,7 @@ public function __construct(DriverConnection $con, $tableName = 'sessions')
/**
* {@inheritdoc}
*/
public function open($path = null, $name = null)
public function open($savePath, $sessionName)
{
return true;
}
Expand All @@ -90,14 +81,14 @@ public function close()
/**
* {@inheritdoc}
*/
public function destroy($id)
public function destroy($sessionId)
{
// delete the record associated with this id
$sql = "DELETE FROM $this->table WHERE $this->idCol = :id";

try {
$stmt = $this->con->prepare($sql);
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
$stmt->bindParam(':id', $sessionId, \PDO::PARAM_STR);
$stmt->execute();
} catch (\Exception $e) {
throw new \RuntimeException(sprintf('Exception was thrown when trying to delete a session: %s', $e->getMessage()), 0, $e);
Expand All @@ -109,14 +100,14 @@ public function destroy($id)
/**
* {@inheritdoc}
*/
public function gc($lifetime)
public function gc($maxlifetime)
{
// delete the session records that have expired
$sql = "DELETE FROM $this->table WHERE $this->timeCol < :time";

try {
$stmt = $this->con->prepare($sql);
$stmt->bindValue(':time', time() - $lifetime, \PDO::PARAM_INT);
$stmt->bindValue(':time', time() - $maxlifetime, \PDO::PARAM_INT);
$stmt->execute();
} catch (\Exception $e) {
throw new \RuntimeException(sprintf('Exception was thrown when trying to delete expired sessions: %s', $e->getMessage()), 0, $e);
Expand All @@ -128,13 +119,13 @@ public function gc($lifetime)
/**
* {@inheritdoc}
*/
public function read($id)
public function read($sessionId)
{
$sql = "SELECT $this->dataCol FROM $this->table WHERE $this->idCol = :id";

try {
$stmt = $this->con->prepare($sql);
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
$stmt->bindParam(':id', $sessionId, \PDO::PARAM_STR);
$stmt->execute();

// We use fetchAll instead of fetchColumn to make sure the DB cursor gets closed
Expand All @@ -153,7 +144,7 @@ public function read($id)
/**
* {@inheritdoc}
*/
public function write($id, $data)
public function write($sessionId, $data)
{
// Session data can contain non binary safe characters so we need to encode it.
$encoded = base64_encode($data);
Expand All @@ -166,7 +157,7 @@ public function write($id, $data)

if (null !== $mergeSql) {
$mergeStmt = $this->con->prepare($mergeSql);
$mergeStmt->bindParam(':id', $id, \PDO::PARAM_STR);
$mergeStmt->bindParam(':id', $sessionId, \PDO::PARAM_STR);
$mergeStmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
$mergeStmt->bindValue(':time', time(), \PDO::PARAM_INT);
$mergeStmt->execute();
Expand All @@ -180,13 +171,13 @@ public function write($id, $data)
$deleteStmt = $this->con->prepare(
"DELETE FROM $this->table WHERE $this->idCol = :id"
);
$deleteStmt->bindParam(':id', $id, \PDO::PARAM_STR);
$deleteStmt->bindParam(':id', $sessionId, \PDO::PARAM_STR);
$deleteStmt->execute();

$insertStmt = $this->con->prepare(
"INSERT INTO $this->table ($this->idCol, $this->dataCol, $this->timeCol) VALUES (:id, :data, :time)"
);
$insertStmt->bindParam(':id', $id, \PDO::PARAM_STR);
$insertStmt->bindParam(':id', $sessionId, \PDO::PARAM_STR);
$insertStmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
$insertStmt->bindValue(':time', time(), \PDO::PARAM_INT);
$insertStmt->execute();
Expand All @@ -211,32 +202,24 @@ public function write($id, $data)
*/
private function getMergeSql()
{
$platform = $pdoDriver = null;
$platform = $this->con->getDatabasePlatform()->getName();

if ($this->con instanceof Connection) {
$platform = $this->con->getDatabasePlatform();
} elseif ($this->con instanceof PDOConnection) {
$pdoDriver = $this->con->getAttribute(\PDO::ATTR_DRIVER_NAME);
}

switch (true) {
case $this->con instanceof MysqliConnection || $platform instanceof MySqlPlatform || 'mysql' === $pdoDriver:
switch ($platform) {
case 'mysql':
return "INSERT INTO $this->table ($this->idCol, $this->dataCol, $this->timeCol) VALUES (:id, :data, :time) " .
"ON DUPLICATE KEY UPDATE $this->dataCol = VALUES($this->dataCol), $this->timeCol = VALUES($this->timeCol)";
case $this->con instanceof OCI8Connection || $platform instanceof OraclePlatform || 'oci' === $pdoDriver:
case 'oracle':
// DUAL is Oracle specific dummy table
return "MERGE INTO $this->table USING DUAL ON ($this->idCol = :id) " .
"WHEN NOT MATCHED THEN INSERT ($this->idCol, $this->dataCol, $this->timeCol) VALUES (:id, :data, :time) " .
"WHEN MATCHED THEN UPDATE SET $this->dataCol = :data";
case $this->con instanceof SQLSrvConnection || $platform instanceof SQLServerPlatform || 'sqlsrv' === $pdoDriver:
case 'mssql':
// MS SQL Server requires MERGE be terminated by semicolon
return "MERGE INTO $this->table USING (SELECT 'x' AS dummy) AS src ON ($this->idCol = :id) " .
"WHEN NOT MATCHED THEN INSERT ($this->idCol, $this->dataCol, $this->timeCol) VALUES (:id, :data, :time) " .
"WHEN MATCHED THEN UPDATE SET $this->dataCol = :data;";
case $platform instanceof SqlitePlatform || 'sqlite' === $pdoDriver:
case 'sqlite':
return "INSERT OR REPLACE INTO $this->table ($this->idCol, $this->dataCol, $this->timeCol) VALUES (:id, :data, :time)";
}

return null;
}
}
Expand Up @@ -22,9 +22,7 @@ class DbalSessionHandlerTest extends \PHPUnit_Framework_TestCase
{
public function testConstruct()
{
$this->connection = $this->getMock('Doctrine\DBAL\Driver\Connection');
$mock = $this->getMockBuilder('Symfony\Bridge\Doctrine\HttpFoundation\DbalSessionHandler');
$mock->setConstructorArgs(array($this->connection));
$this->driver = $mock->getMock();
$connection = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock();
$handler = new DbalSessionHandler($connection);
}
}
Expand Up @@ -107,7 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if (!$matches) {
$output->writeln('<fg=red>None of the routes matches</>');
$output->writeln('<fg=red>None of the routes match</>');

return 1;
}
Expand Down
Expand Up @@ -33,7 +33,7 @@ public function __construct($name, NodeParentInterface $parent = null)
/**
* Instantiate a Node
*
* @return boolNode The node
* @return BooleanNode The node
*/
protected function instantiateNode()
{
Expand Down
Expand Up @@ -23,7 +23,7 @@ class IntegerNodeDefinition extends NumericNodeDefinition
/**
* Instantiates a Node.
*
* @return intNode The node
* @return IntegerNode The node
*/
protected function instantiateNode()
{
Expand Down
Expand Up @@ -81,7 +81,7 @@ public function scalarNode($name)
*
* @param string $name The name of the node
*
* @return boolNodeDefinition The child node
* @return BooleanNodeDefinition The child node
*/
public function booleanNode($name)
{
Expand All @@ -93,7 +93,7 @@ public function booleanNode($name)
*
* @param string $name the name of the node
*
* @return intNodeDefinition The child node
* @return IntegerNodeDefinition The child node
*/
public function integerNode($name)
{
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Process/Process.php
Expand Up @@ -1046,9 +1046,15 @@ public function getStdin()
* @param string|null $stdin The new contents
*
* @return self The current Process instance
*
* @throws LogicException In case the process is running
*/
public function setStdin($stdin)
{
if ($this->isRunning()) {
throw new LogicException('STDIN can not be set while the process is running.');
}

$this->stdin = $stdin;

return $this;
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Process/Tests/AbstractProcessTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Process\Tests;

use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Exception\LogicException;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\ProcessPipes;
Expand Down Expand Up @@ -157,6 +158,20 @@ public function testProcessPipes($code, $size)
$this->assertEquals($expectedLength, strlen($p->getErrorOutput()));
}

public function testSetStdinWhileRunningThrowsAnException()
{
$process = $this->getProcess('php -r "usleep(500000);"');
$process->start();
try {
$process->setStdin('foobar');
$process->stop();
$this->fail('A LogicException should have been raised.');
} catch (LogicException $e) {
$this->assertEquals('STDIN can not be set while the process is running.', $e->getMessage());
}
$process->stop();
}

public function chainedCommandsOutputProvider()
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
Expand Down
Expand Up @@ -41,10 +41,17 @@ public function __construct(SecurityContextInterface $securityContext, Authentic
*/
protected function getPreAuthenticatedData(Request $request)
{
if (!$request->server->has($this->userKey)) {
throw new BadCredentialsException(sprintf('SSL key was not found: %s', $this->userKey));
$user = null;
if ($request->server->has($this->userKey)) {
$user = $request->server->get($this->userKey);
} elseif ($request->server->has($this->credentialKey) && preg_match('#/emailAddress=(.+\@.+\..+)(/|$)#', $request->server->get($this->credentialKey), $matches)) {
$user = $matches[1];
}

return array($request->server->get($this->userKey), $request->server->get($this->credentialKey, ''));
if (null === $user) {
throw new BadCredentialsException(sprintf('SSL credentials not found: %s, %s', $this->userKey, $this->credentialKey));
}

return array($user, $request->server->get($this->credentialKey, ''));
}
}
Expand Up @@ -35,11 +35,7 @@ public function testGetPreAuthenticatedData($user, $credentials)

$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');

$listener = new X509AuthenticationListener(
$context,
$authenticationManager,
'TheProviderKey'
);
$listener = new X509AuthenticationListener($context, $authenticationManager, 'TheProviderKey');

$method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
$method->setAccessible(true);
Expand All @@ -56,22 +52,47 @@ public static function dataProviderGetPreAuthenticatedData()
);
}

/**
* @dataProvider dataProviderGetPreAuthenticatedDataNoUser
*/
public function testGetPreAuthenticatedDataNoUser($emailAddress)
{
$credentials = 'CN=Sample certificate DN/emailAddress='.$emailAddress;
$request = new Request(array(), array(), array(), array(), array(), array('SSL_CLIENT_S_DN' => $credentials));

$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');

$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');

$listener = new X509AuthenticationListener($context, $authenticationManager, 'TheProviderKey');

$method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
$method->setAccessible(true);

$result = $method->invokeArgs($listener, array($request));
$this->assertSame($result, array($emailAddress, $credentials));
}

public static function dataProviderGetPreAuthenticatedDataNoUser()
{
return array(
'basicEmailAddress' => array('cert@example.com'),
'emailAddressWithPlusSign' => array('cert+something@example.com'),
);
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
*/
public function testGetPreAuthenticatedDataNoUser()
public function testGetPreAuthenticatedDataNoData()
{
$request = new Request(array(), array(), array(), array(), array(), array());

$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');

$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');

$listener = new X509AuthenticationListener(
$context,
$authenticationManager,
'TheProviderKey'
);
$listener = new X509AuthenticationListener($context, $authenticationManager, 'TheProviderKey');

$method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
$method->setAccessible(true);
Expand All @@ -91,13 +112,7 @@ public function testGetPreAuthenticatedDataWithDifferentKeys()

$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');

$listener = new X509AuthenticationListener(
$context,
$authenticationManager,
'TheProviderKey',
'TheUserKey',
'TheCredentialsKey'
);
$listener = new X509AuthenticationListener($context, $authenticationManager, 'TheProviderKey', 'TheUserKey', 'TheCredentialsKey');

$method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
$method->setAccessible(true);
Expand Down

0 comments on commit 6658989

Please sign in to comment.