CakePHP - 可以添加用户,但用户无法登录()

时间:2016-08-05 10:37:43

标签: php authentication cakephp login

我可以很好地添加用户,并且它们会显示在数据库中,但是当我在登录表单中输入用户名和密码时,我会收到“用户名和密码无效,请重试”。我已经进行了设置,以便在尝试编辑或向页面添加帖子时以及单击“登录”链接时询问登录详细信息。

用户模型是User.php:

class User extends AppModel {
public $validate = array(
    'username' => array(
        'required' => array(
            'rule' => 'notBlank',
            'message' => 'A username is required'
        )
    ),
    'password' => array(
        'required' => array(
            'rule' => 'notBlank',
            'message' => 'A password is required'
        )
    ),
    'role' => array(
        'valid' => array(
            'rule' => array('inList', array('admin', 'author')),
            'message' => 'Please enter a valid role',
            'allowEmpty' => false
        )
    )
);

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

UsersController.php:

App::uses('AppController', 'Controller');

class UsersController extends AppController {

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

public function login() {
    if ($this -> request -> is('post')) {
        if ($this -> Auth -> login()) {
            return $this -> redirect($this -> Auth -> redirectUrl());
        }
        $this -> Flash -> error(__('Invalid username or password, try again'));
    }
}

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

public function index() {
    $this -> User -> recursive = 0;
    $this -> set('users', $this -> paginate());
}

public function view($id = null) {
    $this -> User -> id = $id;
    if (!$this -> User -> exists()) {
        throw new NotFoundException(__('Invalid user'));
    }
    $this -> set('user', $this -> User -> findById($id));
}

public function add() {
    if ($this -> request -> is('post')) {
        $this -> User -> create();
        if ($this -> User -> save($this -> request -> data)) {
            $this -> Flash -> success(__('The user has been saved'));
            return $this -> redirect(array('action' => 'index'));
        }
        $this -> Flash -> error(
            __('The user could not be saved. Please, try again.')
        );
    }
}

public function edit($id = null) {
    $this -> User -> id = $id;
    if (!$this -> User -> exists()) {
        throw new NotFoundException(__('Invalid user'));
    }
    if ($this -> request -> is('post') || $this -> request -> is('put')) {
        if ($this -> User -> save($this -> request -> data)) {
            $this -> Flash -> success(__('The user has been saved'));
            return $this -> redirect(array('action' => 'index'));
        }
        $this -> Flash -> error(
            __('This user could not be saved. Please, try again.')
        );
    } else {
        $this -> request -> data = $this -> User - findById($id);
        unset($this -> request -> data['User']['password']);
    }
}

public function delete($id = null) {

    $this -> request -> allowMethod('post');

    $this -> User -> id = $id;
    if (!$this -> User -> exists()) {
        throw new NotFoundException(__('Invalid user'));
    }
    if ($this -> User -> delete()) {
        $this -> Flash -> success(__('User deleted'));
        return $this -> redirect(array('action' => 'index'));
    }
    $this -> Flash -> error(__('User was not deleted.'));
    return $this -> redirect(array('action' => 'index'));
}

AppController.php:

class AppController extends Controller {

public $components = array(
    'DebugKit.Toolbar',
    'Flash',
    'Auth' => array(
        'loginRedirect' => array(
            'controller' => 'posts',
            'action' => 'index'
        ),
        'logoutRedirect' => array(
            'controller' => 'pages',
            'action' => 'display',
            'home'
        ),
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish'
            )
        ),
        'authorize' => array('Controller')
    )
);

public function isAuthorized($user) {
    if (isset($user['role']) && $user['role'] === 'admin') {
        return true;
    }

    return false;
}

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

login.ctp:

<div class="users form">
    <?php echo $this -> Flash -> render('auth'); ?>
    <?php echo $this -> Form -> create('user'); ?>
        <fieldset>
            <legend>
                <?php echo __('Please enter you username and password'); ?>
            </legend>
            <?php
                echo $this -> Form -> input('username');
                echo $this -> Form -> input ('password');
            ?>
        </fieldset>
    <?php echo $this -> Form -> end(__('Login')); ?>
</div>

密码好像很好。我看了一些类似的问题,但似乎都没有解决我的问题。版本是CakePHP 2.8.5。 我无法在编码中发现错误。为什么它总是给我“无效的用户名和密码,请再试一次。”?

1 个答案:

答案 0 :(得分:-1)

更改此代码并尝试为您工作

'authorize' => array('Controller')将此替换为'authorize' => array('User')
 并if (isset($user['role']) && $user['role'] === 'admin')替换为if (isset($user['role']) && $user['role'] === 'admin' || $user['role'] === 'author')

感谢