php - Yii 修改管理员密码

标签 php yii passwords admin

我创建了更改密码功能来更改管理员密码。我使用了 this教程。
现在我在 $model->validate() 中遇到问题。
谁能帮帮我??

Controller

    public function actionIndex()
    {
        $id = 1;
        $model = User::model()->findByAttributes(array('usertype' => $id));
        $model->setScenario('changePwd');

        if (isset($_POST['User'])) {
            $model->attributes = $_POST['User'];
            if ($model->validate()) {
                $model->password = md5($model->new_password);
                if ($model->save()) {
                    Yii::app()->user->setFlash('success', "Password Changed Successfully!");
                }

             } else {
                 Yii::app()->user->setFlash('error', "Change Password failed!");
             }
         }

      $this->render('index', array('model' => $model)); 
   }

型号

    class User extends CActiveRecord
{
    public $old_password;
    public $new_password;
    public $repeat_password;
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{user}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('usertype, firstname, lastname, email, password, mobile, gender, dob, country, area, city, address, street, housenumber, extradirection, createdon', 'required'),
            array('usertype, country, area', 'numerical', 'integerOnly'=>true),
            array('firstname, lastname, email, mobile, dob, city, street, housenumber', 'length', 'max'=>155),
            array('password', 'length', 'max'=>225),
            array('gender', 'length', 'max'=>6),
            array('status', 'length', 'max'=>1),
            array('updatedon', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, usertype, firstname, lastname, email, password, mobile, gender, dob, country, area, city, address, street, housenumber, extradirection, createdon, updatedon, status', 'safe', 'on'=>'search'),
            array('old_password, new_password, repeat_password', 'required', 'on' => 'changePwd'),
            array('old_password', 'findPasswords', 'on' => 'changePwd'),
            array('repeat_password', 'compare', 'compareAttribute'=>'new_password', 'on'=>'changePwd'),
        );
    }

public function findPasswords($attribute, $params)
    {
        $user = User::model()->findByPk(Yii::app()->user->id);
        //echo '<pre>';print_r($user);echo '</pre>';
        if ($user->password != md5($this->old_password))
            $this->addError($attribute, 'Old password is incorrect.');
    }

表单

    <div class="login_con_new"> 
    <div class="form">                  
        <?php    
        $form=$this->beginWidget('CActiveForm', array(
        'id'=>'change-password-form',   
        //'action' => Yii::app()->createUrl('login/authenticate'), 
        // 'enableAjaxValidation' => FALSE,
        'enableClientValidation' => true,
        'clientOptions' => array('validateOnSubmit' => true,),
        'htmlOptions' => array(
        'class' => 'form',
        )
        ));  
        ?>  
        <div class="col-sm-6">
            <h2 class="title">Change Password</h2> 
            <?php
    foreach(Yii::app()->user->getFlashes() as $key => $message) {
        echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
    }
?>
            <div class="form-group">             
            <?php echo $form->labelEx($model,'Current_password'); ?>
            <?php echo $form->passwordField($model,'old_password',array('class'=>'form-control login-field','size'=>60,'maxlength'=>222)); ?>
            <?php echo $form->error($model,'old_password'); ?>  
            </div>
            <div class="form-group"> 
            <?php echo $form->labelEx($model,'new_password'); ?>
            <?php echo $form->passwordField($model,'new_password',array('class'=>'form-control login-field','size'=>60,'maxlength'=>222)); ?>
            <?php echo $form->error($model,'new_password'); ?> 
            </div> 
            <div class="form-group"> 
            <?php echo $form->labelEx($model,'repeat_password'); ?>
            <?php echo $form->passwordField($model,'repeat_password',array('class'=>'form-control login-field','size'=>60,'maxlength'=>222)); ?>
            <?php echo $form->error($model,'repeat_password'); ?> 
            </div>          
            <div class="form-group">
            <div class="col-lg-4" style="padding-left: 0px;">
            <?php echo CHtml::submitButton('Change',array('class' => 'btn btn-success','style'=>'color:white')); ?></div>  
        </div>      
    </div>
    <?php  $this->endWidget(); ?>
</div>      </div> 

$valid 返回 false 并进入 else 部分。

最佳答案

我认为在这一行 $model = User::model()->findByAttributes(array('usertype' => $id)); 中你对用户类型犯了错误。这是用户 ID。

关于php - Yii 修改管理员密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32865437/

相关文章:

php - Yii - Cdbcriteria - 在具有关系的条件之间选择 where 运算符

select - 如何在 Yii 中根据条件从表中选择两列的 DISTINCT?

security - 使用部分 GUID 作为随 secret 码是否存在安全风险?

php - 如何判断一个MIME类型是JPG、PNG、BMP还是GIF?

php - 为什么 SOAP 参数的顺序在 PHP SOAP 中很重要,以及如何修复它?

php - 最小化连接数的最佳方法是什么?

php - 从mysql表中获取最近30天的数据并按日期分组

yii - 将 Yii 转换为 Yii 2.0 : getRestParams()

postgresql - 为什么 psql 不要求我输入密码来访问我刚刚为其创建密码的登录角色的 psql?

c - 加密 C 库