我正在尝试通过在CakePHP上检查针对LDAP的输入条件来创建一个授权部分完成的应用程序。我正在关注文字linked in this page,但我遇到了问题。我不想使用任何用户表,我没有兴趣存储数据。我想要做的就是在用户拥有任何LDAP凭据的情况下授予用户访问权限。我设法建立连接并检查数据,但应用程序不会保存会话,因此用户从未登录。你对我做错了什么有任何线索吗?
这是登录功能:
public function login() {
/*if($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash("Invalid data provided");
}
}*/
App::import('Lib', 'ldap');
if ($this->Session->read('Auth.User')) {
$this->redirect(array('controller' => 'allocations', 'action' => 'index'));
} elseif (!empty($this->data)) {
$ldap = new ldap;
if ($ldap->auth($this->Auth->request->data['User']['username'], $this->Auth->request->data['User']['password'])) {
/*$userrow = $this->User->findByUsername($this->data['User']['username']);
if (!$userrow) {
$ldap_info = $ldap->getInfo($this->data['User']['username']);
$this->data['User']['user'] = $this->data['User']['username'];
$this->data['User']['name'] = $ldap_info['name'];
$this->data['User']['group_id'] = 3; //sets the default group
$this->add();
$userrow = $this->User->findByUsername($this->data['User']['user']);
}
$user = $userrow['User'];*/
$user = array()
$this->Auth->Session->write($this->Auth->sessionKey, $user);
$this->Auth->_loggedIn = true;
$this->redirect($this->Auth->redirect());
$this->Session->setFlash('You are logged in!');
} else {
$this->Session->setFlash(__('Login Failed', true));
}
}
}
我尝试调试ldap-> auth(它基本上调用存储在外部库中的ldap_bind函数),并且auth的结果是正确的(该方法返回'true')。所以问题必须在于创建会话。如何让CakePHP知道用户已正确登录并且必须存储会话?谢谢!
答案 0 :(得分:2)
您可以通过
将用户数据从ldap写入会话$this->Session->write('Auth.User', array(
'id' => 1,
'username' => 'admin'
));
我使用上面的代码在会话中设置用户。它与AuthComponent一起使用。
您可以通过AuthComponent :: user()