Reputation: 420
I am using a custom authentification object in cake php.
I have created a file in component/Auth/LdapAuthenticate.php
. In this file I have a function who made the authentification with LDAP. It looks like this:
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
class LdapAuthenticate extends BaseAuthenticate {
public function authenticate(CakeRequest $request, CakeResponse $response) {
$username=$request->data["Users"]["username"];
$pwd=$request->data["Users"]["password"];
$ldap = ldap_connect("ldap:........");
ldap_set_option ($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
$bind = @ldap_bind($ldap, "TEST\\".$username, $pwd);
if ($bind && $pwd!="") {
//CakeLog::write('debug', "loggé");
$ldap_dn ="DC=world,DC=pcm,DC=local";
$filter = "(&(objectClass=user)(samaccountname=".$username.")(cn=*))";
$justthese = array("cn","mail","givenname","distinguishedname","memberof");
$sr=ldap_search($ldap, $ldap_dn, $filter,$justthese);
$info = ldap_get_entries($ldap, $sr);
ldap_close($ldap);
return $info;
} else {
ldap_close($ldap);
return false;
}
}
}
And it log me like this in the User controller :
function login(){
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}else{
$this->Session->setFlash(__('Username ou password incorrect'), 'default', array('class'=>'error-message'), 'auth');
}
I now want to create a second login controller which will log my user to a database. The question I have is how can I create a second custom authentication object and call it at the right place? I want use it in the function logindist()
. There will be 2 pages for authentication, one for ldap connection and an other for database connection.
Upvotes: 1
Views: 313
Reputation: 936
i didn't understand your question . but i will it give blind shot anyway. i think you you want to support multiply cakephp auth. auth objects are they for that purpose. you can attach many objects and cakephp will check them sequentially , if any can identify the request, access will be allowed.
$this->Auth->authenticate = array(
'databaseAuth',
'Ldap'
);
if all your auth objects do they identification in their authenticate() method i.e not stataless, then you don't need any setup more than just include auth objects in correct order and cakephp will take over from there. conversely if you need to authenticate yor users first against the database then the above set will do just as fine.
but remember though if you need to do parallel auth in your app like you said above you will need to call identify() method manually or implement getUser() method in auth objects for cakephp Auth to function properly
public function logindist(){
$user = $this->Auth->identify();
if($user){
$this->Auth->allow();
}
// throw 403 exception
}
Upvotes: 2