0
votes

I got this example from the cakebook and it's not working and it's the case of many people. The problem here is that this passes the login whatever data I sent. I mean i just a have a record in my database username: ric and password: 123.

if I submit in the form asdasdasd/asdasdasdasd it just passes as if this were registered. My model:

<?php
    App::uses('AuthComponent', 'Controller/Component');

    class User extends AppModel {
        public $name = 'Users';
        public $useTable = "rma_web_users";

        public $validate = array(
            'username' => array(
                'required' => array(
                    'rule' => array('notEmpty'),
                    'message' => 'A username is required'
                )
            ),
            'password' => array(
                'required' => array(
                    'rule' => array('notEmpty'),
                    'message' => 'A password is required'
                )
            )
        );
    }
?>

my controller:

<?php

class UsersController extends AppController {

    public $name = 'Users';
    public $uses = array('User');

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add');
    }

    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }
}
?>

my appController:

class AppController extends Controller {
    public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'home', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'home', 'action' => 'index')
        )
    );

    public function beforeFilter() {
        $this->Auth->allow('index', 'view');
    }
}

and my login view:

<div class="users_login">
    <p class='rma_login gray_label'>RMA Login System</p>
    <div class="form_container">
        <?php echo $this->Session->flash('auth'); ?>
        <?php echo $this->Form->create('User'); ?>
        <?php
            echo $this->Form->input('username',array(
                'label' => false,
                'type' => 'text',
                'value' =>'Email',
                'class' => 'entrada',
                'onFocus' => "if(this.value=='Email') this.value=''",
                'onBlur' => "if(this.value=='') this.value='Email'")
            );
            echo $this->Form->input('password',array(
                'label' => false,
                'type' => 'password',
                'class' => 'entrada')
            );
        ?>
        <?php
        $submit = array(
            'label' => 'Log in',
            'class' => array( 'boton_azul', 'login')
        );
        echo $this->Form->end($submit); ?>
    </div>
</div>

Thank you so much.

1

1 Answers

1
votes

Try this:

Delete this lines from Model

App::uses('AuthComponent', 'Controller/Component');
public $name = 'Users';

Delete this from Controller

public $name = 'Users';
public $uses = array('User');