mirror of
https://we.phorge.it/source/phorge.git
synced 2025-02-09 05:18:29 +01:00
Reduce the amount of OAuth1/OAuth2 code duplication for rendering login buttons
Summary: Ref T3687. These buttons don't work quite the same way, but are similar enough that the code seems worth consolidating. Test Plan: Viewed and clicked both OAuth1 (Twitter, JIRA) and OAuth2 (Facebook) login buttons. Got logins. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T3687 Differential Revision: https://secure.phabricator.com/D6874
This commit is contained in:
parent
25eb401e18
commit
e5b4ce5525
3 changed files with 87 additions and 93 deletions
|
@ -360,4 +360,80 @@ abstract class PhabricatorAuthProvider {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render a standard login/register button element.
|
||||||
|
*
|
||||||
|
* The `$attributes` parameter takes these keys:
|
||||||
|
*
|
||||||
|
* - `uri`: URI the button should take the user to when clicked.
|
||||||
|
* - `method`: Optional HTTP method the button should use, defaults to GET.
|
||||||
|
*
|
||||||
|
* @param AphrontRequest HTTP request.
|
||||||
|
* @param string Request mode string.
|
||||||
|
* @param map Additional parameters, see above.
|
||||||
|
* @return wild Login button.
|
||||||
|
*/
|
||||||
|
protected function renderStandardLoginButton(
|
||||||
|
AphrontRequest $request,
|
||||||
|
$mode,
|
||||||
|
array $attributes = array()) {
|
||||||
|
|
||||||
|
PhutilTypeSpec::checkMap(
|
||||||
|
$attributes,
|
||||||
|
array(
|
||||||
|
'method' => 'optional string',
|
||||||
|
'uri' => 'string',
|
||||||
|
));
|
||||||
|
|
||||||
|
$viewer = $request->getUser();
|
||||||
|
$adapter = $this->getAdapter();
|
||||||
|
|
||||||
|
if ($mode == 'link') {
|
||||||
|
$button_text = pht('Link External Account');
|
||||||
|
} else if ($mode == 'refresh') {
|
||||||
|
$button_text = pht('Refresh Account Link');
|
||||||
|
} else if ($this->shouldAllowRegistration()) {
|
||||||
|
$button_text = pht('Login or Register');
|
||||||
|
} else {
|
||||||
|
$button_text = pht('Login');
|
||||||
|
}
|
||||||
|
|
||||||
|
$icon = id(new PHUIIconView())
|
||||||
|
->setSpriteSheet(PHUIIconView::SPRITE_LOGIN)
|
||||||
|
->setSpriteIcon($this->getLoginIcon());
|
||||||
|
|
||||||
|
$button = id(new PHUIButtonView())
|
||||||
|
->setSize(PHUIButtonView::BIG)
|
||||||
|
->setColor(PHUIButtonView::GREY)
|
||||||
|
->setIcon($icon)
|
||||||
|
->setText($button_text)
|
||||||
|
->setSubtext($this->getProviderName());
|
||||||
|
|
||||||
|
$uri = $attributes['uri'];
|
||||||
|
$uri = new PhutilURI($uri);
|
||||||
|
$params = $uri->getQueryParams();
|
||||||
|
$uri->setQueryParams(array());
|
||||||
|
|
||||||
|
$content = array($button);
|
||||||
|
|
||||||
|
foreach ($params as $key => $value) {
|
||||||
|
$content[] = phutil_tag(
|
||||||
|
'input',
|
||||||
|
array(
|
||||||
|
'type' => 'hidden',
|
||||||
|
'name' => $key,
|
||||||
|
'value' => $value,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return phabricator_form(
|
||||||
|
$viewer,
|
||||||
|
array(
|
||||||
|
'method' => idx($attributes, 'method', 'GET'),
|
||||||
|
'action' => (string)$uri,
|
||||||
|
),
|
||||||
|
$content);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,56 +34,17 @@ abstract class PhabricatorAuthProviderOAuth extends PhabricatorAuthProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderLoginForm(AphrontRequest $request, $mode) {
|
protected function renderLoginForm(AphrontRequest $request, $mode) {
|
||||||
$viewer = $request->getUser();
|
|
||||||
|
|
||||||
if ($mode == 'link') {
|
|
||||||
$button_text = pht('Link External Account');
|
|
||||||
} else if ($mode == 'refresh') {
|
|
||||||
$button_text = pht('Refresh Account Link');
|
|
||||||
} else if ($this->shouldAllowRegistration()) {
|
|
||||||
$button_text = pht('Login or Register');
|
|
||||||
} else {
|
|
||||||
$button_text = pht('Login');
|
|
||||||
}
|
|
||||||
|
|
||||||
$icon = id(new PHUIIconView())
|
|
||||||
->setSpriteSheet(PHUIIconView::SPRITE_LOGIN)
|
|
||||||
->setSpriteIcon($this->getLoginIcon());
|
|
||||||
|
|
||||||
$button = id(new PHUIButtonView())
|
|
||||||
->setSize(PHUIButtonView::BIG)
|
|
||||||
->setColor(PHUIButtonView::GREY)
|
|
||||||
->setIcon($icon)
|
|
||||||
->setText($button_text)
|
|
||||||
->setSubtext($this->getProviderName());
|
|
||||||
|
|
||||||
$adapter = $this->getAdapter();
|
$adapter = $this->getAdapter();
|
||||||
$adapter->setState(PhabricatorHash::digest($request->getCookie('phcid')));
|
$adapter->setState(PhabricatorHash::digest($request->getCookie('phcid')));
|
||||||
|
|
||||||
$uri = new PhutilURI($adapter->getAuthenticateURI());
|
$attributes = array(
|
||||||
$params = $uri->getQueryParams();
|
'method' => 'GET',
|
||||||
$uri->setQueryParams(array());
|
'uri' => $adapter->getAuthenticateURI(),
|
||||||
|
);
|
||||||
|
|
||||||
$content = array($button);
|
return $this->renderStandardLoginButton($request, $mode, $attributes);
|
||||||
|
|
||||||
foreach ($params as $key => $value) {
|
|
||||||
$content[] = phutil_tag(
|
|
||||||
'input',
|
|
||||||
array(
|
|
||||||
'type' => 'hidden',
|
|
||||||
'name' => $key,
|
|
||||||
'value' => $value,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
return phabricator_form(
|
|
||||||
$viewer,
|
|
||||||
array(
|
|
||||||
'method' => 'GET',
|
|
||||||
'action' => (string)$uri,
|
|
||||||
),
|
|
||||||
$content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function processLoginRequest(
|
public function processLoginRequest(
|
||||||
PhabricatorAuthLoginController $controller) {
|
PhabricatorAuthLoginController $controller) {
|
||||||
|
|
||||||
|
|
|
@ -39,54 +39,11 @@ abstract class PhabricatorAuthProviderOAuth1 extends PhabricatorAuthProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderLoginForm(AphrontRequest $request, $mode) {
|
protected function renderLoginForm(AphrontRequest $request, $mode) {
|
||||||
$viewer = $request->getUser();
|
$attributes = array(
|
||||||
|
'method' => 'POST',
|
||||||
if ($mode == 'link') {
|
'uri' => $this->getLoginURI(),
|
||||||
$button_text = pht('Link External Account');
|
);
|
||||||
} else if ($mode == 'refresh') {
|
return $this->renderStandardLoginButton($request, $mode, $attributes);
|
||||||
$button_text = pht('Refresh Account Link');
|
|
||||||
} else if ($this->shouldAllowRegistration()) {
|
|
||||||
$button_text = pht('Login or Register');
|
|
||||||
} else {
|
|
||||||
$button_text = pht('Login');
|
|
||||||
}
|
|
||||||
|
|
||||||
$icon = id(new PHUIIconView())
|
|
||||||
->setSpriteSheet(PHUIIconView::SPRITE_LOGIN)
|
|
||||||
->setSpriteIcon($this->getLoginIcon());
|
|
||||||
|
|
||||||
$button = id(new PHUIButtonView())
|
|
||||||
->setSize(PHUIButtonView::BIG)
|
|
||||||
->setColor(PHUIButtonView::GREY)
|
|
||||||
->setIcon($icon)
|
|
||||||
->setText($button_text)
|
|
||||||
->setSubtext($this->getProviderName());
|
|
||||||
|
|
||||||
$adapter = $this->getAdapter();
|
|
||||||
|
|
||||||
$uri = new PhutilURI($this->getLoginURI());
|
|
||||||
$params = $uri->getQueryParams();
|
|
||||||
$uri->setQueryParams(array());
|
|
||||||
|
|
||||||
$content = array($button);
|
|
||||||
|
|
||||||
foreach ($params as $key => $value) {
|
|
||||||
$content[] = phutil_tag(
|
|
||||||
'input',
|
|
||||||
array(
|
|
||||||
'type' => 'hidden',
|
|
||||||
'name' => $key,
|
|
||||||
'value' => $value,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
return phabricator_form(
|
|
||||||
$viewer,
|
|
||||||
array(
|
|
||||||
'method' => 'POST',
|
|
||||||
'action' => (string)$uri,
|
|
||||||
),
|
|
||||||
$content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function processLoginRequest(
|
public function processLoginRequest(
|
||||||
|
|
Loading…
Add table
Reference in a new issue