I am trying to reset the password but it doesn't update. Can someone fix this? I am getting an error message saying I'm not submitting enough non-code so this is filler.
This is reset.ctp:
<?php echo $this->Form->create('User', array('action' => 'reset')); ?>
<?php
echo $this->Form->input('password',array("type"=>"password","name"=>"data[User][password]"));
echo $this->Form->input('password_confirm',array("type"=>"password","name"=>"data[User][password_confirm]"));
echo $this->Form->submit();
echo $this->Form->end();?>
This is the reset method in UsersController.php
function reset($token=null){
//$this->layout="Login";
$this->User->recursive=-1;
if(!empty($token)){
$u=$this->User->findBytokenhash($token);
if($u){
$this->User->id=$u['User']['id'];
if(!empty($this->data)){
$this->User->data=$this->data;
$this->User->data['User']['username']=$u['User']['username'];
$new_hash=sha1($u['User']['username'].rand(0,100));//created token
$this->User->data['User']['tokenhash']=$new_hash;
if($this->User->validates(array('fieldList'=>array('password','password_confirm')))){
if($this->User->save($this->User->data))
{
$this->Session->setFlash('Password Has been Updated');
$this->redirect(array('controller'=>'users','action'=>'login'));
}
}
else{
$this->set('errors',$this->User->invalidFields());
}
}
}
else
{
$this->Session->setFlash('Token Corrupted,Please Retry.the reset link work only for once.');
}
}
else{
$this->redirect('/');
}
}
I used this in my reset.ctp and the password got updated: echo $this->Form->input('password');
Related
Im CakePHP newbie, apologies if the question has been asked i search it but could not find. I have problem with the code below. It works well when it i register new user, problem when im trying to login i get and error "Invalid username or password'); Please help
public function login() {
if ($this->request->is('post')) {
if ((!empty($this->request->data['User']) && $this->Auth->login())) {
$this->Session->setFlash(__('Welcome, '. $this->Auth->user('username')));
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Invalid username or password'));
}
}
}
//******* LOGIN CTP************
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Please enter your username and password'); ?></legend>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
//*******************BEFORE SAVE *************************
public function beforeSave($options = array()) {
// hash our password
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
// if we get a new password, hash it
if (isset($this->data[$this->alias]['password_update']) && !empty($this->data[$this->alias]['password_update'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password_update']);
}
// fallback to our parent
return parent::beforeSave($options);
}
Guys I uploaded my site on servers and I'm facing this problem.
Usercontroller.php
<?php
class UsersController extends AppController {
var $name = 'Users';
var $components = array('Auth', 'Acl');
function beforeFilter(){
//parent::beforeFilter();
$this->Auth->loginError = "asdad!";
$this->Auth->authError = "sdsd !.";
App::uses('CakeEmail', 'Network/Email');
$this->Auth->userModel = 'User';
$this->Auth->allow('login','register','login','step2','TakeId','kontakt');
}
function login(){
if(!empty($this->data)){
$this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
$sprawdz = $this->User->find('first',array('conditions'=>array('User.username'=>$this->request->data['User']['username'],'User.password'=>$this->request->data['User']['password'])));
echo debug($sprawdz);
if(!empty($sprawdz)){
if($this->Auth->login($this->request->data)){
$this->redirect('/');
echo debug('you are log in!') ;
}
}
else {
$this->User->invalidate('username', 'mistake!');
}
}
}
function logout(){
$this->Auth->logout();
$this->redirect('/');
}
}
User/login.ctp
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
<?php echo $this->Form->input('username');
echo $this->Form->input('password'); ?>
</fieldset>
<?php echo $this->Form->end(__('Log in')); ?>
It is like Auth does not work at all. In localhost was fine. Additionaly, users/index is empty page :)
Any ideas would be great. Thanks.
Keep it simple, and follow the tutorial which shows how to use the auth component.
For example, try this:
function login(){
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->Session->setFlash('You have successfully logged in the system');
return $this->redirect('/');
}
$this->Session->setFlash(__('Invalid username or password, try again [warning]'));
}
}
Also please make sure Security.Salt which is responsible to hash a password with the application's salt value
You did not defined any index function in usersController, that is why it is throwing error. Try to enable debug mode on so that you can track the errors
add $this->request->data on 2nd line of your login aciton-
function login(){
if(!empty($this->request->data)){
$this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
$sprawdz = $this->User->find('first',array('conditions'=>array('User.username'=>$this->request->data['User']['username'],'User.password'=>$this->request->data['User']['password'])));
echo debug($sprawdz);
if(!empty($sprawdz)){
if($this->Auth->login()){
$this->redirect('/');
echo debug('you are log in!') ;
}
}
else {
$this->Session->setFlash('Invalid username or password');
}
}
}
<div>//login.ctp
<?php echo $this->Form->create('Register'); ?>
<?php echo $this->Form->input('username'); ?>
<?php echo $this->Form->input('password'); ?>
<?php echo $this->Form->end('Login'); ?>
this is login page of my application.
If i give invalid details or valid details it logged in to home.please help hear..
public function login(){
if($this->Auth->loggedIn()){
$this->redirect(array('action' => 'home'));
}
if($this->request->is('post')){
if ($this->Auth->login($this->request->data)) {
$this->Session->write('Register',$this->request->data);
$this->redirect(array('controller' => 'Registers','action' => 'home'));
} else {
$this->Session->setFlash(__('Username or password is incorrect'));
}
}
}
this is RegistersController page..
change your form as below --
<?php echo $this->Form->create('Register'); ?>
<?php echo $this->Form->input('User.username'); ?>
<?php echo $this->Form->input('User.password'); ?>
<?php echo $this->Form->end('Login'); ?>
and check that in your AUTH component you have setted the model name to 'User' and fields mapped to Auth component settings -- and in your login function
$this->Session->write('Register',$this->request->data); this line is not needed any more.
go on below link for detail --
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
Here is the dropdown in a form:
echo $this->Form->input('customer_id', array('id'=>'initials','label'=>'Customer', 'value'=>$customers));
The $customers is a key-value array like this:
array(
(int) 2 => 'Best customer',
(int) 5 => 'Good customer',
(int) 9 => 'Customer')
I need one value to be pre selected once the dropdown is created. I have tried to select a default value as the int id and as the customer name but none of them worked.
Here is the controller edit function:
function edit($id = null) {
$this->Project->id=$id;
$this->Project->recursive = 0;
$customers = $this->Customer->find('list', array('conditions'=>array('company_id'=>$this->Auth->user('company_id'))));
$this -> set('customers', $customers);
if(!$this->Project->exists()){
throw new NotFoundException('Invalid project');
}
if($this->request->is('post')|| $this->request->is('put')){
if($this->Project->save($this->request->data)){
$this->Session->setFlash('The project has been edited');
$this->redirect(array('controller'=>'projects', 'action'=>'view', $id));
} else{
$this->Session->setFlash('The project could not be edited. Please, try again');
}
}else{
$this->request->data = $this->Project->read();
$this->request->data['Project']['customer_id'] = 'New customer';
}
}
And here is the edit form:
<?php echo $this->Form->create('Project');?>
<fieldset>
<legend><?php __('Edit Project'); ?></legend>
<?php
echo $this->Form->input('customer_id', array('id'=>'initials','label'=>'Customer', 'value'=>$customers));
echo $this->Form->input('name');
echo $this->Form->input('project_nr', array('type'=>'text', 'label'=>'Case number'));
echo $this->Form->input('address');
echo $this->Form->input('post_nr');
echo $this->Form->input('city');
echo $this->Form->input('start_date', array('label'=>'Start Date', 'class'=>'datepicker', 'type'=>'text'));
echo $this->Form->input('finish_date', array('label'=>'End Date', 'class'=>'datepicker', 'type'=>'text'));
echo $this->Form->input('company_id', array('value' => $current_user['company_id'], 'type'=>'text', 'type'=>'hidden'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
The best approach is to leverage the controller for this
if ($this->request->is('post')) {
$this->Model->create();
if ($this->Model->save($this->request->data)) {
...
} else {
...
}
} else {
/* Now here you can put your default values */
$this->request->data['Model']['field'] = ...;
}
For details see http://www.dereuromark.de/2010/06/23/working-with-forms/
i use this controller to login and logout users, and i want to display welcome message and the login username with logout link the problem is when i try to login this message apear to me
Notice (8): Undefined variable: results [APP\views\users\login.ctp, line 4]
users_controller.php
<?php
# /controllers/users_controller.php
# please note that not all code is shown...
uses('sanitize');
class UsersController extends AppController {
var $name = 'Users';
// Include the Email Component so we can send some out :)
var $components = array('Email','Auth','Recaptcha');
var $helpers = array('Recaptcha');
// Allow users to access the following action when not logged in
function beforeFilter () {
$this->Auth->allow('register','activate','logout','login');
$this->Auth->autoRedirect = false;
}
function login() {
// Check for incoming login request.
if ($this->data) {
// Use the AuthComponent's login action
if ($this->Auth->login($this->data)) {
// Retrieve user data
$results = $this->User->find(array('User.username' => $this->data['User']['username']), array('User.active'), null, false);
// Check to see if the User's account isn't active
if ($results['User']['active'] == 0) {
// Uh Oh!
$this->Session->setFlash('Your account has not been activated yet!');
$this->Auth->logout();
$this->data['User']['password'] = null;
//if not active user
}else {
$this->set('users', $results);
$this->redirect(array('controller' => 'users', 'action' => 'login'));
}
}
}
}
function logout() {
$this->redirect($this->Auth->logout());
}
users/login.ctp
<?php if ($this->Session->read('Auth.User')):?>
<?php
echo "Welcome".'<br />' ;
echo $results;
echo $html->link('logout', array('action'=>'logout'));
?>
<?php else : ?>
<div class="types form">
<?php echo $form->create('User');?>
<fieldset>
<legend><?php echo ('Please enter your username and password'); ?></legend>
<?php
echo $form->input('username');
echo $form->input('password');
?>
</fieldset>
<?php echo $form->end(('Login'));?>
</div>
<?php endif; ?>
You have to pass the data from your controller to the view by using the set method: $this->set('results', $results);. See also http://book.cakephp.org/view/977/Controller-Methods#Interacting-with-Views-978
AppController.php
function beforeFilter(){
$this->set('username', $this->_usersUsername());
}
function _usersUsername(){
$users_username = NULL;
if($this->Auth->user()){
$users_username = $this->Auth->user('username');
}
return $users_username;
}
view.ctp
<?php if(isset($username)) :?>
hello <?php echo $username; ?>! Welcome back.
<?php endif; ?>