1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-15 11:22:40 +01:00
phorge-phorge/src/applications/people/controller/PhabricatorPeopleLdapController.php

217 lines
5.9 KiB
PHP
Raw Normal View History

2012-07-04 04:10:38 +02:00
<?php
final class PhabricatorPeopleLdapController
extends PhabricatorPeopleController {
public function handleRequest(AphrontRequest $request) {
$this->requireApplicationCapability(
PeopleCreateUsersCapability::CAPABILITY);
2012-07-04 04:10:38 +02:00
$admin = $request->getUser();
$content = array();
$form = id(new AphrontFormView())
2012-07-10 16:56:38 +02:00
->setAction($request->getRequestURI()
->alter('search', 'true')->alter('import', null))
2012-07-04 04:10:38 +02:00
->setUser($admin)
->appendChild(
id(new AphrontFormTextControl())
->setLabel(pht('LDAP username'))
->setName('username'))
2012-07-04 04:10:38 +02:00
->appendChild(
id(new AphrontFormPasswordControl())
->setDisableAutocomplete(true)
->setLabel(pht('Password'))
->setName('password'))
2012-07-04 04:10:38 +02:00
->appendChild(
id(new AphrontFormTextControl())
->setLabel(pht('LDAP query'))
->setCaption(pht('A filter such as (objectClass=*)'))
->setName('query'))
2012-07-04 04:10:38 +02:00
->appendChild(
id(new AphrontFormSubmitControl())
->setValue(pht('Search')));
2012-07-04 04:10:38 +02:00
$panel = id(new PHUIObjectBoxView())
->setHeaderText(pht('Import LDAP Users'))
->setForm($form);
2012-07-04 04:10:38 +02:00
$crumbs = $this->buildApplicationCrumbs();
$crumbs->addTextCrumb(
pht('Import Ldap Users'),
$this->getApplicationURI('/ldap/'));
$nav = $this->buildSideNavView();
$nav->setCrumbs($crumbs);
$nav->selectFilter('ldap');
$nav->appendChild($content);
2012-07-04 04:10:38 +02:00
2012-07-10 16:56:38 +02:00
if ($request->getStr('import')) {
$nav->appendChild($this->processImportRequest($request));
2012-07-04 04:10:38 +02:00
}
$nav->appendChild($panel);
2012-07-04 04:10:38 +02:00
2012-07-10 16:56:38 +02:00
if ($request->getStr('search')) {
$nav->appendChild($this->processSearchRequest($request));
2012-07-10 16:56:38 +02:00
}
return $this->buildApplicationPage(
$nav,
2012-07-10 16:56:38 +02:00
array(
'title' => pht('Import Ldap Users'),
2012-07-10 16:56:38 +02:00
));
2012-07-04 04:10:38 +02:00
}
private function processImportRequest($request) {
$admin = $request->getUser();
2012-07-10 16:56:38 +02:00
$usernames = $request->getArr('usernames');
$emails = $request->getArr('email');
$names = $request->getArr('name');
$notice_view = new PHUIErrorView();
$notice_view->setSeverity(PHUIErrorView::SEVERITY_NOTICE);
$notice_view->setTitle(pht('Import Successful'));
$notice_view->setErrors(array(
pht('Successfully imported users from LDAP'),
));
2012-07-04 04:10:38 +02:00
$list = new PHUIObjectItemListView();
$list->setNoDataString(pht('No users imported?'));
2012-07-04 04:10:38 +02:00
2012-07-10 16:56:38 +02:00
foreach ($usernames as $username) {
2012-07-04 04:10:38 +02:00
$user = new PhabricatorUser();
$user->setUsername($username);
$user->setRealname($names[$username]);
2012-07-10 16:56:38 +02:00
2012-07-04 04:10:38 +02:00
$email_obj = id(new PhabricatorUserEmail())
->setAddress($emails[$username])
->setIsVerified(1);
try {
id(new PhabricatorUserEditor())
->setActor($admin)
->createNewUser($user, $email_obj);
2012-07-10 16:56:38 +02:00
id(new PhabricatorExternalAccount())
->setUserPHID($user->getPHID())
->setAccountType('ldap')
->setAccountDomain('self')
->setAccountID($username)
->save();
$header = pht('Successfully added %s', $username);
$attribute = null;
$color = 'green';
2012-07-04 04:10:38 +02:00
} catch (Exception $ex) {
$header = pht('Failed to add %s', $username);
$attribute = $ex->getMessage();
$color = 'red';
2012-07-04 04:10:38 +02:00
}
$item = id(new PHUIObjectItemView())
->setHeader($header)
->addAttribute($attribute)
->setBarColor($color);
$list->addItem($item);
2012-07-10 16:56:38 +02:00
}
2012-07-04 04:10:38 +02:00
return array(
$notice_view,
$list,
);
2012-07-04 04:10:38 +02:00
}
private function processSearchRequest($request) {
$panel = new AphrontPanelView();
$admin = $request->getUser();
$search = $request->getStr('query');
2012-07-04 04:10:38 +02:00
$ldap_provider = PhabricatorLDAPAuthProvider::getLDAPProvider();
if (!$ldap_provider) {
throw new Exception('No LDAP provider enabled!');
}
2012-07-04 04:10:38 +02:00
$ldap_adapter = $ldap_provider->getAdapter();
$ldap_adapter->setLoginUsername($request->getStr('username'));
$ldap_adapter->setLoginPassword(
new PhutilOpaqueEnvelope($request->getStr('password')));
// This causes us to connect and bind.
// TODO: Clean up this discard mode stuff.
DarkConsoleErrorLogPluginAPI::enableDiscardMode();
$ldap_adapter->getAccountID();
DarkConsoleErrorLogPluginAPI::disableDiscardMode();
$results = $ldap_adapter->searchLDAP('%Q', $search);
foreach ($results as $key => $record) {
$account_id = $ldap_adapter->readLDAPRecordAccountID($record);
if (!$account_id) {
unset($results[$key]);
continue;
}
$info = array(
$account_id,
$ldap_adapter->readLDAPRecordEmail($record),
$ldap_adapter->readLDAPRecordRealName($record),
);
$results[$key] = $info;
$results[$key][] = $this->renderUserInputs($info);
2012-07-04 04:10:38 +02:00
}
$form = id(new AphrontFormView())
->setUser($admin);
$table = new AphrontTableView($results);
$table->setHeaders(
array(
pht('Username'),
pht('Email'),
pht('Real Name'),
pht('Import?'),
));
$form->appendChild($table);
$form->setAction($request->getRequestURI()
->alter('import', 'true')->alter('search', null))
->appendChild(
id(new AphrontFormSubmitControl())
->setValue(pht('Import')));
$panel->appendChild($form);
return $panel;
2012-07-04 04:10:38 +02:00
}
2012-07-10 16:56:38 +02:00
2012-07-04 04:10:38 +02:00
private function renderUserInputs($user) {
$username = $user[0];
2013-02-13 23:50:15 +01:00
return hsprintf(
'%s%s%s',
phutil_tag(
'input',
array(
'type' => 'checkbox',
'name' => 'usernames[]',
'value' => $username,
)),
phutil_tag(
'input',
array(
'type' => 'hidden',
'name' => "email[$username]",
'value' => $user[1],
)),
phutil_tag(
'input',
array(
'type' => 'hidden',
'name' => "name[$username]",
'value' => $user[2],
)));
2012-07-04 04:10:38 +02:00
}
2012-07-04 04:10:38 +02:00
}