Guest User

Untitled

a guest
Apr 9th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. // The process
  2. 1. User provides details
  3. 2. Cake checks the database
  4. 3. If OK, then check the custom object method
  5. 4. If OK, return true
  6.  
  7. // What I'd like:
  8. 1. User provides details.
  9. 2. Check the custom object method
  10. 3. If OK, return true
  11. 4. Profit.
  12.  
  13. // app/controllers/components/ldap_auth.php
  14. <?php
  15. App::import('Component', 'Auth');
  16. class LdapAuthComponent extends AuthComponent {
  17. /**
  18. * Don't hash passwords
  19. */
  20. function hashPasswords($data){
  21. return $data;
  22. }
  23. /**
  24. * We will initially identify the user
  25. */
  26. function identify($user=null, $conditions=null) {
  27. // bind credentials against ldap
  28. $ldapUser = $this->_ldapAuth($user); // do your stuff
  29. if (!$ldapUser) {
  30. return null; // if bind fails, then return null (as stated in api)
  31. }
  32. // get the cake model you would normally be authenticating against
  33. $model =& $this->getModel(); // default is User
  34. // check for existing User in mysql
  35. $user = $model->find('first', array('conditions' => array(
  36. 'username' => $ldapUser['cn']
  37. ));
  38. // if no existing User, create a new User
  39. if (!$user) {
  40. $user = $model->save(array('User' => array(
  41. 'username' => $ldapUser['cn'],
  42. // .. map needed ldap fields to mysql fields ..
  43. )));
  44. if (!$user) {
  45. $this->cakeError('ldapCreateUser');
  46. }
  47. // pass the id of the newly created User to Auth's identify
  48. return parent::identify($model->id, $conditions);
  49. }
  50. // pass the id of the existing User to Auth's identify
  51. return parent::identify($user[$this->userModel][$model->primaryKey], $conditions);
  52. }
  53. /**
  54. * Lets check LDAP
  55. *
  56. * @return mixed Array of user data from ldap, or false if bind fails
  57. */
  58. function _ldapAuth($user) {
  59. $username = $user[$this->userModel][$this->fields['username']];
  60. $password = $user[$this->userModel][$this->fields['password']];
  61. // use the php ldap functions here
  62. return $ldapUser;
  63. }
  64. }
  65. ?>
  66.  
  67. function hashPasswords($data) {
  68. $data['User']['password'] = 'a';
  69. return $data;
  70. }
  71.  
  72. $this->Auth->authenticate = ClassRegistry::init('User');
  73.  
  74. // The process
  75. 1. User provides details
  76. 2. Cake checks the database **and always returns OK**
  77. 3. If OK, then check the custom object method
  78. 4. If OK, return true
Add Comment
Please, Sign In to add comment