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

When a user logs in for the first time, import their data from LDAP.

parent fb97c30d
No related branches found
No related tags found
No related merge requests found
......@@ -53,6 +53,12 @@ class Auth_IndexController extends App_Controller_Action {
return;
}
$user = Auth_UserModel::findCurrentUser();
if (!$user) {
$user = Auth_UserModel::fetchNewFromLdap($username);
print_r($user); exit;
}
$this->_redirect('/');
}
......
......@@ -2,6 +2,13 @@
class Auth_UserModel extends Unl_Model {
/**
* Singleton LDAP instance for new user lookups
*
* @var Unl_Ldap
*/
static protected $_ldap;
public static function find($id)
{
$db = Zend_Registry::get('db');
......@@ -75,7 +82,7 @@ class Auth_UserModel extends Unl_Model {
}
public static function findCurrentUser()
static public static function findCurrentUser()
{
$username = Zend_Auth::getInstance()->getIdentity();
if (!$username) {
......@@ -85,29 +92,298 @@ class Auth_UserModel extends Unl_Model {
return self::findByUsername($username);
}
public function fetchNew()
{
$data = array(
'userId' => null,
'userName' => null,
'primaryGroup' => null,
'firstName' => null,
'middleName' => null,
'lastName' => null,
'email' => null,
'phone' => null,
'department' => null
);
$new = new self($data);
return $new;
}
/**
* 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 Unl_Ldap('ldap://localhost:10389');
self::$_ldap->bind('uid=fpatrack,ou=service,dc=unl,dc=edu', 'eziehuoz');
}
$person = self::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->setUserName($userName);
$person->setFirstName($userInfo[0]['givenname'][0]);
$person->setLastName($userInfo[0]['sn'][0]);
$person->setEmail($userInfo[0]['mail'][0]);
$person->setPhone($userInfo[0]['telephonenumber'][0]);
//$person->setDepartment($department->getPrimaryKey());
self::save($person);
return $person;
}
static public function save($models)
{
if (!Unl_Util::isArray($models)) {
$collection = new Unl_Model_Collection(__CLASS__);
$collection[] = $models;
$models = $collection;
}
$db = Zend_Registry::get('db');
$db->beginTransaction();
$unsavedData = array();
foreach ($models as $id => $model) {
$unsavedData[$id] = $model->_data;
}
try {
$modelsToInsert = new Unl_Model_Collection(__CLASS__);
$modelsToUpdate = new Unl_Model_Collection(__CLASS__);
foreach ($models as $model) {
if ($model->getId()) {
$modelsToUpdate[] = $model;
} else {
$modelsToInsert[] = $model;
}
}
self::_insert($modelsToInsert);
self::_update($modelsToUpdate);
$db->commit();
} catch (Exception $e) {
$db->rollback();
foreach ($models as $id => $model) {
$model->_data = $unsavedData[$id];
}
throw $e;
}
}
static protected function _update(Unl_Model_Collection $models)
{
if (count($models) == 0) {
return;
}
$db = Zend_Registry::get('db');
$sql = 'CREATE TEMPORARY TABLE creqUsersUpdate '
. 'SELECT * FROM creqUsers LIMIT 0';
$db->query($sql);
$sql = 'INSERT INTO creqUsersUpdate VALUES ';
$sqlParts = array();
foreach ($models as $model) {
$sqlParts[] = $db->quoteInto('(?, ', $model->_data['userId'])
. $db->quoteInto('?, ' , $model->_data['userName'])
. $db->quoteInto('?)' , $model->_data['primaryGroup']);
}
$sql .= implode(', ', $sqlParts);
$db->query($sql);
$sql = 'UPDATE creqUsers AS a, '
. ' creqUsersUpdate AS b '
. 'SET a.userName = b.userName, '
. ' a.primaryGroup = b.primaryGroup '
. 'WHERE a.userId = b.userId ';
$db->query($sql);
$db->query('DROP TABLE creqUsersUpdate');
$sql = 'CREATE TEMPORARY TABLE creqUsersUpdate '
. 'SELECT * FROM creqUsers LIMIT 0';
$db->query($sql);
$sql = 'INSERT INTO creqPeopleUpdate VALUES ';
$sqlParts = array();
foreach ($models as $model) {
$sqlParts[] = $db->quoteInto('(?, ', $model->_data['userId'])
. $db->quoteInto('?, ' , $model->_data['firstName'])
. $db->quoteInto('?, ' , $model->_data['middleName'])
. $db->quoteInto('?, ' , $model->_data['lastName'])
. $db->quoteInto('?, ' , $model->_data['email'])
. $db->quoteInto('?, ' , $model->_data['phone'])
. $db->quoteInto('?)' , $model->_data['department']);
}
$sql .= implode(', ', $sqlParts);
$db->query($sql);
$sql = 'UPDATE creqPeople AS a, '
. ' creqPeopleUpdate AS b '
. 'SET a.firstName = b.firstName, '
. ' a.middleName = b.middleName, '
. ' a.lastName = b.lastName, '
. ' a.email = b.email, '
. ' a.phone = b.phone, '
. ' a.department = b.department '
. 'WHERE a.userId = b.userId ';
$db->query($sql);
$db->query('DROP TABLE creqPeopleUpdate');
}
static protected function _insert(Unl_Model_Collection $models)
{
if (count($models) == 0) {
return;
}
$db = Zend_Registry::get('db');
$sql = 'INSERT INTO creqGroups (type, name, description) VALUES ';
$sqlParts = array();
foreach ($models as $model) {
$sqlParts[] = $db->quoteInto('(?, ', 1)
. $db->quoteInto('?, ' , $model->_data['userName'])
. $db->quoteInto('?)' , $model->_data['firstName'] . ' ' . $model->_data['lastName'] . "'s Primary Group");
}
$sql .= implode(', ', $sqlParts);
$db->query($sql);
$lastId = $db->lastInsertId();
foreach ($models as $model) {
$model->_data['primaryGroup'] = $lastId;
$lastId++;
}
$sql = 'INSERT INTO creqUsers (userName, primaryGroup) VALUES ';
$sqlParts = array();
foreach ($models as $model) {
$sqlParts[] = $db->quoteInto('(?, ', $model->_data['userName'])
. $db->quoteInto('?)' , $model->_data['primaryGroup']);
}
$sql .= implode(', ', $sqlParts);
$db->query($sql);
$lastId = $db->lastInsertId();
foreach ($models as $model) {
$model->_data['userId'] = $lastId;
$lastId++;
}
$sql = 'INSERT INTO creqPeople (userId, firstName, middleName, lastName, email, phone, department) VALUES ';
$sqlParts = array();
foreach ($models as $model) {
$sqlParts[] = $db->quoteInto('(?, ', $model->_data['userId'])
. $db->quoteInto('?, ' , $model->_data['firstName'])
. $db->quoteInto('?, ' , $model->_data['middleName'])
. $db->quoteInto('?, ' , $model->_data['lastName'])
. $db->quoteInto('?, ' , $model->_data['email'])
. $db->quoteInto('?, ' , $model->_data['phone'])
. $db->quoteInto('?)' , $model->_data['department']);
}
$sql .= implode(', ', $sqlParts);
$db->query($sql);
}
public function getId()
{
return $this->_data['userId'];
}
public function getUsername()
{
return $this->_data['userName'];
}
public function setUsername($username)
{
$this->_data['userName'] = $username;
}
public function getFirstName()
{
return $this->_data['firstName'];
}
public function setFirstName($firstName)
{
$this->_data['firstName'] = $firstName;
}
public function getLastName()
{
return $this->_data['lastName'];
}
public function getEmail()
public function setLastName($lastName)
{
return $this->_data['email'];
$this->_data['lastName'] = $lastName;
}
public function getEmail()
{
return $this->_data['email'];
}
public function setEmail($email)
{
$this->_data['email'] = $email;
}
public function getPhone()
{
return $this->_data['phone'];
}
public function setPhone($phone)
{
$this->_data['phone'] = $phone;
}
public function getPrimaryGroup()
{
return $this->_data['primaryGroup'];
}
public function setPrimaryGroup($primaryGroup)
{
$this->_data['primaryGroup'] = $primaryGroup;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment