Bạn không nên gây ô nhiễm mô hình của mình với rác.Vui lòng luôn ghi nhớ các nguyên tắc cơ bản về MVC này:
- Bộ điều khiển của bạn không được biết về việc triển khai mô hình của bạn.
- Đừng gây ô nhiễm mô hình của bạn với nội dung không được kết nối với mô hình kinh doanh của ứng dụng của bạn.
Luôn tạo mã tái sử dụng, làm cho mã của bạn "khô" (Đừng lặp lại chính mình)
Bằng cách này, mục đích của các lĩnh vực tên là gì? Vì biểu mẫu sẽ chỉ khả dụng cho người dùng đã đăng nhập, tên người dùng có thể được truy cập với Yii :: app() -> user.
<?php
// models/ChangePasswordForm.php
class ChangePasswordForm extends CFormModel
{
/**
* @var string
*/
public $currentPassword;
/**
* @var string
*/
public $newPassword;
/**
* @var string
*/
public $newPasswordRepeat;
/**
* Validation rules for this form.
*
* @return array
*/
public function rules()
{
return array(
array('currentPassword, newPassword, newPasswordRepeat', 'required'),
array('currentPassword', 'validateCurrentPassword', 'message'=>'This is not your password.'),
array('newPassword', 'compare', 'compareAttribute'=>'validateNewPassword'),
array('newPassword', 'match', 'pattern'=>'/^[a-z0-9_\-]{5,}/i', 'message'=>'Your password does not meet our password complexity policy.'),
);
}
/**
* I don't know your hashing policy, so I assume it's simple MD5 hashing method.
*
* @return string Hashed password
*/
protected function createPasswordHash($password)
{
return md5($password);
}
/**
* I don't know how you access user's password as well.
*
* @return string
*/
protected function getUserPassword()
{
return Yii::app()->user->password;
}
/**
* Saves the new password.
*/
public function saveNewPassword()
{
$user = UserModel::findByPk(Yii::app()->user->username);
$user->password = $this->createPasswordHash($this->newPassword);
$user->update();
}
/**
* Validates current password.
*
* @return bool Is password valid
*/
public function validateCurrentPassword()
{
return $this->createPasswordHash($this->currentPassword) == $this->getUserPassword();
}
}
dụ controller action:
public function actionChangePassword()
{
$model=new ChangePasswordForm();
if (isset($_POST['ChangePasswordForm'])) {
$model->setAttributes($_POST['ChangePasswordForm']);
if ($model->validate()) {
$model->save();
// you can redirect here
}
}
$this->render('changePasswordTemplate', array('model'=>$model));
}
dụ mẫu mã:
<?php echo CHtml::errorSummary($model); ?>
<div class="row">
<?php echo CHtml::activeLabel($model,'currentPassword'); ?>
<?php echo CHtml::activePasswordField($model,'currentPassword') ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'newPassword'); ?>
<?php echo CHtml::activePasswordField($model,'newPassword') ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'newPasswordRepeat'); ?>
<?php echo CHtml::activePasswordField($model,'newPasswordRepeat') ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton('Change password'); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
Mẫu nên đủ dễ dàng để tạo ra. Mã này, với một số chỉnh sửa nhỏ, sẵn sàng để được sao chép & được dán vào một dự án Yii khác.
Bạn nên sử dụng lại khung xác thực hiện tại bất cứ khi nào có thể, bởi vì, thực sự, nó phức tạp. Ví dụ: hãy xem https://github.com/delight-im/PHP-Auth, cả hai đều không có khung công tác bất khả tri và không có cơ sở dữ liệu. – caw