【发布时间】:2015-11-22 19:00:40
【问题描述】:
我想创建一个简单的检查,即用户在登录后是否存在于我的数据库中(LDAP 身份验证)。如果他不存在,则应在数据库中创建一条记录。 有没有标准的 CakePHP 3.x 做这些事情的方式? 或者我可以在我的“用户”控制器中创建一个函数来检查数据库中是否存在用户并在 login() 函数结束时调用它(如果用户会话已成功创建)?
public function login(){
if ($this->request->is('post')){
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
$this->createStudent($user);
return $this->redirect($this->Auth->redirectUrl());
}
// user is not identified
$this->Flash->error('Your username or password is not correct');
}
}
public function createStudent($user){
$studExists = $this->Students->find('isExists', ['uid' => $user['uid']]);
if (!$studExists) {
$student = $this->Students->newEntity();
$data = ['id' => $user['uid']];
$student = $this->Students->patchEntity($student, $data);
if ($this->Students->save($student)) {
$this->Flash->success(__('It is your first logon. You have been added to our database!'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('You could not be saved in our database. Please, try again.'));
}
}
}
提前谢谢你。
【问题讨论】:
标签: php cakephp cakephp-3.0