Author Topic: change password error in cakephp  (Read 2685 times)

0 Members and 1 Guest are viewing this topic.

Offline sonoton345Topic starter

  • Enthusiast
  • Posts: 85
    • View Profile
change password error in cakephp
« on: August 17, 2009, 08:12:11 AM »
Can someone help me with this code please? I'm trying to setup a page to change password, first by entering the old password then the new password(twice). What my script is doing is putting a new entry into the database.

My Controller code:
Code: [Select]
function admin_changepassword()
{
if($this->Session->check('userid'))
{
if($this->Session->read('usertype')=='admin')
{

if (!empty($this->data))
{
$this->data['Admin']['id']=$_SESSION['id'];
$uid = $this->Admin->findById($this->data['Admin']['id']);
  if($this->data['Admin']['current']!= $uid['Admin']['password'])
  {
  $this->Session->setFlash("Your old Password field didn't match");
  }
  else if($this->data['Admin']['new_password'] != $this->data['Admin']['confirm_password'] ) {
  $this->Session_setFlash("New password and Confirm password field do not match");
  }
  else {
  $this->data['Admin']['password'] = $this->data['Admin']['new_password'];
  $this->data['Admin']['id'] = $this->Admin->id;
  if($this->Admin->save($this->data))
  {
  $this->Session->setFlash("Password updated");
  $this->redirect(array('controller'=>'jobseekers','action'=>'welcome/'));
  }
  else
{

$this->Session->setFlash('Please try again.', true);

}
  }

}
}
}
else
{
$this->Session->setFlash('You are not logged in.', true);
$this->redirect(array('action'=>'index/'));
}
$this->layout = 'admin';
}
My view code:
Code: [Select]
<table border="0" width="100%" id="table7" cellspacing="0" cellpadding="0">
<tr>
<td>
<img border="0" src="<?= $html->url('/img/adm/changepass.gif'); ?>" width="235" height="38"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>
<?php echo $form->create('Employer',array('action'=>'changepassword')); ?>
<table border="0" width="753" id="table9" cellspacing="5" cellpadding="0">
<tr>
<td width="140" align="left">
<p style="margin-left: 10px">Password: </td>
<td width="598">
<p style="margin-left: 10px">
<?php echo $form->input('Admin.current',array('type'=>'password','label'=>false,'class'=>'input','id'=>'textfield','style'=>'font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left'))?>
<!--input name="textfield29" size="34" style="font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left" /--></td>
</tr>
<tr>
<td width="140" align="left">
<p style="margin-left: 10px">New Password:</td>
<td width="598">
<p style="margin-left: 10px">
<?php echo $form->input('Admin.new_password',array('type'=>'password','label'=>false,'class'=>'input','id'=>'textfield','style'=>'font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left'))?>
<!--input name="textfield27" size="34" style="font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left" /--></td>
</tr>
<tr>
<td width="140" align="left">
<p style="margin-left: 10px">Confirm New
Password:</td>
<td width="598">
<p style="margin-left: 10px">
<?php echo $form->input('Admin.confirm_password',array('type'=>'password','label'=>false,'class'=>'input','id'=>'textfield','style'=>'font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left'))?>
<!--input name="textfield28" size="34" style="font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left" /--></td>
</tr>
                            <?php echo $form->input('id',array('type'=>'hidden','value'=>$_SESSION['userid']));?>
<tr>
<td width="140">&nbsp;</td>
<td width="598">
<p style="margin-left: 10px; margin-top: 5px; margin-bottom: 5px" align="left">
<?php echo $form->submit('/img/adm/submit.gif'); ?>
<!--img border="0" src="../ima/tit/submit.gif" width="58" height="15"--></td>
</tr>
</table>
<?php echo $form->end();?>
</td>
</tr>
</table>
[/code]
My admin table has 3 fields: ID,USERNAME AND PASSWORD

Offline jcombs_31

  • Guru
  • Addict
  • *
  • Posts: 2,426
    • View Profile
    • My Blog
Re: change password error in cakephp
« Reply #1 on: August 17, 2009, 05:30:32 PM »
From what I can see, you are doing a few things wrong. First, you are creating a form with the 'Employer' model but then using an Admin model data. I suspect this is your problem right there.  I would use saveField rather than the save function.  That form is a mess and hard to read with all that inline styles and tables.  You should really clean up your code and create a stylesheet. You should be doing your validation in the model, not the controller. You can create a custom function to see if 2 fields match.

Offline sonoton345Topic starter

  • Enthusiast
  • Posts: 85
    • View Profile
Re: change password error in cakephp
« Reply #2 on: August 17, 2009, 05:45:59 PM »
hmm..ok..I can do everything else with admin (add employer,delete employer etc) except changing admin password. I kind of "inherited" the code from a previous programmer working on the site but all else seem to work in admin even though there are some functions in Employer Controller and some functions in Jobseeker Controller.
So are you suggesting creating an admin controller?

Offline jcombs_31

  • Guru
  • Addict
  • *
  • Posts: 2,426
    • View Profile
    • My Blog
Re: change password error in cakephp
« Reply #3 on: August 17, 2009, 06:03:25 PM »
I would re-read what I wrote.

Quote
<?php echo $form->create('Employer',array('action'=>'changepassword')); ?>

Is changepassword a function of Employer or Admin

Offline sonoton345Topic starter

  • Enthusiast
  • Posts: 85
    • View Profile
Re: change password error in cakephp
« Reply #4 on: August 17, 2009, 06:07:11 PM »
It's a function for admin to change his/her password.

Offline jcombs_31

  • Guru
  • Addict
  • *
  • Posts: 2,426
    • View Profile
    • My Blog
Re: change password error in cakephp
« Reply #5 on: August 19, 2009, 07:22:25 AM »
It's a function for admin to change his/her password.

I understand what it does.  But when you call the form help you are calling it with the wrong model.

You should be using

Code: [Select]

$form->create('Admin', array(...)); ?>


While I suppose you could do it by specifying Admin.variable for the form fields, it doesn't make sense, and if for some reason an associate is not defined correctly it is not going to work correctly.  Since all your data belongs to the Admin model, that is what you should be specifying for the form creation.

Second, if debug is set to 2, you should be able to see the queries that cake is running at the bottom of your screen.  I would take a look there to see if that leads to the answer.