在YII框架中,Captcha;t show(与accessControl冲突)


In YII Framework, the Captcha doesn't show(conflict with accessControl)

在我的站点中,只有当我删除filters()方法时,captcha才能出现。其他时间captcha不起作用。我的php-gd支持是启用的。

现在我使用的是自定义WebUser,如果我将其从配置中删除,captcha也能很好地工作。

顺便说一句,如果我直接访问user/catcha,它只显示一个图片框,而不显示内容,可能无法加载图片。。

以下是我的UserController:中的一些代码段

actions();

public function actions()
{
    return array(
        // captcha action renders the CAPTCHA image displayed on the contact page
        'captcha'=>array(
            'class'=>'CCaptchaAction',
            'backColor'=>0xFFFFFF,
            'minLength' => 4,
            'maxLength' => 4,
            'testLimit' => 99999
            )
    );
}

过滤器():

public function filters()
{
    // return the filter configuration for this controller, e.g.:
    return array(
        "accessControl",
    );
}

accessRulse():

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('captcha'),
            'users'=>array('*'),
        ),
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('index','login','signup'),
            'expression'=>'Yii::app()->user->isGuest',
        ),
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array('cpassword','info','logout'),
            'expression'=>'!Yii::app()->user->isGuest',
        ),
        array('allow', // allow admin user to perform 'admin' and 'delete' actions
            'users'=>array('admin@example.com'),
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
            'message'=>'Access Denied.',
        ),
    );
}

我的WebUsers.php

<?php 
// this file must be stored in: 
// protected/components/WebUser.php 
class WebUser extends CWebUser { 
  // Store model to not repeat query. 
  private $_model; 
  // Return first name. 
  // access it by Yii::app()->user->first_name 
  public function getDisplayName(){ 
    $user = $this->loadUser(Yii::app()->user->id);
    if($user)
        return $user->display_name; 
  } 
  public function getGroupId(){
      $user = $this->loadUser(Yii::app()->user->id); 
      return $user->group_id;
  }
  // This is a function that checks the field 'role' 
  // in the User model to be equal to 1, that means it's admin 
  // access it by Yii::app()->user->isAdmin() 
  public function isAdmin(){ 
      $user = $this->loadUser(Yii::app()->user->id); 
      return intval($user->group_id) == 1; 
  } 
  public function isGroupAAS(){
      $user = $this->loadUser(Yii::app()->user->id); 
      return intval($user->group_id) == 1001;
  }

  // Load user model. 
  protected function loadUser($id=null) 
    { 
        if($this->_model===null) 
        { 
            if($id!==null) 
                $this->_model=User::model()->findByPk($id); 
        } 
        return $this->_model; 
    }
  protected function afterLogin($fromCookie){
      $user = $this->loadUser($this->id);
      $user->last_login_ip = Yii::app()->request->userHostAddress;
      $user->last_login_time = new CDbExpression('NOW()');
      $user->save();
  }
} 
?> 

在您的控制器中,确保已定义。

    // captcha action renders the CAPTCHA image displayed on the contact page
    'captcha'=>array(
        'class'=>'CCaptchaAction',
        'backColor'=>0xFFFFFF,
    ),

然后,允许执行以下操作。

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('captcha'),
            'users'=>array('*'),
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
            'message'=>'Access Denied.',
        ),
    );
}

并且以的形式

<?php $this->widget('CCaptcha'); ?><br>
<?php echo CHtml::textField('captcha'); ?>

如果这不起作用,试试这个办法。。

<?php $this->widget('CCaptcha', array('captchaAction' => 'site/captcha')); ?>

要验证capthca,请在操作中将其定义如下

$captcha=Yii::app()->getController()->createAction("captcha");
$code = $captcha->verifyCode;
if($code === $_REQUEST['captcha']){
}

您的代码看起来很好,并将您的代码与此答案进行比较,或者请提供源代码进行查看。

访问您的captcha显示方法,即actions

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('actions'),
            'users'=>array('*'),
        ),

我已经用我的一个SiteController&它(captcha动作)在接触动作下运行良好。你能发布完整的UserController代码来审查吗;确定确切的原因?

只需在操作数组中添加"captcha",如

public function accessRules()
{
    return array(array('allow',  // allow admin user to perform these actions
            'actions'=>array('index','view','add','captcha'),
            'users'=>array('admin'),
        ), ...