Zend framework2 更改zf2中登录用户的密码

Zend framework2 更改zf2中登录用户的密码,zend-framework2,Zend Framework2,我是Zf2的新手。我有用户和管理模块。当用户登录时,我在管理员中创建了配置文件,他在其中更改了配置文件和密码。 现在我正在更改密码,但我不明白如何为更改密码编码。我的代码如下: 我的ProfileController是: <?php namespace Admin\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; use Admin\Mode

我是Zf2的新手。我有用户和管理模块。当用户登录时,我在管理员中创建了配置文件,他在其中更改了配置文件和密码。 现在我正在更改密码,但我不明白如何为更改密码编码。我的代码如下:

我的ProfileController是:

 <?php

namespace Admin\Controller;

 use Zend\Mvc\Controller\AbstractActionController;
 use Zend\View\Model\ViewModel;

 use Admin\Model\Profile;           
 use Admin\Form\ProfileForm;

 class ProfileController extends AbstractActionController
 {


protected $profileTable;  


public function getAuthService()
{
    $this->authservice = $this->getServiceLocator()->get('AuthService');  
    return $this->authservice;  
}


public function indexAction()
{
    $this->layout("layout/layout_admin");   
    /* this is used to show user email of logined user. */  
    $user_email = $this->getAuthService()->getStorage()->read(); 

    // below condition is new and for back browser validation 
    if (!$this->getServiceLocator()
            ->get('AuthService')->hasIdentity()) {
        return $this->plugin(redirect)->toRoute('login', array('action' => 'index'));
    } 

    return new ViewModel(array(
        'admin_profile' => $this->getProfileTable()->fetchAll($user_email),             
    ));


}

public function editAction()
{
    $this->layout("layout/layout_admin");  

    $id = (int) $this->params()->fromRoute('id', 0);
    if (!$id) {
        return $this->redirect()->toRoute('login', array(
            'action' => 'index'
        ));
    }

    // Get the Profile with the specified id.  An exception is thrown
    // if it cannot be found, in which case go to the index page.
    try {
        $profile = $this->getProfileTable()->getProfile($id);
    }
    catch (\Exception $ex) {
        return $this->redirect()->toRoute('profile', array(
            'action' => 'index'
        ));
    }

    $form  = new ProfileForm();
    $form->bind($profile);
    $form->get('submit')->setAttribute('value', 'Edit');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setInputFilter($profile->getInputFilter());
        $form->setData($request->getPost());

        if ($form->isValid()) {
            $this->getProfileTable()->saveProfile($profile);

            // Redirect to profile
            return $this->redirect()->toRoute('profile');
        }
    }

    return array(
        'id' => $id,
        'form' => $form,
    );

}

 public function changepasswordAction()
{
    $this->layout("layout/layout_admin");  

    $id = (int) $this->params()->fromRoute('id', 0);
    if (!$id) {
        return $this->redirect()->toRoute('login', array(
            'action' => 'index'
        ));
    }

    // Get the Profile with the specified id.  An exception is thrown
    // if it cannot be found, in which case go to the index page.
    try {
        $profile = $this->getProfileTable()->getProfile($id);
    }
    catch (\Exception $ex) {
        return $this->redirect()->toRoute('profile', array(
            'action' => 'index'
        ));
    }


    $form  = new ProfileForm();
    $form->bind($profile);
    $form->get('submit')->setAttribute('value', 'Change');



    $request = $this->getRequest();  


    if ($request->isPost()) {
        $form->setInputFilter($profile->getInputFilter());
        $form->setData($request->getPost()); 

        if ($form->isValid()) { 
            echo "form is valid now save the data";
        }else echo "form is invalid, don't save data";
    }


    return array(
        'id' => $id,
        'form' => $form,
    );

}


public function getProfileTable()
{
    if (!$this->profileTable) {
        $sm = $this->getServiceLocator();
        $this->profileTable = $sm->get('Admin\Model\ProfileTable');
    }
    return $this->profileTable;
}



}

这是我自己完成的。更改密码的答案和最佳解决方案如下:

changepassword.phtml:

<div class="jumbotron">
<?php 

 $title = 'Change your password Here:';
 $this->headTitle($title);
 ?>
<h2 align="center"><?php echo $this->escapeHtml($title); ?></h2>

<div align="right">
<a href="<?php echo $this->url('profile');?>">View Profile</a>
</div>
<br/>



<?php
 $form = $this->form;
 $form->setAttribute('action', $this->url('profile',
     array(
         'action' => 'changepassword',
         'id'     => $this->id,
     )
 ));
 $form->prepare();
 ?>


 <div align="center">
 <?php



 echo $this->form()->openTag($form);
 echo $this->formHidden($form->get('id'));  
 echo $this->formRow($form->get('old_password'));
 echo "<br/>";
 echo $this->formRow($form->get('new_password'));
 echo "<br/>";
 echo $this->formRow($form->get('confirm_password'));
 echo "<br/>"; 

 echo $this->formSubmit($form->get('submit'));
 echo $this->form()->closeTag();
 ?>
 <a href="<?php echo $this->url('profile');?>">Cancel</a>
 </div> 


 </div>
