gpt4 book ai didi

php - CakePHP 2.0 帐户验证

转载 作者:行者123 更新时间:2023-11-29 08:52:49 24 4
gpt4 key购买 nike

我正在尝试创建一个简单的登录页面。我希望在该页面上进行验证,以便当用户单击“登录”时,站点会检查用户数据库中的“激活”是否设置为 1,如果不是,则无法登录。我对 cakephp 还很陌生,并且正在尝试快速学习,所以如果这是一个简单的初学者问题,我很抱歉。

这是我的用户模型中的验证

public $checkActive = array(
'activated'=>array(
'rule'=>array('equalTo', '0'),
'message'=>'The account must be activated, please check your email.'
));

这是我的 usersController 中的登录功能

 public function login() {

$this->set('title_for_layout', 'Individual Registration');
$this->set('stylesheet_used', 'style');
$this->set('image_used', 'eBOXLogo.jpg');


if ($this->request->is('post')){
if ($this->request->data['User']['password'] == 'qazwsx'){
if ($this->Auth->login()){
if (0 === $this->User->find('count',array('conditions'=>array('enabled'=>1,'login'=> $username)))) {
$this->Session->setFlash('Sorry, your account is not validated yet.');
}

$this->Auth->user('id');
$this->redirect($this->Auth->redirect('eboxs/home'));
}
}
else {

$this->Session->setFlash('Username or password is incorrect');
}
}else{
$this->Session->setFlash('Welcome, please login');
}


}

这是我在 usersController 中的 beforeLogin 函数

 public function beforeLogin(){

if(isset($this->data['User']['password'])){
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}

应用程序 Controller

class AppController extends Controller {

public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth'=>array(
'loginRedirect'=>array('controller'=>'users', 'action'=>'login'),
'logoutRedirect'=>array('controller'=>'users', 'action'=>'login'),
'authError'=>"You can't access this page",
'authorize'=>array('Controller')
)
);

public function isAuthorized($user){
return true;
}

public function beforeFilter(){
$this->Auth->allow('index','view');
$this->set('logged_in', $this->Auth->loggedIn());
$this->set('current_user',$this->Auth->user());

}

我意识到我的 Controller 中没有对验证的调用,但是对于我的其他验证(例如用户名是唯一的),我不必调用它。

简而言之,目前任何人都可以登录我的页面,我正在努力使只有用户表中激活字段中有 1 的人才能登录。

最佳答案

一种选择是在登录后立即检查帐户验证,如下所示:

<?php
if ($this->request->is('post')){
if ($this->request->data['User']['password'] == 'qazwsx'){
if ($this->Auth->login()) {

// login ok, but check if activated
$username = $this->request->data['User']['username'];
if (0 === $this->User->find('count',array('conditions'=>array('activated'=>1,'username'=> $username)))) {
$this->Session->setFlash('Sorry, your account is not validated yet.');
$this->redirec($this->referer());
}

$this->Auth->user('id');
$this->redirect($this->Auth->redirect('eboxs/home'));
}
}

关于php - CakePHP 2.0 帐户验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10734693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com