我正在cakephp应用中实现身份验证。
在该应用程序中,我开始按照本教程:简单身份验证和授权应用程序来实现AUTH,但是该教程需要发送验证电子邮件,不确定为什么。这是我的代码:
用户模型:
<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
/**
* User Model
*
*/
class User extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'username';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'username' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'password' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
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;
}
}
AppController
class AppController extends Controller {
public $layout = 'bootstrap';
public $helpers = array(
'Session',
'Html' => array('className' => 'TwitterBootstrap.BootstrapHtml'),
'Form' => array('className' => 'TwitterBootstrap.BootstrapForm'),
'Paginator' => array('className' => 'TwitterBootstrap.BootstrapPaginator'),
'Time',
'Js'
);
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'reports', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home')
)
);
}
用户controller
<?php
App::uses('AppController', 'Controller');
/**
* Users Controller
*
* @property User $User
*/
class UsersController extends AppController {
/**
* Layout
*
* @var string
*/
public $layout = 'bootstrap';
/**
* Helpers
*
* @var array
*/
public $helpers = array('TwitterBootstrap.BootstrapHtml', 'TwitterBootstrap.BootstrapForm', 'TwitterBootstrap.BootstrapPaginator');
/**
* Components
*
* @var array
*/
public $components = array('Session');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add', 'logout');
}
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());
}
/**
* index method
*
* @return void
*/
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
/**
* view method
*
* @param string $id
* @return void
*/
public function view($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid %s', __('user')));
}
$this->set('user', $this->User->read(null, $id));
}
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(
__('The %s has been saved', __('user')),
'alert',
array(
'plugin' => 'TwitterBootstrap',
'class' => 'alert-success'
)
);
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(
__('The %s could not be saved. Please, try again.', __('user')),
'alert',
array(
'plugin' => 'TwitterBootstrap',
'class' => 'alert-error'
)
);
}
}
}
/**
* edit method
*
* @param string $id
* @return void
*/
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid %s', __('user')));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(
__('The %s has been saved', __('user')),
'alert',
array(
'plugin' => 'TwitterBootstrap',
'class' => 'alert-success'
)
);
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(
__('The %s could not be saved. Please, try again.', __('user')),
'alert',
array(
'plugin' => 'TwitterBootstrap',
'class' => 'alert-error'
)
);
}
} else {
$this->request->data = $this->User->read(null, $id);
}
}
/**
* delete method
*
* @param string $id
* @return void
*/
public function delete($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid %s', __('user')));
}
if ($this->User->delete()) {
$this->Session->setFlash(
__('The %s deleted', __('user')),
'alert',
array(
'plugin' => 'TwitterBootstrap',
'class' => 'alert-success'
)
);
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(
__('The %s was not deleted', __('user')),
'alert',
array(
'plugin' => 'TwitterBootstrap',
'class' => 'alert-error'
)
);
$this->redirect(array('action' => 'index'));
}
}
表结构:
id int(10)
username varchar(50)
password varchar(50)
email varchar(60)
email_verified varchar(70
email_token_expires date
slug varchar(40)
created datetime
modified datetime
此解决方案需要电子邮件验证,但我想禁用电子邮件验证。如何?基本上,我需要对上述代码进行所有更改,以具有具有以下功能的简单验证系统:
- 无需访问控制
- 所有控制器&amp;所有动作都需要身份
- 通过用户名/密码验证。
- 登录/注销/记住我。
我弄清楚了我在做什么错。我有位于Plugins
目录中的用户插件&amp;那可能被CakePlugin::loadAll()
加载了,这就是这种有趣的行为的原因。 删除该插件&amp;现在它可以按预期工作。
故事的道德:如果蛋糕没有按照方式行为,它将是因为插件