8000 Merge branch '2.4' · symfony/symfony@c15175a · GitHub
[go: up one dir, main page]

Skip to content

Commit c15175a

Browse files
committed
Merge branch '2.4'
* 2.4: fix some cs use restore_error_handler instead of set_error_handler($previous) fix #9321 Crawler::addHtmlContent add gbk encoding support [Console] fixed column width when using the Table helper with some decoration in cells [Security] Fixed problem with losing ROLE_PREVIOUS_ADMIN role. Fix for cache-key conflict when having a \Traversable as choices [Security] removed obsolete comment
2 parents 9fbe148 + 37813bd commit c15175a

File tree

7 files changed

+148
-13
lines changed

7 files changed

+148
-13
lines changed

src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
8989
array_walk_recursive($choiceHashes, function (&$value) {
9090
$value = spl_object_hash($value);
9191
});
92+
} elseif ($choiceHashes instanceof \Traversable) {
93+
$hashes = array();
94+
foreach ($choiceHashes as $value) {
95+
$hashes[] = spl_object_hash($value);
96+
}
97+
98+
$choiceHashes = $hashes;
9299
}
93100

94101
8000 $preferredChoiceHashes = $options['preferred_choices'];

src/Symfony/Component/Console/Helper/TableHelper.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ private function renderCell(array $row, $column, $cellFormat)
427427
$width += strlen($cell) - mb_strlen($cell, $encoding);
428428
}
429429

430+
$width += $this->strlen($cell) - $this->computeLengthWithoutDecoration($cell);
431+
430432
$content = sprintf($this->cellRowContentFormat, $cell);
431433

432434
$this->output->write(sprintf($cellFormat, str_pad($content, $width, $this->paddingChar, $this->padType)));
@@ -484,15 +486,7 @@ private function getColumnWidth($column)
484486
*/
485487
private function getCellWidth(array $row, $column)
486488
{
487-
if ($column < 0) {
488-
return 0;
489-
}
490-
491-
if (isset($row[$column])) {
492-
return $this->strlen($row[$column]);
493-
}
494-
495-
return $this->getCellWidth($row, $column - 1);
489+
return isset($row[$column]) ? $this->computeLengthWithoutDecoration($row[$column]) : 0;
496490
}
497491

498492
/**
@@ -504,6 +498,18 @@ private function cleanup()
504498
$this->numberOfColumns = null;
505499
}
506500 9E88

501+
private function computeLengthWithoutDecoration($string)
502+
{
503+
$formatter = $this->output->getFormatter();
504+
$isDecorated = $formatter->isDecorated();
505+
$formatter->setDecorated(false);
506+
507+
$string = $formatter->format($string);
508+
$formatter->setDecorated($isDecorated);
509+
510+
return $this->strlen($string);
511+
}
512+
507513
/**
508514
* {@inheritDoc}
509515
*/

src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,40 @@ public function testRenderProvider()
216216
TableHelper::LAYOUT_DEFAULT,
217217
'',
218218
),
219+
'Cell text with tags used for Output styling' => array(
220+
array('ISBN', 'Title', 'Author'),
221+
array(
222+
array('<info>99921-58-10-7</info>', '<error>Divine Comedy</error>', '<fg=blue;bg=white>Dante Alighieri</fg=blue;bg=white>'),
223+
array('9971-5-0210-0', 'A Tale of Two Cities', '<info>Charles Dickens</>'),
224+
),
225+
TableHelper::LAYOUT_DEFAULT,
226+
<<<TABLE
227+
+---------------+----------------------+-----------------+
228+
| ISBN | Title | Author |
229+
+---------------+----------------------+-----------------+
230+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
231+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
232+
+---------------+----------------------+-----------------+
233+
234+
TABLE
235+
),
236+
'Cell text with tags not used for Output styling' => array(
237+
array('ISBN', 'Title', 'Author'),
238+
array(
239+
array('<strong>99921-58-10-700</strong>', '<f>Divine Com</f>', 'Dante Alighieri'),
240+
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
241+
),
242+
TableHelper::LAYOUT_DEFAULT,
243+
<<<TABLE
244+
+----------------------------------+----------------------+-----------------+
245+
| ISBN | Title | Author |
246+
+----------------------------------+----------------------+-----------------+
247+
| <strong>99921-58-10-700</strong> | <f>Divine Com</f> | Dante Alighieri |
248+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
249+
+----------------------------------+----------------------+-----------------+
250+
251+
TABLE
252+
),
219253
);
220254
}
221255

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,18 @@ public function addHtmlContent($content, $charset = 'UTF-8')
157157
$dom = new \DOMDocument('1.0', $charset);
158158
$dom->validateOnParse = true;
159159

160-
if (function_exists('mb_convert_encoding') && in_array(strtolower($charset), array_map('strtolower', mb_list_encodings()))) {
161-
$content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
160+
if (function_exists('mb_convert_encoding')) {
161+
$hasError = false;
162+
set_error_handler(function () use (&$hasError) {
163+
$hasError = true;
164+
});
165+
$tmpContent = @mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
166+
167+
restore_error_handler();
168+
169+
if (!$hasError) {
170+
$content = $tmpContent;
171+
}
162172
}
163173

164174
@$dom->loadHTML($content);

src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ public function testAddHtmlContentUnsupportedCharset()
113113
$this->assertEquals('Žťčýů', $crawler->filterXPath('//p')->text());
114114
}
115115

116+
/**
117+
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
118+
*/
119+
public function testAddHtmlContentCharsetGbk()
120+
{
121+
$crawler = new Crawler();
122+
//gbk encode of <html><p>中文</p></html>
123+
$crawler->addHtmlContent(base64_decode('PGh0bWw+PHA+1tDOxDwvcD48L2h0bWw+'), 'gbk');
124+
125+
$this->assertEquals('中文', $crawler->filterXPath('//p')->text());
126+
}
127+
116128
/**
117129
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
118130
*/

src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
2020
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2121
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
22+
use Symfony\Component\Security\Core\Role\SwitchUserRole;
2223

2324
/**
2425
* UserProviderInterface retrieves users for UsernamePasswordToken tokens.
@@ -92,7 +93,7 @@ public function authenticate(TokenInterface $token)
9293
throw $e;
9394
}
9495

95-
$authenticatedToken = new UsernamePasswordToken($user, $token->getCredentials(), $this->providerKey, $user->getRoles());
96+
$authenticatedToken = new UsernamePasswordToken($user, $token->getCredentials(), $this->providerKey, $this->getRoles($user, $token));
9697
$authenticatedToken->setAttributes($token->getAttributes());
9798

9899
return $authenticatedToken;
@@ -106,6 +107,29 @@ public function supports(TokenInterface $token)
106107
return $token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey();
107108
}
108109

110+
/**
111+
* Retrieves roles from user and appends SwitchUserRole if original token contained one.
112+
*
113+
* @param UserInterface $user The user
114+
* @param TokenInterface $token The token
115+
*
116+
* @return Role[] The user roles
117+
*/
118+
private function getRoles(UserInterface $user, TokenInterface $token)
119+
{
120+
$roles = $user->getRoles();
121+
122+
foreach ($token->getRoles() as $role) {
123+
if ($role instanceof SwitchUserRole) {
124+
$roles[] = $role;
125+
126+
break;
127+
}
128+
}
129+
130+
return $roles;
131+
}
132+
109133
/**
110134
* Retrieves the user from an implementation-specific location.
111135
*

src/Symfony/Component/Security/Core/Tests/Authentication/Provider/UserAuthenticationProviderTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider;
1515
use Symfony\Component\Security\Core\Role\Role;
16+
use Symfony\Component\Security\Core\Role\SwitchUserRole;
1617
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1718

1819
class UserAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
@@ -172,6 +173,11 @@ public function testAuthenticate()
172173
->will($this->returnValue('foo'))
173174
;
174175

176+
$token->expects($this->once())
177+
->method('getRoles')
178+
->will($this->returnValue(array()))
179+
;
180+
175181
$authToken = $provider->authenticate($token);
176182

177183
$this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $authToken);
@@ -181,9 +187,45 @@ public function testAuthenticate()
181187
$this->assertEquals(array('foo' => 'bar'), $authToken->getAttributes(), '->authenticate() copies token attributes');
182188
}
183189

190+
public function testAuthenticateWithPreservingRoleSwitchUserRole()
191+
{
192+
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
193+
$user->expects($this->once())
194+
->method('getRoles')
195+
->will($this->returnValue(array('ROLE_FOO')))
196+
;
197+
198+
$provider = $this->getProvider();
199+
$provider->expects($this->once())
200+
->method('retrieveUser')
201+
->will($this->returnValue($user))
202+
;
203+
204+
$token = $this->getSupportedToken();
205+
$token->expects($this->once())
206+
->method('getCredentials')
207+
->will($this->returnValue('foo'))
208+
;
209+
210+
$switchUserRole = new SwitchUserRole('foo', $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'));
211+
$token->expects($this->once())
212+
->method('getRoles')
213+
->will($this->returnValue(array($switchUserRole)))
214+
;
215+
216+
$authToken = $provider->authenticate($token);
217+
218+
$this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $authToken);
219+
$this->assertSame($user, $authToken->getUser());
220+
$this->assertContains(new Role('ROLE_FOO'), $authToken->getRoles(), '', false, false);
221+
$this->assertContains($switchUserRole, $authToken->getRoles());
222+
$this->assertEquals('foo', $authToken->getCredentials());
223+
$this->assertEquals(array('foo' => 'bar'), $authToken->getAttributes(), '->authenticate() copies token attributes');
224+
}
225+
184226
protected function getSupportedToken()
185227
{
186-
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', array('getCredentials', 'getProviderKey'), array(), '', false);
228+
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', array('getCredentials', 'getProviderKey', 'getRoles'), array(), '', false);
187229
$mock
188230
->expects($this->any())
189231
->method('getProviderKey')

0 commit comments

Comments
 (0)
0