CakePHP身份验证/登录'员工'而不是用户'

时间:2014-04-28 09:31:48

标签: cakephp authentication login controller

我试图让我的登录工作,但我似乎遇到了问题。有人可以帮忙吗?我使用'Employees'作为数据库的用户。下面是我的AppController,EmployeeController,Employee和login.ctp的代码:

App Controller:

class AppController extends Controller {

    public $components = array(
    'DebugKit.Toolbar',
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'employees', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'employees', 'action' => 'login'),
        'authError' => 'You must be logged in to view this page.',
        'loginError' => 'Invalid Username or Password entered, please try again.'

    ));

// only allow the login controllers only
public function beforeFilter() {
    $this->Auth->allow('login');
}
}

员工控制员:

class EmployeesController extends AppController {
//..other code
/**
 * Components
 *
 * @var array
 */
    //public $components = array('Paginator');
    public $paginate = array(
        'limit' => 25,
        'conditions' => array('status' => '1'),
        'order' => array('Employee.employee_username' => 'asc' ) 
    );

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

    }



    public function login() {

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

        // if we get the post information, try to authenticate
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->Session->setFlash(__('Welcome, '. $this->Auth->user('username')));
                $this->redirect($this->Auth->redirectUrl());
            } else {
                $this->Session->setFlash(__('Invalid username or password'));
            }
        } 
    }

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

/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->paginate = array(
            'limit' => 6,
            'order' => array('Employee.employee_username' => 'asc' )
        );
        $employees = $this->paginate('Employee');
        $this->set(compact('employees'));
    }

员工模型:

class Employee extends AppModel {
//..other code
function isUniqueUsername($check) {

        $username = $this->find(
            'first',
            array(
                'fields' => array(
                    'Employee.id',
                    'Employee.employee_username'
                ),
                'conditions' => array(
                    'Employee.employee_username' => $check['username']
                )
            )
        );

        if(!empty($username)){
            if($this->data[$this->alias]['id'] == $username['Employee']['id']){
                return true; 
            }else{
                return false; 
            }
        }else{
            return true; 
        }
    }

    /**
     * Before isUniqueEmail
     * @param array $options
     * @return boolean
     */
    function isUniqueEmail($check) {

        $email = $this->find(
            'first',
            array(
                'fields' => array(
                    'Employee.id'
                ),
                'conditions' => array(
                    'Employee.employee_email' => $check['email']
                )
            )
        );

        if(!empty($email)){
            if($this->data[$this->alias]['id'] == $email['Employee']['id']){
                return true; 
            }else{
                return false; 
            }
        }else{
            return true; 
        }
    }

    public function alphaNumericDashUnderscore($check) {
        // $data array is passed using the form field name as the key
        // have to extract the value to make the function generic
        $value = array_values($check);
        $value = $value[0];

        return preg_match('/^[a-zA-Z0-9_ \-]*$/', $value);
    }

    public function equaltofield($check,$otherfield) 
    { 
        //get name of field 
        $fname = ''; 
        foreach ($check as $key => $value){ 
            $fname = $key; 
            break; 
        } 
        return $this->data[$this->name][$otherfield] === $this->data[$this->name][$fname]; 
    } 

    /**
     * Before Save
     * @param array $options
     * @return boolean
     */
     public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = $passwordHasher->hash(
            $this->data[$this->alias]['password']
        );
    }
    // if we get a new password, hash it

        if (isset($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);
    //return true;
    }

}

登录页面:

<div class=“employees form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Employee'); ?>
    <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')); ?>
</div>

2 个答案:

答案 0 :(得分:0)

使用Employee表进行身份验证:

public $components = array(
        'Auth' => array(
                'authenticate' => array(
                        'Form' => array(
                                'fields' => array('username' => 'username'),
                                'userModel'=>'Employee'
                        )
                )
        )
);

答案 1 :(得分:0)

1.接受关于userModelfieldspasswordHasher的Auth组件的配置:

public $components = array(
    'DebugKit.Toolbar',
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'Employee', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'Employee', 'action' => 'login'),
        'authError' => 'You must be logged in to view this page.',
        'loginError' => 'Invalid Username or Password entered, please try again.',
        'authenticate' => array(
            'Form' => array(
            'fields' => array('username' => 'username', 'password' => 'password'),
            'userModel'=>'Employee',
            'passwordHasher' => 'name of your password hasher'
            ))
    ));

2.关于CakePHP的代码对话将控制器重命名为EmployeeController

3.在您的员工模型中,而不是isUniqueUsernameisUniqueEmail,您最好使用验证规则isUnique

4.使用相同的密码来创建密码和更新密码