Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Curriculum-Request
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tim Steiner
Curriculum-Request
Commits
25831c76
Commit
25831c76
authored
16 years ago
by
Tim Steiner
Browse files
Options
Downloads
Patches
Plain Diff
When a user logs in for the first time, import their data from LDAP.
parent
fb97c30d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
application/modules/auth/controllers/IndexController.php
+6
-0
6 additions, 0 deletions
application/modules/auth/controllers/IndexController.php
application/modules/auth/models/UserModel.php
+279
-3
279 additions, 3 deletions
application/modules/auth/models/UserModel.php
with
285 additions
and
3 deletions
application/modules/auth/controllers/IndexController.php
+
6
−
0
View file @
25831c76
...
...
@@ -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
(
'/'
);
}
...
...
This diff is collapsed.
Click to expand it.
application/modules/auth/models/UserModel.php
+
279
−
3
View file @
25831c76
...
...
@@ -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
g
et
Email
(
)
public
function
s
et
LastName
(
$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
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment