Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The process
- 1. User provides details
- 2. Cake checks the database
- 3. If OK, then check the custom object method
- 4. If OK, return true
- // What I'd like:
- 1. User provides details.
- 2. Check the custom object method
- 3. If OK, return true
- 4. Profit.
- // app/controllers/components/ldap_auth.php
- <?php
- App::import('Component', 'Auth');
- class LdapAuthComponent extends AuthComponent {
- /**
- * Don't hash passwords
- */
- function hashPasswords($data){
- return $data;
- }
- /**
- * We will initially identify the user
- */
- function identify($user=null, $conditions=null) {
- // bind credentials against ldap
- $ldapUser = $this->_ldapAuth($user); // do your stuff
- if (!$ldapUser) {
- return null; // if bind fails, then return null (as stated in api)
- }
- // get the cake model you would normally be authenticating against
- $model =& $this->getModel(); // default is User
- // check for existing User in mysql
- $user = $model->find('first', array('conditions' => array(
- 'username' => $ldapUser['cn']
- ));
- // if no existing User, create a new User
- if (!$user) {
- $user = $model->save(array('User' => array(
- 'username' => $ldapUser['cn'],
- // .. map needed ldap fields to mysql fields ..
- )));
- if (!$user) {
- $this->cakeError('ldapCreateUser');
- }
- // pass the id of the newly created User to Auth's identify
- return parent::identify($model->id, $conditions);
- }
- // pass the id of the existing User to Auth's identify
- return parent::identify($user[$this->userModel][$model->primaryKey], $conditions);
- }
- /**
- * Lets check LDAP
- *
- * @return mixed Array of user data from ldap, or false if bind fails
- */
- function _ldapAuth($user) {
- $username = $user[$this->userModel][$this->fields['username']];
- $password = $user[$this->userModel][$this->fields['password']];
- // use the php ldap functions here
- return $ldapUser;
- }
- }
- ?>
- function hashPasswords($data) {
- $data['User']['password'] = 'a';
- return $data;
- }
- $this->Auth->authenticate = ClassRegistry::init('User');
- // The process
- 1. User provides details
- 2. Cake checks the database **and always returns OK**
- 3. If OK, then check the custom object method
- 4. If OK, return true
Add Comment
Please, Sign In to add comment