Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create impersonation_exit_path() and *_url() functions#32841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:masterfromdFayet:twig_bridge_impersonation
Sep 6, 2020

Conversation

@dFayet
Copy link

@dFayetdFayet commentedJul 31, 2019
edited by javiereguiluz
Loading

QA
Branch?4.4
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes (not added atm)
Fixed tickets#24676
LicenseMIT
Doc PRTo come later

This is a relaunch of the PR#24737

It adds more flexibility to the previous try.
You have two twig functionsimpersonation_exit_url() andimpersonation_exit_path()

You can either leave on the same path, redirect to the path where was the user at the user switch, or to the path you want.

Example:

The following code

<p>{{ impersonation_exit_url() }}</p><p>{{ impersonation_exit_path() }}</p><p>&nbsp;</p><p>{{ impersonation_exit_url(path('app_default_other')) }}</p><p>{{ impersonation_exit_path(path('app_default_other')) }}</p><p>&nbsp;</p>    <p>{{ impersonation_exit_url('_use_impersonated_from_url') }}</p><p>{{ impersonation_exit_path('_use_impersonated_from_url') }}</p>

will output
Capture d’écran de 2019-07-31 20-58-42

Note: If this proposal appears to be better, I'll add tests, update changelog, and prepare the doc.

Bonus:
As the "impersonated from url" is stored in theSwitchUserToken it might be possible to display it in the profiler:

Capture d’écran de 2019-07-31 21-04-50

WDYT?

zairigimad and matyo91 reacted with thumbs up emoji
$this->firewallMap =$firewallMap;
}

publicfunctiongenerateImpersonateExitUrl(string$exitTo =null,$referenceType = UrlGeneratorInterface::ABSOLUTE_URL):string
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I definitely don't like the nameexitTo, any suggestion?

Copy link
Contributor

@ro0NLro0NLAug 10, 2019
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

i suggestgenerateExitUrl/Path(string $targetUri = null): string

@dFayetdFayetforce-pushed thetwig_bridge_impersonation branch fromef15c60 to16a96f1CompareJuly 31, 2019 19:17
@nicolas-grekasnicolas-grekas added this to thenext milestoneAug 2, 2019
@dFayet
Copy link
Author

ping@Taluu

@dFayetdFayetforce-pushed thetwig_bridge_impersonation branch 2 times, most recently fromb13daf3 toe0c8ebbCompareAugust 10, 2019 11:12

