【问题标题】:CakePHP 2.0 Account validationCakePHP 2.0 账户验证
【发布时间】:2012-05-30 20:55:42
【问题描述】:

我正在尝试创建一个简单的登录页面。我希望在该页面上进行验证,以便当用户单击登录时,站点会检查用户数据库中激活的是否设置为 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;
    }

应用控制器

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());

    }

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

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

【问题讨论】:

  • 你为什么要手动检查密码?请发布您的 Auth 组件设置(应该在您的 AppController 中)

标签: php mysql database cakephp login


【解决方案1】:

在您的身份验证设置中添加scope 选项:

'Auth'=>array(
        'loginRedirect'=>array('controller'=>'users', 'action'=>'login'),
        'logoutRedirect'=>array('controller'=>'users', 'action'=>'login'),
        'authError'=>"You can't access this page",
        'authorize'=>array('Controller'),
        'scope' => array('User.activated' => 1)
    )

如果用户没有User.activated = 1,这将阻止用户登录。

另外,查看您的auth setup and re-read the manual page for CakePHP 2.0,您的配置看起来像 1.3。应该不用自己去查密码,这么简单的设置绝对不需要beforeLogin方法。

【讨论】:

  • 这将阻止记录,但不包含任何未记录原因的线索
【解决方案2】:

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

<?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')); 
    }   
} 

【讨论】:

  • 此代码阻止所有人登录,包括已激活的人 =1
  • 也没有出现 setflash,它出现了一个数据库错误,说我们在错误视图中缺少 pdo.ctp 视图
  • 好的,将“已激活”字段的代码从“true”更新为“1”,并设置了referer() 重定向。
  • 还是不行,上传我的新函数
  • 注意:它不是“启用”而是“激活”......还更正了“用户名”中的“登录”字段。小心更新整个 'if (0==...' 块
猜你喜欢
  • 2012-08-26
  • 1970-01-01
  • 2017-04-20
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 2012-01-27
  • 1970-01-01
相关资源
最近更新 更多