0
votes

I have tried just about everything now and I'm still having problems with my Auth Setup. I using Employee as my model with the fields employee_id and password. Blowfish is my passwordhasher and Employees/index in my Controller/action.

I'am trying to echo $this->Auth->login() but its not responding.

Output from $this->request->data

Array ( [Employee] => Array ( [employee_id] => bob [password] => temp ) )

Employee Model

    <?php

    class Employee extends AppModel {

        public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }

        public $primaryKey = 'employee_id';

        public $hasMany = array(
            'CustomerInteraction' => array(
                'foreignKey' => 'created_by'
            ),
            'Appointment' => array(
                'foreignKey' => 'assigned_to'
            ),
            'Customer' => array(
                'className' => 'Customer',
                'foreignKey' => false
            )
        );

        public $hasOne = array(
            'SalesTarget' => array(
                'foreignKey' => 'subject'
            )
        );

        public $virtualFields = array(
            'full_name' => 'CONCAT(Employee.first_name, " ", Employee.last_name)'
        );

    }

    ?>

AppController

public $components = array(
        'Session',
        'Auth' => array(
            'flashElement' => 'login_error',
            'authError' => 'You must be logged in to view this page.',
            'loginError' => 'Invalid Username or Password entered, please try again.',
            'loginAction' => array(
                'controller' => 'Employees',
                'action' => 'index',
            ),
            'authenticate' => array(
                'all' => array(
                    'userModel' => 'Employee',
                    'passwordHasher' => 'Blowfish'
                ),
                'Form' => array(
                    'userModel' => 'Employee',
                    'passwordHasher' => 'Blowfish',
                    'fields' => array(
                        'username' => 'employee_id',
                        'password' => 'password',
                    )
                )
            )   
        ),
    );

    public $uses = array(
        'ProgramApplication',
        'Employee'
    );

    public function beforeFilter() {
        parent::beforeFilter();

        $this->Auth->fields = array(
            'username' => 'employee_id',
            'password' => 'password'
        );

EmployeesController

<?php

class EmployeesController extends AppController {

    public $uses = array(
        'TimeEntry',
        'Employee',
        'CakeEmail',
        'Network/Email' ,
        'CraigslistTemplate',
        'Inventory'
    );

    public $helpers = array(
        'DateConversion'
    );


    public function beforeFilter() {

        parent::beforeFilter();
        $this->Auth->allow('index','checkUsername','passwordStatus','passwordCheck','convertPassword');
    }



    public function index() {

        //Set layout to loginr
        $this->layout = 'login';

         //if already logged-in, redirect
        if($this->Session->check('Auth.User')){
            return $this->redirect(
            array('controller' => 'Search', 'action' => 'index'));
        }

        echo "1";
        if($this->request->is('post')) {

            echo $this->Auth->login();

            if($this->passwordCheck($this->request->data['Employee']['employee_id'],$this->request->data['Employee']['password'])) {

            }else{
                echo "Invalid username/password";
            }
        }

Index.ctp

<div class="Employees form">
<?php //echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Employee',array('controller' => 'Employees','type' => 'POST')); ?>
    <fieldset>
        <legend>
            <?php echo __('Please enter your username and password'); ?>
        </legend>
        <?php echo $this->Form->input('employee_id',array('type' => 'Username'));
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
1

1 Answers

1
votes

I made the same setup you have and got an error related to blowfish.

Removing blowfish works ('passwordHasher' => 'Blowfish'). I am able to login.

I also used a dummy edit function that updates an user:

public function edit() {
    $data = array(
        'employee_id' => '111',
        'password' => 'test',
    );
    $res = $this->Employee->save($data);
}

After running this function, I was able to login with 111/test, without blowfish.

Is blowfish a requirement for the project? Can you use normal passwords?