2012-07-04 04:10:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorPeopleLdapController
|
|
|
|
extends PhabricatorPeopleController {
|
|
|
|
|
|
|
|
private $view;
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$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())
|
2013-02-21 23:10:22 +01:00
|
|
|
->setLabel(pht('LDAP username'))
|
2012-07-04 04:10:38 +02:00
|
|
|
->setName('username'))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormPasswordControl())
|
2013-02-21 23:10:22 +01:00
|
|
|
->setLabel(pht('Password'))
|
2012-07-10 16:56:38 +02:00
|
|
|
->setName('password'))
|
2012-07-04 04:10:38 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2013-02-21 23:10:22 +01:00
|
|
|
->setLabel(pht('LDAP query'))
|
|
|
|
->setCaption(pht('A filter such as (objectClass=*)'))
|
2012-07-04 04:10:38 +02:00
|
|
|
->setName('query'))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2013-02-21 23:10:22 +01:00
|
|
|
->setValue(pht('Search')));
|
2012-07-04 04:10:38 +02:00
|
|
|
|
|
|
|
$panel = new AphrontPanelView();
|
2013-02-21 23:10:22 +01:00
|
|
|
$panel->setHeader(pht('Import LDAP Users'));
|
2012-07-04 04:10:38 +02:00
|
|
|
$panel->appendChild($form);
|
|
|
|
|
|
|
|
|
2012-07-10 16:56:38 +02:00
|
|
|
if ($request->getStr('import')) {
|
|
|
|
$content[] = $this->processImportRequest($request);
|
2012-07-04 04:10:38 +02:00
|
|
|
}
|
|
|
|
|
2012-07-10 16:56:38 +02:00
|
|
|
$content[] = $panel;
|
2012-07-04 04:10:38 +02:00
|
|
|
|
2012-07-10 16:56:38 +02:00
|
|
|
if ($request->getStr('search')) {
|
|
|
|
$content[] = $this->processSearchRequest($request);
|
|
|
|
}
|
|
|
|
|
2012-08-14 00:27:21 +02:00
|
|
|
$nav = $this->buildSideNavView();
|
|
|
|
$nav->selectFilter('ldap');
|
|
|
|
$nav->appendChild($content);
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
|
|
|
$nav,
|
2012-07-10 16:56:38 +02:00
|
|
|
array(
|
2013-02-21 23:10:22 +01:00
|
|
|
'title' => pht('Import Ldap Users'),
|
|
|
|
'device' => true,
|
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');
|
|
|
|
|
2012-07-04 04:10:38 +02:00
|
|
|
$panel = new AphrontErrorView();
|
|
|
|
$panel->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
|
2013-02-21 23:10:22 +01:00
|
|
|
$panel->setTitle(pht("Import Successful"));
|
|
|
|
$errors = array(pht("Successfully imported users from LDAP"));
|
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
|
|
|
|
2012-07-04 04:10:38 +02:00
|
|
|
$ldap_info = new PhabricatorUserLDAPInfo();
|
|
|
|
$ldap_info->setLDAPUsername($username);
|
|
|
|
$ldap_info->setUserID($user->getID());
|
|
|
|
$ldap_info->save();
|
2013-02-21 23:10:22 +01:00
|
|
|
$errors[] = pht('Successfully added %s', $username);
|
2012-07-04 04:10:38 +02:00
|
|
|
} catch (Exception $ex) {
|
2013-02-21 23:10:22 +01:00
|
|
|
$errors[] = pht('Failed to add %s %s', $username, $ex->getMessage());
|
2012-07-04 04:10:38 +02:00
|
|
|
}
|
2012-07-10 16:56:38 +02:00
|
|
|
}
|
2012-07-04 04:10:38 +02:00
|
|
|
|
|
|
|
$panel->setErrors($errors);
|
2012-07-10 16:56:38 +02:00
|
|
|
return $panel;
|
2012-07-04 04:10:38 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processSearchRequest($request) {
|
|
|
|
$panel = new AphrontPanelView();
|
|
|
|
|
|
|
|
$admin = $request->getUser();
|
|
|
|
|
|
|
|
$username = $request->getStr('username');
|
|
|
|
$password = $request->getStr('password');
|
|
|
|
$search = $request->getStr('query');
|
|
|
|
|
|
|
|
try {
|
2012-07-10 16:56:38 +02:00
|
|
|
$ldap_provider = new PhabricatorLDAPProvider();
|
2012-07-17 23:05:26 +02:00
|
|
|
$envelope = new PhutilOpaqueEnvelope($password);
|
|
|
|
$ldap_provider->auth($username, $envelope);
|
2012-07-10 16:56:38 +02:00
|
|
|
$results = $ldap_provider->search($search);
|
|
|
|
foreach ($results as $key => $result) {
|
2012-07-04 04:10:38 +02:00
|
|
|
$results[$key][] = $this->renderUserInputs($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($admin);
|
|
|
|
|
|
|
|
$table = new AphrontTableView($results);
|
|
|
|
$table->setHeaders(
|
|
|
|
array(
|
2013-02-21 23:10:22 +01:00
|
|
|
pht('Username'),
|
|
|
|
pht('Email'),
|
|
|
|
pht('Real Name'),
|
|
|
|
pht('Import?'),
|
2012-07-04 04:10:38 +02:00
|
|
|
));
|
|
|
|
$form->appendChild($table);
|
2012-07-10 16:56:38 +02:00
|
|
|
$form->setAction($request->getRequestURI()
|
|
|
|
->alter('import', 'true')->alter('search', null))
|
2012-07-04 04:10:38 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2013-02-21 23:10:22 +01:00
|
|
|
->setValue(pht('Import')));
|
2012-07-04 04:10:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
$panel->appendChild($form);
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$error_view = new AphrontErrorView();
|
2013-02-21 23:10:22 +01:00
|
|
|
$error_view->setTitle(pht('LDAP Search Failed'));
|
2012-07-04 04:10:38 +02:00
|
|
|
$error_view->setErrors(array($ex->getMessage()));
|
|
|
|
return $error_view;
|
|
|
|
}
|
|
|
|
return $panel;
|
|
|
|
|
|
|
|
}
|
2012-07-10 16:56:38 +02:00
|
|
|
|
2012-07-04 04:10:38 +02:00
|
|
|
private function renderUserInputs($user) {
|
2012-07-17 23:05:26 +02:00
|
|
|
$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-17 23:05:26 +02:00
|
|
|
|
2012-07-04 04:10:38 +02:00
|
|
|
}
|