2012-07-04 04:10:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorPeopleLdapController
|
|
|
|
extends PhabricatorPeopleController {
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-04-02 17:58:52 +02:00
|
|
|
$panel = id(new AphrontPanelView())
|
|
|
|
->setHeader(pht('Import LDAP Users'))
|
|
|
|
->setNoBackground()
|
|
|
|
->setWidth(AphrontPanelView::WIDTH_FORM)
|
|
|
|
->appendChild($form);
|
2012-07-04 04:10:38 +02:00
|
|
|
|
2013-04-02 17:58:52 +02:00
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
|
|
|
$crumbs->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())
|
|
|
|
->setName(pht('Import Ldap Users'))
|
|
|
|
->setHref($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')) {
|
2013-04-02 17:58:52 +02:00
|
|
|
$nav->appendChild($this->processImportRequest($request));
|
2012-07-04 04:10:38 +02:00
|
|
|
}
|
|
|
|
|
2013-04-02 17:58:52 +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')) {
|
2013-04-02 17:58:52 +02:00
|
|
|
$nav->appendChild($this->processSearchRequest($request));
|
2012-07-10 16:56:38 +02:00
|
|
|
}
|
|
|
|
|
2012-08-14 00:27:21 +02:00
|
|
|
return $this->buildApplicationPage(
|
|
|
|
$nav,
|
2012-07-10 16:56:38 +02:00
|
|
|
array(
|
2013-04-02 17:58:52 +02:00
|
|
|
'title' => pht('Import Ldap Users'),
|
2013-02-21 23:10:22 +01:00
|
|
|
'device' => true,
|
2013-04-02 17:58:52 +02:00
|
|
|
'dust' => 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');
|
|
|
|
|
2013-04-02 17:58:52 +02:00
|
|
|
$notice_view = new AphrontErrorView();
|
|
|
|
$notice_view->setSeverity(AphrontErrorView::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
|
|
|
|
2013-04-02 17:58:52 +02:00
|
|
|
$list = new PhabricatorObjectItemListView();
|
|
|
|
$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
|
|
|
|
2013-06-16 18:55:55 +02:00
|
|
|
id(new PhabricatorExternalAccount())
|
|
|
|
->setUserPHID($user->getPHID())
|
|
|
|
->setAccountType('ldap')
|
|
|
|
->setAccountDomain('self')
|
|
|
|
->setAccountID($username)
|
|
|
|
->save();
|
2013-04-02 17:58:52 +02:00
|
|
|
|
|
|
|
$header = pht('Successfully added %s', $username);
|
|
|
|
$attribute = null;
|
|
|
|
$color = 'green';
|
2012-07-04 04:10:38 +02:00
|
|
|
} catch (Exception $ex) {
|
2013-04-02 17:58:52 +02:00
|
|
|
$header = pht('Failed to add %s', $username);
|
|
|
|
$attribute = $ex->getMessage();
|
|
|
|
$color = 'red';
|
2012-07-04 04:10:38 +02:00
|
|
|
}
|
2013-04-02 17:58:52 +02:00
|
|
|
|
|
|
|
$item = id(new PhabricatorObjectItemView())
|
|
|
|
->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
|
|
|
|
2013-04-02 17:58:52 +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();
|
|
|
|
|
|
|
|
$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
|
|
|
}
|