<div class="jumbotron">
<h2 align="center">Change Your Password Here:</h2>

<div align="right">
<a href="<?php echo $this->url('profile');?>">View Profile</a>
</div>
<br/>

 <?php
 $form = $this->form;
 $form->setAttribute('action', $this->url('profile',
     array(
     'action' => 'changepassword',
     'id'     => $this->id,
     )
 ));
 $form->prepare();
 ?>

 <?php echo $error; ?>
 <div align="center">
 <?php
echo $this->form()->openTag($form);
 echo $this->formHidden($form->get('id')); 
echo $this->formRow($form->get('old_password'));
 echo "<br/>";
 echo $this->formRow($form->get('new_password'));
 echo "<br/>";
 echo $this->formRow($form->get('confirm_password'));
 echo "<br/>"; 

 echo $this->formSubmit($form->get('submit'));
 echo $this->form()->closeTag();
 ?>
 <a href="<?php echo $this->url('profile');?>">Cancel</a>
 </div> 

 </div>
在ProfileTable.php中添加以下函数

public function updatePassword($password,Profile $profile)
{
    $data = array( 
        'password' => md5($password), 
    ); 
     $id = (int) $profile->id;

    if ($this->getProfile($id)) 
    {
        $this->tableGateway->update($data, array('id' => $id));
    } 
    else 
    {
        throw new \Exception('Profile id does not exist');
    }  

}
在ProfileController.php中添加以下代码

 public function changepasswordAction()
{
    $this->layout("layout/layout_admin");  

    $id = (int) $this->params()->fromRoute('id', 0);
    if (!$id) {
        return $this->redirect()->toRoute('login', array(
            'action' => 'index'
        ));
    }

    // below condition is new and for back browser validation 
    if (!$this->getServiceLocator()
            ->get('AuthService')->hasIdentity()) {
        return $this->plugin(redirect)->toRoute('login', array('action' => 'index'));
    } 
    $error = ""; 
    $password="";

    // Get the Profile with the specified id.  An exception is thrown
    // if it cannot be found, in which case go to the index page.
    try {
        $profile = $this->getProfileTable()->getProfile($id);
    }
    catch (\Exception $ex) {
        return $this->redirect()->toRoute('profile', array(
            'action' => 'index'
        ));
    }


    $form  = new ProfileForm();
    $form->bind($profile);
    $form->get('submit')->setAttribute('value', 'Change');



    $request = $this->getRequest(); 



    if ($request->isPost()) {
        $form->setInputFilter($profile->getInputFilter2());
        $form->setData($request->getPost()); 

        if(($profile->password == md5($this->params()->fromPost('old_password')))AND($form->isValid())) 
        {
            if($this->params()->fromPost('new_password') == $this->params()->fromPost('confirm_password')) 
            {  
                $password=$this->params()->fromPost('confirm_password');
                $this->getProfileTable()->updatePassword($password,$profile);

                //Redirect to profile
                return $this->redirect()->toRoute('profile');
            } 
            else 
            {
                $error="<b>Error: </b>New Password and Confirm password didn't match";
            }

        } 
        else 
        {
            $error="<b>Error: </b>Old Password didn't match";
        }


    }


    return array(
        'id' => $id,
        'form' => $form,
        'error' => $error,
    );

}
公共函数changepasswordAction()
{
$this->layout(“layout/layout_admin”);
$id=(int)$this->params()->fromRoute('id',0);
如果(!$id){
返回$this->redirect()->toRoute('login',数组(
'操作'=>'索引'
));
}
//以下条件是新的,用于后浏览器验证
如果(!$this->getServiceLocator()
->get('AuthService')->hasIdentity(){
返回$this->plugin(redirect)->toRoute('login',array('action'=>'index'));
} 
$error=“”;
$password=“”;
//获取具有指定id的配置文件。将引发异常
//如果找不到,则转到索引页。
试一试{
$profile=$this->getProfileTable()->getProfile($id);
}
捕获(\Exception$ex){
返回$this->redirect()->toRoute('profile',数组(
'操作'=>'索引'
));
}
$form=newprofileform();
$form->bind($profile);
$form->get('submit')->setAttribute('value','Change');
$request=$this->getRequest();
如果($request->isPost()){
$form->setInputFilter($profile->getInputFilter2());
$form->setData($request->getPost());
如果($profile->password==md5($this->params()->fromPost('old_password'))和($form->isValid())
{
如果($this->params()->fromPost('new_password')==$this->params()->fromPost('confirm_password'))
{  
$password=$this->params()->fromPost('confirm_password');
$this->getProfileTable()->updatePassword($password,$profile);
//重定向到配置文件
返回$this->redirect()->toRoute('profile');
} 
其他的
{
$error=“error:新密码和确认密码不匹配”;
}
} 
其他的
{
$error=“error:旧密码不匹配”;
}
}
返回数组(
'id'=>$id,
'form'=>$form,
'error'=>$error,
);
}
<div class="jumbotron">
<h2 align="center">Change Your Password Here:</h2>

<div align="right">
<a href="<?php echo $this->url('profile');?>">View Profile</a>
</div>
<br/>

 <?php
 $form = $this->form;
 $form->setAttribute('action', $this->url('profile',
     array(
     'action' => 'changepassword',
     'id'     => $this->id,
     )
 ));
 $form->prepare();
 ?>

 <?php echo $error; ?>
 <div align="center">
 <?php
echo $this->form()->openTag($form);
 echo $this->formHidden($form->get('id')); 
echo $this->formRow($form->get('old_password'));
 echo "<br/>";
 echo $this->formRow($form->get('new_password'));
 echo "<br/>";
 echo $this->formRow($form->get('confirm_password'));
 echo "<br/>"; 

 echo $this->formSubmit($form->get('submit'));
 echo $this->form()->closeTag();
 ?>
 <a href="<?php echo $this->url('profile');?>">Cancel</a>
 </div> 

 </div>
public function getInputFilter2()
 {
     if (!$this->inputFilter) {
         $inputFilter = new InputFilter();

         $inputFilter->add(array(
             'name'     => 'id',
             'required' => true,
             'filters'  => array(
                 array('name' => 'Int'),
             ),
         ));

          $inputFilter->add(array(
             'name'     => 'old_password',
             'required' => true,
             'filters'  => array(
                 array('name' => 'StripTags'),
                 array('name' => 'StringTrim'),
             ),
             'validators' => array(
                 array(
                     'name'    => 'StringLength',
                     'options' => array(
                         'encoding' => 'UTF-8',
                         'min'      => 2,
                         'max'      => 100,
                     ),
                 ),
             ),
         ));

          $inputFilter->add(array(
             'name'     => 'new_password',
             'required' => true,
             'filters'  => array(
                 array('name' => 'StripTags'),
                 array('name' => 'StringTrim'),
             ),
             'validators' => array(
                 array(
                     'name'    => 'StringLength',
                     'options' => array(
                         'encoding' => 'UTF-8',
                         'min'      => 2,
                         'max'      => 100,
                     ),
                 ),
             ),
         ));

          $inputFilter->add(array(
             'name'     => 'confirm_password',
             'required' => true,
             'filters'  => array(
                 array('name' => 'StripTags'),
                 array('name' => 'StringTrim'),
             ),
             'validators' => array(
                 array(
                     'name'    => 'StringLength',
                     'options' => array(
                         'encoding' => 'UTF-8',
                         'min'      => 2,
                         'max'      => 100,
                     ),
                 ),
             ),
         ));

         $this->inputFilter = $inputFilter;
     }

     return $this->inputFilter;
 }
public function updatePassword($password,Profile $profile)
{
    $data = array( 
        'password' => md5($password), 
    ); 
     $id = (int) $profile->id;

    if ($this->getProfile($id)) 
    {
        $this->tableGateway->update($data, array('id' => $id));
    } 
    else 
    {
        throw new \Exception('Profile id does not exist');
    }  

}
 public function changepasswordAction()
{
    $this->layout("layout/layout_admin");  

    $id = (int) $this->params()->fromRoute('id', 0);
    if (!$id) {
        return $this->redirect()->toRoute('login', array(
            'action' => 'index'
        ));
    }

    // below condition is new and for back browser validation 
    if (!$this->getServiceLocator()
            ->get('AuthService')->hasIdentity()) {
        return $this->plugin(redirect)->toRoute('login', array('action' => 'index'));
    } 
    $error = ""; 
    $password="";

    // Get the Profile with the specified id.  An exception is thrown
    // if it cannot be found, in which case go to the index page.
    try {
        $profile = $this->getProfileTable()->getProfile($id);
    }
    catch (\Exception $ex) {
        return $this->redirect()->toRoute('profile', array(
            'action' => 'index'
        ));
    }


    $form  = new ProfileForm();
    $form->bind($profile);
    $form->get('submit')->setAttribute('value', 'Change');



    $request = $this->getRequest(); 



    if ($request->isPost()) {
        $form->setInputFilter($profile->getInputFilter2());
        $form->setData($request->getPost()); 

        if(($profile->password == md5($this->params()->fromPost('old_password')))AND($form->isValid())) 
        {
            if($this->params()->fromPost('new_password') == $this->params()->fromPost('confirm_password')) 
            {  
                $password=$this->params()->fromPost('confirm_password');
                $this->getProfileTable()->updatePassword($password,$profile);

                //Redirect to profile
                return $this->redirect()->toRoute('profile');
            } 
            else 
            {
                $error="<b>Error: </b>New Password and Confirm password didn't match";
            }

        } 
        else 
        {
            $error="<b>Error: </b>Old Password didn't match";
        }


    }


    return array(
        'id' => $id,
        'form' => $form,
        'error' => $error,
    );

}