Skip to content
Snippets Groups Projects
Commit ecab7e5b authored by Tim Steiner's avatar Tim Steiner
Browse files

Added People::fetchNewFromLdap() function, made use of this in various places

parent 73385c1b
Branches
Tags
No related merge requests found
...@@ -46,35 +46,7 @@ class AuthController extends Nmc_Controller_Action ...@@ -46,35 +46,7 @@ class AuthController extends Nmc_Controller_Action
$user = People::getInstance()->findByUserName($userName); $user = People::getInstance()->findByUserName($userName);
if(!$user) { if(!$user) {
$user = People::getInstance()->fetchNew(); $user = People::getInstance()->fetchNewFromLdap($userName);
$filter = 'uid='
. strtr($userName,
array(',' => '', '=' => '', ' ' => ''));
$ldap->bind('uid=' . $userName . ',ou=people,dc=unl,dc=edu', $password);
$userInfo = $ldap->search('ou=people,dc=unl,dc=edu', $filter);
$departmentIn = $userInfo[0]['unlhrprimarydepartment'][0];
$department = Departments::getInstance()->fetchByName($departmentIn);
if(!$department) {
$departments = Departments::getInstance()->fetchAll();
$minDiff = 99999;
$bestMatch = null;
foreach($departments as $row) {
$diff = levenshtein($departmentIn, $row->name);
if($diff < $minDiff) {
$minDiff = $diff;
$bestMatch = $row;
}
}
$department = $bestMatch;
}
$user->userName = $postData['user_name'];
$user->firstName = $userInfo[0]['givenname'][0];
$user->lastName = $userInfo[0]['sn'][0];
$user->email = $userInfo[0]['mail'][0];
$user->phone = $userInfo[0]['telephonenumber'][0];
$user->department = $department->getPrimaryKey();
$user->save(); $user->save();
} }
Nmc_User::getInstance()->login($user); Nmc_User::getInstance()->login($user);
......
...@@ -105,7 +105,6 @@ class UserAdminController extends Nmc_Controller_Action ...@@ -105,7 +105,6 @@ class UserAdminController extends Nmc_Controller_Action
} else { } else {
$out->group = Groups::getInstance()->findOne($groupId); $out->group = Groups::getInstance()->findOne($groupId);
} }
$out->groups = Groups::getInstance()->fetchAllWithoutPrimaries();
$out->users = People::getInstance()->fetchAll(); $out->users = People::getInstance()->fetchAll();
$out->groups = Groups::getInstance()->fetchAllWithoutPrimaries(); $out->groups = Groups::getInstance()->fetchAllWithoutPrimaries();
...@@ -171,6 +170,45 @@ class UserAdminController extends Nmc_Controller_Action ...@@ -171,6 +170,45 @@ class UserAdminController extends Nmc_Controller_Action
$out->refresh = '/UserAdmin/EditGroup/' . $groupId; $out->refresh = '/UserAdmin/EditGroup/' . $groupId;
echo $out->render('unlModernWrapper.xhtml'); echo $out->render('unlModernWrapper.xhtml');
} }
public function importUserAction()
{
$in = $this->getRequest();
$view = new Application_View();
$view->clearSidebarModules();
$view->page = 'user_admin';
$view->users = People::getInstance()->fetchAll();
$view->groups = Groups::getInstance()->fetchAllWithoutPrimaries();
$view->importUser = true;
$out = $this->getResponse();
$out->setBody($view->render('unlModernWrapper.xhtml'));
}
public function importUserPostAction()
{
$in = $this->getRequest();
$userName = $in->getPost('userName');
$person = People::getInstance()->findByUserName($userName);
if (!$person) {
$person = People::getInstance()->fetchNewFromLdap($userName);
if (!$person) {
throw new Nmc_Exception('Error importing user.');
}
$person->save();
}
$view = new Application_View();
$view->refresh = '/UserAdmin/EditUser/' . $person->getPrimaryKey();
$out = $this->getResponse();
$out->setBody($view->render('unlModernWrapper.xhtml'));
}
} }
?>
\ No newline at end of file
...@@ -7,6 +7,13 @@ class People extends Nmc_Db_Table ...@@ -7,6 +7,13 @@ class People extends Nmc_Db_Table
static private $_instance; static private $_instance;
/**
* Singleton LDAP instance for new user lookups
*
* @var Nmc_Ldap
*/
static protected $_ldap;
/** /**
* Return the one true instance * Return the one true instance
* *
...@@ -61,6 +68,57 @@ class People extends Nmc_Db_Table ...@@ -61,6 +68,57 @@ class People extends Nmc_Db_Table
return $this->fetchRow($where); return $this->fetchRow($where);
} }
} }
/**
* Searches the LDAP database for the given username, and creates
* a new Person object with their data. If no user is found,
* null will be returned.
*
* @param string $userName
* @return Person
*/
public function fetchNewFromLdap($userName)
{
if (!self::$_ldap) {
self::$_ldap = new Nmc_Ldap('ldap://localhost:10389');
self::$_ldap->bind('uid=fpatrack,ou=service,dc=unl,dc=edu', 'eziehuoz');
}
$person = parent::fetchNew();
$filter = 'uid='
. strtr($userName,
array(',' => '', '=' => '', ' ' => ''));
$userInfo = self::$_ldap->search('ou=people,dc=unl,dc=edu', $filter);
if (count($userInfo) == 0) {
return null;
}
$departmentIn = $userInfo[0]['unlhrprimarydepartment'][0];
$department = Departments::getInstance()->fetchByName($departmentIn);
if(!$department) {
$departments = Departments::getInstance()->fetchAll();
$minDiff = 99999;
$bestMatch = null;
foreach($departments as $row) {
$diff = levenshtein($departmentIn, $row->name);
if($diff < $minDiff) {
$minDiff = $diff;
$bestMatch = $row;
}
}
$department = $bestMatch;
}
$person->userName = $userName;
$person->firstName = $userInfo[0]['givenname'][0];
$person->lastName = $userInfo[0]['sn'][0];
$person->email = $userInfo[0]['mail'][0];
$person->phone = $userInfo[0]['telephonenumber'][0];
$person->department = $department->getPrimaryKey();
return $person;
}
} }
?>
\ No newline at end of file
...@@ -22,13 +22,15 @@ ...@@ -22,13 +22,15 @@
<?php } ?> <?php } ?>
<div id="admin_list"> <div id="admin_list">
<h2>Groups</h2>
<ul>
<li>
<a href="/UserAdmin/EditGroup/-1"> <a href="/UserAdmin/EditGroup/-1">
<h3>--Create New Group--</h3> <h3>--Create New Group--</h3>
</a> </a>
</li> <a href="/UserAdmin/ImportUser/">
<h3>--Import New User--</h3>
</a>
<h2>Groups</h2>
<ul>
<?php foreach($this->groups as $group) { ?> <?php foreach($this->groups as $group) { ?>
<li> <li>
<?php if($group->isRootGroup()) {list_group($group);} ?> <?php if($group->isRootGroup()) {list_group($group);} ?>
...@@ -37,6 +39,23 @@ ...@@ -37,6 +39,23 @@
</ul> </ul>
</div> </div>
<?php if ($this->importUser) { ?>
<div id="import_user">
<h2>Import User</h2>
<form action="/UserAdmin/ImportUserPost" method="post">
<label>User Name:</label>
<input type="text" name="userName" />
<br />
<input type="submit" value="Import User" />
</form>
</div>
<?php } ?>
<?php if($this->user) { ?> <?php if($this->user) { ?>
<div id="edit_user" class="edit_pane"> <div id="edit_user" class="edit_pane">
<h2>Editing User: <?php echo $this->user->userName; ?></h2> <h2>Editing User: <?php echo $this->user->userName; ?></h2>
...@@ -59,6 +78,11 @@ ...@@ -59,6 +78,11 @@
</div> </div>
<?php } ?> <?php } ?>
<?php if($this->group) { ?> <?php if($this->group) { ?>
<div id="edit_group" class="edit_pane"> <div id="edit_group" class="edit_pane">
<h2>Editing Group: <?php echo $this->group->name; ?></h2> <h2>Editing Group: <?php echo $this->group->name; ?></h2>
......
...@@ -41,3 +41,7 @@ div.edit_pane h2 { ...@@ -41,3 +41,7 @@ div.edit_pane h2 {
#maincontent fieldset { #maincontent fieldset {
border-width: 0px border-width: 0px
} }
div#import_user {
overflow: hidden;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment