diff --git a/application/controllers/AuthController.php b/application/controllers/AuthController.php index fef7b333119980e393ec37090e2e15c3d366c0ed..f2377858753f878b704a7461c943f5e4118edc37 100644 --- a/application/controllers/AuthController.php +++ b/application/controllers/AuthController.php @@ -46,35 +46,7 @@ class AuthController extends Nmc_Controller_Action $user = People::getInstance()->findByUserName($userName); if(!$user) { - $user = People::getInstance()->fetchNew(); - $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 = People::getInstance()->fetchNewFromLdap($userName); $user->save(); } Nmc_User::getInstance()->login($user); diff --git a/application/controllers/UseradminController.php b/application/controllers/UseradminController.php index 681745d40594bc6faa97d8fea2a1d3325de3a29e..5873dee72c8a5df7f7f84ec4f134bf8e3cee9740 100644 --- a/application/controllers/UseradminController.php +++ b/application/controllers/UseradminController.php @@ -105,7 +105,6 @@ class UserAdminController extends Nmc_Controller_Action } else { $out->group = Groups::getInstance()->findOne($groupId); } - $out->groups = Groups::getInstance()->fetchAllWithoutPrimaries(); $out->users = People::getInstance()->fetchAll(); $out->groups = Groups::getInstance()->fetchAllWithoutPrimaries(); @@ -171,6 +170,45 @@ class UserAdminController extends Nmc_Controller_Action $out->refresh = '/UserAdmin/EditGroup/' . $groupId; 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 diff --git a/application/models/tables/People.php b/application/models/tables/People.php index a8d4e47897fee5bc851f63f83723de9bdeed41a7..0f2cc22b08b5985116f908072f866c9f08567c77 100644 --- a/application/models/tables/People.php +++ b/application/models/tables/People.php @@ -7,6 +7,13 @@ class People extends Nmc_Db_Table static private $_instance; + /** + * Singleton LDAP instance for new user lookups + * + * @var Nmc_Ldap + */ + static protected $_ldap; + /** * Return the one true instance * @@ -61,6 +68,57 @@ class People extends Nmc_Db_Table 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 diff --git a/application/views/user_admin.xhtml b/application/views/user_admin.xhtml index 8c8bb675ad37fd43b20bd85d6bfc13f8b24207c3..290d1f8317d08533e04a290610e79f6f4e600f1d 100644 --- a/application/views/user_admin.xhtml +++ b/application/views/user_admin.xhtml @@ -22,13 +22,15 @@ <?php } ?> <div id="admin_list"> + + <a href="/UserAdmin/EditGroup/-1"> + <h3>--Create New Group--</h3> + </a> + <a href="/UserAdmin/ImportUser/"> + <h3>--Import New User--</h3> + </a> <h2>Groups</h2> <ul> - <li> - <a href="/UserAdmin/EditGroup/-1"> - <h3>--Create New Group--</h3> - </a> - </li> <?php foreach($this->groups as $group) { ?> <li> <?php if($group->isRootGroup()) {list_group($group);} ?> @@ -37,6 +39,23 @@ </ul> </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) { ?> <div id="edit_user" class="edit_pane"> <h2>Editing User: <?php echo $this->user->userName; ?></h2> @@ -59,6 +78,11 @@ </div> <?php } ?> + + + + + <?php if($this->group) { ?> <div id="edit_group" class="edit_pane"> <h2>Editing Group: <?php echo $this->group->name; ?></h2> diff --git a/document_root/css/user_admin.css b/document_root/css/user_admin.css index 23090d52f97c312267b0c25bf7b9ab2badcae931..c06a1704f54466cc509f3b956046ebf99afe2cb9 100644 --- a/document_root/css/user_admin.css +++ b/document_root/css/user_admin.css @@ -40,4 +40,8 @@ div.edit_pane h2 { #maincontent fieldset { border-width: 0px +} + +div#import_user { + overflow: hidden; } \ No newline at end of file