mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-23 15:22:41 +01:00
Refactor methods, fix style issues
This commit is contained in:
parent
51d2d06e37
commit
42dfee38e7
2 changed files with 40 additions and 67 deletions
|
@ -25,47 +25,16 @@ final class PhabricatorPeopleLdapController
|
||||||
|
|
||||||
private $view;
|
private $view;
|
||||||
|
|
||||||
public function willProcessRequest(array $data) {
|
|
||||||
$this->view = idx($data, 'view');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function processRequest() {
|
public function processRequest() {
|
||||||
|
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
$admin = $request->getUser();
|
$admin = $request->getUser();
|
||||||
|
|
||||||
$base_uri = '/people/edit/';
|
|
||||||
|
|
||||||
$content = array();
|
$content = array();
|
||||||
|
|
||||||
|
|
||||||
$response = $this->processBasicRequest();
|
|
||||||
|
|
||||||
if ($response instanceof AphrontResponse) {
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
|
|
||||||
$content[] = $response;
|
|
||||||
|
|
||||||
|
|
||||||
return $this->buildStandardPageResponse(
|
|
||||||
$content,
|
|
||||||
array(
|
|
||||||
'title' => 'Import Ldap Users',
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays a ldap login form, as we need to auth before we can search
|
|
||||||
*/
|
|
||||||
private function processBasicRequest() {
|
|
||||||
$panels = array();
|
|
||||||
|
|
||||||
$request = $this->getRequest();
|
|
||||||
|
|
||||||
$admin = $request->getUser();
|
|
||||||
|
|
||||||
$form = id(new AphrontFormView())
|
$form = id(new AphrontFormView())
|
||||||
|
->setAction($request->getRequestURI()
|
||||||
|
->alter('search', 'true')->alter('import', null))
|
||||||
->setUser($admin)
|
->setUser($admin)
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new AphrontFormTextControl())
|
id(new AphrontFormTextControl())
|
||||||
|
@ -74,12 +43,12 @@ final class PhabricatorPeopleLdapController
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new AphrontFormPasswordControl())
|
id(new AphrontFormPasswordControl())
|
||||||
->setLabel('Password')
|
->setLabel('Password')
|
||||||
->setName('password'))
|
->setName('password'))
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new AphrontFormTextControl())
|
id(new AphrontFormTextControl())
|
||||||
->setLabel('LDAP query')
|
->setLabel('LDAP query')
|
||||||
|
->setCaption('A filter such as (objectClass=*)')
|
||||||
->setName('query'))
|
->setName('query'))
|
||||||
->setAction($request->getRequestURI()->alter('search', 'true')->alter('import', null))
|
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new AphrontFormSubmitControl())
|
id(new AphrontFormSubmitControl())
|
||||||
->setValue('Search'));
|
->setValue('Search'));
|
||||||
|
@ -88,38 +57,41 @@ final class PhabricatorPeopleLdapController
|
||||||
$panel->setHeader('Import Ldap Users');
|
$panel->setHeader('Import Ldap Users');
|
||||||
$panel->appendChild($form);
|
$panel->appendChild($form);
|
||||||
|
|
||||||
|
|
||||||
if($request->getStr('import')) {
|
|
||||||
$panels[] = $this->processImportRequest($request);
|
|
||||||
}
|
|
||||||
|
|
||||||
$panels[] = $panel;
|
|
||||||
|
|
||||||
if($request->getStr('search')) {
|
if ($request->getStr('import')) {
|
||||||
$panels[] = $this->processSearchRequest($request);
|
$content[] = $this->processImportRequest($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $panels;
|
$content[] = $panel;
|
||||||
|
|
||||||
|
if ($request->getStr('search')) {
|
||||||
|
$content[] = $this->processSearchRequest($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->buildStandardPageResponse(
|
||||||
|
$content,
|
||||||
|
array(
|
||||||
|
'title' => 'Import Ldap Users',
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function processImportRequest($request) {
|
private function processImportRequest($request) {
|
||||||
$admin = $request->getUser();
|
$admin = $request->getUser();
|
||||||
$usernames = $request->getArr('usernames');
|
$usernames = $request->getArr('usernames');
|
||||||
$emails = $request->getArr('email');
|
$emails = $request->getArr('email');
|
||||||
$names = $request->getArr('name');
|
$names = $request->getArr('name');
|
||||||
|
|
||||||
$panel = new AphrontErrorView();
|
$panel = new AphrontErrorView();
|
||||||
$panel->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
|
$panel->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
|
||||||
$panel->setTitle("Import Successful");
|
$panel->setTitle("Import Successful");
|
||||||
$errors = array("Successfully imported users from ldap");
|
$errors = array("Successfully imported users from LDAP");
|
||||||
|
|
||||||
|
|
||||||
foreach($usernames as $username) {
|
foreach ($usernames as $username) {
|
||||||
$user = new PhabricatorUser();
|
$user = new PhabricatorUser();
|
||||||
$user->setUsername($username);
|
$user->setUsername($username);
|
||||||
$user->setRealname($names[$username]);
|
$user->setRealname($names[$username]);
|
||||||
|
|
||||||
$email_obj = id(new PhabricatorUserEmail())
|
$email_obj = id(new PhabricatorUserEmail())
|
||||||
->setAddress($emails[$username])
|
->setAddress($emails[$username])
|
||||||
->setIsVerified(1);
|
->setIsVerified(1);
|
||||||
|
@ -127,19 +99,19 @@ final class PhabricatorPeopleLdapController
|
||||||
id(new PhabricatorUserEditor())
|
id(new PhabricatorUserEditor())
|
||||||
->setActor($admin)
|
->setActor($admin)
|
||||||
->createNewUser($user, $email_obj);
|
->createNewUser($user, $email_obj);
|
||||||
|
|
||||||
$ldap_info = new PhabricatorUserLDAPInfo();
|
$ldap_info = new PhabricatorUserLDAPInfo();
|
||||||
$ldap_info->setLDAPUsername($username);
|
$ldap_info->setLDAPUsername($username);
|
||||||
$ldap_info->setUserID($user->getID());
|
$ldap_info->setUserID($user->getID());
|
||||||
$ldap_info->save();
|
$ldap_info->save();
|
||||||
$errors[] = 'Succesfully added ' . $username;
|
$errors[] = 'Successfully added ' . $username;
|
||||||
} catch (Exception $ex) {
|
} catch (Exception $ex) {
|
||||||
$errors[] = 'Failed to add ' . $username . ' ' . $ex->getMessage();
|
$errors[] = 'Failed to add ' . $username . ' ' . $ex->getMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$panel->setErrors($errors);
|
$panel->setErrors($errors);
|
||||||
return $panel;
|
return $panel;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,10 +125,10 @@ final class PhabricatorPeopleLdapController
|
||||||
$search = $request->getStr('query');
|
$search = $request->getStr('query');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$ldapProvider = new PhabricatorLDAPProvider();
|
$ldap_provider = new PhabricatorLDAPProvider();
|
||||||
$ldapProvider->auth($username, $password);
|
$ldap_provider->auth($username, $password);
|
||||||
$results = $ldapProvider->search($search);
|
$results = $ldap_provider->search($search);
|
||||||
foreach($results as $key => $result) {
|
foreach ($results as $key => $result) {
|
||||||
$results[$key][] = $this->renderUserInputs($result);
|
$results[$key][] = $this->renderUserInputs($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,7 +144,8 @@ final class PhabricatorPeopleLdapController
|
||||||
'',
|
'',
|
||||||
));
|
));
|
||||||
$form->appendChild($table);
|
$form->appendChild($table);
|
||||||
$form->setAction($request->getRequestURI()->alter('import', 'true')->alter('search', null))
|
$form->setAction($request->getRequestURI()
|
||||||
|
->alter('import', 'true')->alter('search', null))
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new AphrontFormSubmitControl())
|
id(new AphrontFormSubmitControl())
|
||||||
->setValue('Import'));
|
->setValue('Import'));
|
||||||
|
@ -188,15 +161,15 @@ final class PhabricatorPeopleLdapController
|
||||||
return $panel;
|
return $panel;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function renderUserInputs($user) {
|
private function renderUserInputs($user) {
|
||||||
$username = $user[0];
|
$username = $user[0];
|
||||||
$inputs = phutil_render_tag(
|
$inputs = phutil_render_tag(
|
||||||
'input',
|
'input',
|
||||||
array(
|
array(
|
||||||
'type' => 'checkbox',
|
'type' => 'checkbox',
|
||||||
'name' => 'usernames[]',
|
'name' => 'usernames[]',
|
||||||
'value' =>$username,
|
'value' =>$username,
|
||||||
),
|
),
|
||||||
'');
|
'');
|
||||||
|
|
||||||
|
@ -205,16 +178,16 @@ final class PhabricatorPeopleLdapController
|
||||||
array(
|
array(
|
||||||
'type' => 'hidden',
|
'type' => 'hidden',
|
||||||
'name' => "email[$username]",
|
'name' => "email[$username]",
|
||||||
'value' =>$user[1],
|
'value' =>$user[1],
|
||||||
),
|
),
|
||||||
'');
|
'');
|
||||||
|
|
||||||
$inputs .= phutil_render_tag(
|
$inputs .= phutil_render_tag(
|
||||||
'input',
|
'input',
|
||||||
array(
|
array(
|
||||||
'type' => 'hidden',
|
'type' => 'hidden',
|
||||||
'name' => "name[$username]",
|
'name' => "name[$username]",
|
||||||
'value' =>$user[2],
|
'value' =>$user[2],
|
||||||
),
|
),
|
||||||
'');
|
'');
|
||||||
|
|
||||||
|
|
|
@ -135,7 +135,7 @@ final class PhabricatorPeopleListController
|
||||||
phutil_render_tag(
|
phutil_render_tag(
|
||||||
'a',
|
'a',
|
||||||
array(
|
array(
|
||||||
'href' => '/people/ldap',
|
'href' => '/people/ldap/',
|
||||||
'class' => 'button green'
|
'class' => 'button green'
|
||||||
),
|
),
|
||||||
'Import from Ldap'));
|
'Import from Ldap'));
|
||||||
|
|
Loading…
Reference in a new issue