privatefunctionisImpersonatedUser():bool
{
if (null ===$token =$this->tokenStorage->getToken()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

return $this->tokenStorage->getToken() instanceof SwitchUserToken?

class SwitchUserTokenextends UsernamePasswordToken
{
private$originalToken;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

newline not needed

{
private$originalToken;

private$impersonatedFromUri;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

what about$originatedFromUri

* @param mixed $credentials This usually is the password of the user
* @param string $providerKey The provider key
* @param string[] $roles An array of roles
* @param string|null $impersonatedFromUri The Url where was the user at the switch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The URI ...

$roles[] =newSwitchUserRole('ROLE_PREVIOUS_ADMIN',$this->tokenStorage->getToken(),false);

$token =newSwitchUserToken($user,$user->getPassword(),$this->providerKey,$roles,$token);
$token =newSwitchUserToken(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

i still think we prefer one line :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

me too :)

class ImpersonateUrlGenerator
{
private$requestStack;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

newlines not needed

fabpot reacted with thumbs up emoji

publicfunctiongenerateImpersonateExitUrl(string$exitTo =null,$referenceType = UrlGeneratorInterface::ABSOLUTE_URL):string
{
$request =$this->requestStack->getCurrentRequest();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

can be moved down the if the below


useSymfony\Bundle\SecurityBundle\Security\FirewallMap;
useSymfony\Component\HttpFoundation\RequestStack;
useSymfony\Component\Routing\Generator\UrlGeneratorInterface;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

this is only a dev-dependency, not sure we should rely on it here (we dont have to)

perhaps create 2 separate methods as done inSecurityExtension, or inline the consts

thrownew \LogicException('Unable to generate the impersonate exit URL without a firewall configured for the user switch.');
}

if ('_use_impersonated_from_url' ===$exitTo) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

perhaps we should decouple this into its own (twig) method so we can do

{{ impersonation_exit_url(impersonation_start_url()) }}

meaning we can implementimpersonation_start_url as a hybrid;
if impersonated: points to "originated from uri"
else: points to "switch user uri"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

and perhaps for completenessis_impersonated()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

and perhaps for completenessis_impersonated()

I don't think it's worth adding this function, as this information can be simply retrieved by doing{% if is_granted('ROLE_PREVIOUS_ADMIN') %}, your thoughts?

ro0NL reacted with thumbs up emoji
Copy link
Author

@dFayetdFayetAug 11, 2019
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@ro0NL About your idea ofimpersonation_start_url should we also consider to create function to get url/path to start impersonation?

I thing the nameimpersonation_start_url might be misleading as it looks like the*_exit* ones, but does not really generate urls to "start" the impersonation.

I would be more confident with names likeimpersonation_get_start_url orimpersonation_started_url.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm not sure I like this_use_impersonated_from_url feature. I would drop it completely (at least at first).

Copy link
Member

@fabpotfabpot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@dFayet I've just made some comments (mainly because core code has changed), can you take the comments into account and rebase on current master?

dFayet reacted with thumbs up emoji
<argumenttype="service"id="request_stack" />
<argumenttype="service"id="security.firewall.map" />
<argumenttype="service"id="security.token_storage" />
</service>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Should be moved to the new PHP configuration file

<serviceid="twig.extension.security"class="Symfony\Bridge\Twig\Extension\SecurityExtension">
<tagname="twig.extension" />
<argumenttype="service"id="security.authorization_checker"on-invalid="ignore" />
<argumenttype="service"id="security.impersonate_url_generator"on-invalid="ignore" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Should be moved to the new PHP configuration file

"symfony/security-csrf":"^4.2|^5.0",
"symfony/security-guard":"^4.2|^5.0",
"symfony/security-http":"^4.3"
"symfony/security-http":"^4.4"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

5.2

$roles[] =newSwitchUserRole('ROLE_PREVIOUS_ADMIN',$this->tokenStorage->getToken(),false);

$token =newSwitchUserToken($user,$user->getPassword(),$this->providerKey,$roles,$token);
$token =newSwitchUserToken(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

me too :)

$this->providerKey,
$roles,
$token,
str_replace('/&','/?',preg_replace('#[&?]'.$this->usernameParameter.'=[^&]*#','',$request->getRequestUri()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

you might want to create a temp var instead

thrownew \LogicException('Unable to generate the impersonate exit URL without a firewall configured for the user switch.');
}

if ('_use_impersonated_from_url' ===$exitTo) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm not sure I like this_use_impersonated_from_url feature. I would drop it completely (at least at first).

@fabpot
Copy link
Member

@dFayet Do you have time to finish this PR?

@dFayet
Copy link
Author

@fabpot I will try to finish it today, otherwise, at worst, this week it should be ready

@dFayetdFayetforce-pushed thetwig_bridge_impersonation branch 3 times, most recently from945d41f to18604f6CompareSeptember 6, 2020 08:40
@dFayetdFayetforce-pushed thetwig_bridge_impersonation branch from18604f6 toc1e3703CompareSeptember 6, 2020 08:52
@dFayet
Copy link
Author

dFayet commentedSep 6, 2020
edited
Loading

@fabpot This should be ready now.

@dFayetdFayet requested a review fromfabpotSeptember 6, 2020 09:05
@fabpot
Copy link
Member

Thank you@dFayet.

publicfunction__serialize():array
{
return [$this->originalToken,parent::__serialize()];
return [$this->originalToken,$this->originatedFromUri,parent::__serialize()];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Don't we need to add at the end to avoid issues with existing data when applications are updated?

wouterj reacted with thumbs up emoji
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestSep 7, 2020
This PR was merged into the master branch.Discussion----------Leave impersonation functionsWill solve#14183Documents new Twig functions `impersonation_exit_path` and `impersonation_exit_url` added insymfony/symfony#32841Commits-------b7bd572 Leave impersonation functions
@nicolas-grekasnicolas-grekas modified the milestones:next,5.2Oct 5, 2020
@fabpotfabpot mentioned this pull requestOct 5, 2020
@dFayetdFayet deleted the twig_bridge_impersonation branchMarch 9, 2023 08:36
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@xabbuhxabbuhxabbuh left review comments

@fabpotfabpotfabpot approved these changes

@dunglasdunglasAwaiting requested review from dunglas

@jderussejderusseAwaiting requested review from jderusse

@lyrixxlyrixxAwaiting requested review from lyrixx

@srozesrozeAwaiting requested review from sroze

+2 more reviewers

@ro0NLro0NLro0NL left review comments

@TaluuTaluuTaluu approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

5.2

Development

Successfully merging this pull request may close these issues.

7 participants

@dFayet@fabpot@Taluu@ro0NL@xabbuh@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp