Skip to content

Commit 7102b79

Browse files
committed
bug #487 Yaml source manipulator - fix access control (weaverryan)
This PR was squashed before being merged into the 1.0-dev branch (closes #487). Discussion ---------- Yaml source manipulator - fix access control This helps #460 by fixing a few bugs with YamlSourceManipulator. But, it doesn't yet allow for the `access_control` array structure to be printed in an inline format (#460 (comment)). Cheers! Commits ------- aa6c358 Fixing bug where a scalar value would become and array, but comment placement was wrong 1927d14 Fixing bug when a yaml key with no value ends a file
2 parents 443411a + aa6c358 commit 7102b79

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

src/Util/YamlSourceManipulator.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private function updateData(array $newData)
217217
}
218218

219219
// 3b) value DID change
220-
$this->log('updating value');
220+
$this->log(sprintf('updating value to {%s}', is_array($newVal) ? '<array>' : $newVal));
221221
$this->changeValueInYaml($newVal);
222222
}
223223

@@ -461,12 +461,30 @@ private function changeValueInYaml($value)
461461
// empty space between key & value
462462
$newYamlValue = ' '.$newYamlValue;
463463
}
464+
465+
$newPosition = $this->currentPosition + \strlen($newYamlValue);
466+
$isNextContentComment = $this->isPreviousLineComment($newPosition);
467+
if ($isNextContentComment) {
468+
$newPosition++;
469+
}
470+
464471
$newContents = substr($this->contents, 0, $this->currentPosition)
465472
.$newYamlValue
473+
/*
474+
* If the next line is a comment, this means we probably had
475+
* a structure that looks like this:
476+
* access_control:
477+
* # - { path: ^/admin, roles: ROLE_ADMIN }
478+
*
479+
* In this odd case, we need to know that the next line
480+
* is a comment, so we can add an extra line break.
481+
* Otherwise, the result is something like:
482+
* access_control:
483+
* - { path: /foo, roles: ROLE_USER } # - { path: ^/admin, roles: ROLE_ADMIN }
484+
*/
485+
.($isNextContentComment ? "\n" : '')
466486
.substr($this->contents, $endValuePosition);
467487

468-
$newPosition = $this->currentPosition + \strlen($newYamlValue);
469-
470488
$newData = $this->currentData;
471489
$newData = $this->setValueAtCurrentPath($value, $newData);
472490

@@ -765,6 +783,12 @@ private function findEndPositionOfValue($value, $offset = null)
765783

766784
$offset = null === $offset ? $this->currentPosition : $offset;
767785

786+
// a value like "foo:" can simply end a file
787+
// this means the value is null
788+
if ($offset === strlen($this->contents)) {
789+
return $offset;
790+
}
791+
768792
preg_match(sprintf('#%s#', $pattern), $this->contents, $matches, PREG_OFFSET_CAPTURE, $offset);
769793
if (empty($matches)) {
770794
throw new YamlManipulationFailedException(sprintf('Cannot find the original value "%s"', $value));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
security:
2+
access_control:
3+
===
4+
$data['security']['access_control'] = 'foo';
5+
===
6+
security:
7+
access_control: foo
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
2+
security:
3+
access_control:
4+
===
5+
$data['security']['access_control'] = [];
6+
$data['security']['access_control'][] = ['path' => '^/login$', 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY'];
7+
===
8+
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
9+
security:
10+
access_control:
11+
-
12+
path: ^/login$
13+
roles: IS_AUTHENTICATED_ANONYMOUSLY
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
2+
security:
3+
access_control:
4+
# - { path: ^/admin, roles: ROLE_ADMIN }
5+
# - { path: ^/profile, roles: ROLE_USER }
6+
===
7+
$data['security']['access_control'] = [];
8+
$data['security']['access_control'][] = ['path' => '^/login$', 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY'];
9+
===
10+
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
11+
security:
12+
access_control:
13+
-
14+
path: ^/login$
15+
roles: IS_AUTHENTICATED_ANONYMOUSLY
16+
# - { path: ^/admin, roles: ROLE_ADMIN }
17+
# - { path: ^/profile, roles: ROLE_USER }

0 commit comments

Comments
 (0)