2012-06-13 17:52:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorLDAPLoginController extends PhabricatorAuthController {
|
|
|
|
private $provider;
|
|
|
|
|
|
|
|
public function shouldRequireLogin() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->provider = new PhabricatorLDAPProvider();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
if (!$this->provider->isProviderEnabled()) {
|
|
|
|
return new Aphront400Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
$current_user = $this->getRequest()->getUser();
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
2012-07-17 23:05:26 +02:00
|
|
|
$ldap_username = $request->getCookie('phusr');
|
2012-06-13 17:52:05 +02:00
|
|
|
if ($request->isFormPost()) {
|
2012-07-17 23:05:26 +02:00
|
|
|
$ldap_username = $request->getStr('username');
|
2012-06-13 17:52:05 +02:00
|
|
|
try {
|
2012-07-17 21:06:33 +02:00
|
|
|
$envelope = new PhutilOpaqueEnvelope($request->getStr('password'));
|
2012-07-17 23:05:26 +02:00
|
|
|
$this->provider->auth($ldap_username, $envelope);
|
2012-06-13 17:52:05 +02:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$errors[] = $e->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($errors)) {
|
|
|
|
$ldap_info = $this->retrieveLDAPInfo($this->provider);
|
|
|
|
|
|
|
|
if ($current_user->getPHID()) {
|
|
|
|
if ($ldap_info->getID()) {
|
|
|
|
$existing_ldap = id(new PhabricatorUserLDAPInfo())->loadOneWhere(
|
|
|
|
'userID = %d',
|
|
|
|
$current_user->getID());
|
|
|
|
|
|
|
|
if ($ldap_info->getUserID() != $current_user->getID() ||
|
|
|
|
$existing_ldap) {
|
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($current_user);
|
2013-01-27 01:17:44 +01:00
|
|
|
$dialog->setTitle(pht('Already Linked to Another Account'));
|
2013-02-09 19:31:09 +01:00
|
|
|
$dialog->appendChild(phutil_tag('p', array(), pht(
|
|
|
|
'The LDAP account you just authorized is already '.
|
2013-01-27 01:17:44 +01:00
|
|
|
'linked toanother Phabricator account. Before you can link it '.
|
|
|
|
'to a different LDAP account, you must unlink the old '.
|
2013-02-09 19:31:09 +01:00
|
|
|
'account.')));
|
2012-08-13 21:37:26 +02:00
|
|
|
$dialog->addCancelButton('/settings/panel/ldap/');
|
2012-06-13 17:52:05 +02:00
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
} else {
|
|
|
|
return id(new AphrontRedirectResponse())
|
2012-08-13 21:37:26 +02:00
|
|
|
->setURI('/settings/panel/ldap/');
|
2012-06-13 17:52:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$request->isDialogFormPost()) {
|
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($current_user);
|
2013-01-27 01:17:44 +01:00
|
|
|
$dialog->setTitle(pht('Link LDAP Account'));
|
2013-02-09 19:31:09 +01:00
|
|
|
$dialog->appendChild(phutil_tag('p', array(), pht(
|
|
|
|
'Link your LDAP account to your Phabricator account?')));
|
2012-06-13 17:52:05 +02:00
|
|
|
$dialog->addHiddenInput('username', $request->getStr('username'));
|
|
|
|
$dialog->addHiddenInput('password', $request->getStr('password'));
|
2013-01-27 01:17:44 +01:00
|
|
|
$dialog->addSubmitButton(pht('Link Accounts'));
|
2012-08-13 21:37:26 +02:00
|
|
|
$dialog->addCancelButton('/settings/panel/ldap/');
|
2012-06-13 17:52:05 +02:00
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
$ldap_info->setUserID($current_user->getID());
|
|
|
|
|
|
|
|
$this->saveLDAPInfo($ldap_info);
|
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())
|
2012-08-13 21:37:26 +02:00
|
|
|
->setURI('/settings/panel/ldap/');
|
2012-06-13 17:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($ldap_info->getID()) {
|
|
|
|
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
|
|
|
|
|
|
|
|
$known_user = id(new PhabricatorUser())->load(
|
|
|
|
$ldap_info->getUserID());
|
|
|
|
|
|
|
|
$session_key = $known_user->establishSession('web');
|
|
|
|
|
|
|
|
$this->saveLDAPInfo($ldap_info);
|
|
|
|
|
|
|
|
$request->setCookie('phusr', $known_user->getUsername());
|
|
|
|
$request->setCookie('phsid', $session_key);
|
|
|
|
|
|
|
|
$uri = new PhutilURI('/login/validate/');
|
|
|
|
$uri->setQueryParams(
|
|
|
|
array(
|
|
|
|
'phusr' => $known_user->getUsername(),
|
|
|
|
));
|
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())->setURI((string)$uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
$controller = newv('PhabricatorLDAPRegistrationController',
|
|
|
|
array($this->getRequest()));
|
|
|
|
$controller->setLDAPProvider($this->provider);
|
|
|
|
$controller->setLDAPInfo($ldap_info);
|
|
|
|
|
|
|
|
return $this->delegateToController($controller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$ldap_form = new AphrontFormView();
|
|
|
|
$ldap_form
|
|
|
|
->setUser($request->getUser())
|
|
|
|
->setAction('/ldap/login/')
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2013-01-27 01:17:44 +01:00
|
|
|
->setLabel(pht('LDAP username'))
|
2012-06-13 17:52:05 +02:00
|
|
|
->setName('username')
|
|
|
|
->setValue($ldap_username))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormPasswordControl())
|
2013-01-27 01:17:44 +01:00
|
|
|
->setLabel(pht('Password'))
|
2012-06-13 17:52:05 +02:00
|
|
|
->setName('password'));
|
|
|
|
|
|
|
|
$ldap_form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2013-01-27 01:17:44 +01:00
|
|
|
->setValue(pht('Login')));
|
2012-06-13 17:52:05 +02:00
|
|
|
|
|
|
|
$panel = new AphrontPanelView();
|
|
|
|
$panel->setWidth(AphrontPanelView::WIDTH_FORM);
|
2013-01-27 01:17:44 +01:00
|
|
|
$panel->appendChild('<h1>'.pht('LDAP login').'</h1>');
|
2012-06-13 17:52:05 +02:00
|
|
|
$panel->appendChild($ldap_form);
|
|
|
|
|
|
|
|
if (isset($errors) && count($errors) > 0) {
|
|
|
|
$error_view = new AphrontErrorView();
|
2013-01-27 01:17:44 +01:00
|
|
|
$error_view->setTitle(pht('Login Failed'));
|
2012-06-13 17:52:05 +02:00
|
|
|
$error_view->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->buildStandardPageResponse(
|
|
|
|
array(
|
|
|
|
isset($error_view) ? $error_view : null,
|
|
|
|
$panel,
|
|
|
|
),
|
|
|
|
array(
|
2013-01-27 01:17:44 +01:00
|
|
|
'title' => pht('Login'),
|
2012-06-13 17:52:05 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function retrieveLDAPInfo(PhabricatorLDAPProvider $provider) {
|
|
|
|
$ldap_info = id(new PhabricatorUserLDAPInfo())->loadOneWhere(
|
|
|
|
'ldapUsername = %s',
|
|
|
|
$provider->retrieveUsername());
|
|
|
|
|
|
|
|
if (!$ldap_info) {
|
|
|
|
$ldap_info = new PhabricatorUserLDAPInfo();
|
|
|
|
$ldap_info->setLDAPUsername($provider->retrieveUsername());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ldap_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function saveLDAPInfo(PhabricatorUserLDAPInfo $info) {
|
|
|
|
// UNGUARDED WRITES: Logging-in users don't have their CSRF set up yet.
|
|
|
|
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
|
|
|
|
$info->save();
|
|
|
|
}
|
|
|
|
}
|