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

199 lines
5.2 KiB
PHP
Raw Normal View History

2012-07-04 04:10:38 +02:00
<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
final class PhabricatorPeopleLdapController
extends PhabricatorPeopleController {
public function shouldRequireAdmin() {
return true;
}
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())
->setLabel('LDAP username')
->setName('username'))
->appendChild(
id(new AphrontFormPasswordControl())
->setLabel('Password')
2012-07-10 16:56:38 +02:00
->setName('password'))
2012-07-04 04:10:38 +02:00
->appendChild(
id(new AphrontFormTextControl())
->setLabel('LDAP query')
2012-07-10 16:56:38 +02:00
->setCaption('A filter such as (objectClass=*)')
2012-07-04 04:10:38 +02:00
->setName('query'))
->appendChild(
id(new AphrontFormSubmitControl())
->setValue('Search'));
$panel = new AphrontPanelView();
$panel->setHeader('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);
}
return $this->buildStandardPageResponse(
$content,
array(
'title' => 'Import Ldap Users',
));
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);
$panel->setTitle("Import Successful");
2012-07-10 16:56:38 +02:00
$errors = array("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();
2012-07-10 16:56:38 +02:00
$errors[] = 'Successfully added ' . $username;
2012-07-04 04:10:38 +02:00
} catch (Exception $ex) {
$errors[] = 'Failed to add ' . $username . ' ' . $ex->getMessage();
}
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();
$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(
'Username',
'Email',
'RealName',
'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())
->setValue('Import'));
$panel->appendChild($form);
} catch (Exception $ex) {
$error_view = new AphrontErrorView();
$error_view->setTitle('LDAP Search Failed');
$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) {
$username = $user[0];
$inputs = phutil_render_tag(
'input',
array(
'type' => 'checkbox',
'name' => 'usernames[]',
'value' =>$username,
),
'');
$inputs .= phutil_render_tag(
'input',
array(
'type' => 'hidden',
'name' => "email[$username]",
'value' =>$user[1],
),
'');
$inputs .= phutil_render_tag(
'input',
array(
'type' => 'hidden',
'name' => "name[$username]",
'value' =>$user[2],
),
'');
2012-07-04 04:10:38 +02:00
return $inputs;
2012-07-04 04:10:38 +02:00
}
2012-07-04 04:10:38 +02:00
}