0

我正在 Cakephp App 中实现身份验证。

在那个应用程序中,我按照本教程开始实施身份验证:简单的身份验证和授权应用程序,但是本教程需要发送验证电子邮件,不知道为什么。这是我的代码:

用户模型

<?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;
    }
}

应用控制器

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')
        )
    );


}

用户控制器

<?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

此解决方案需要电子邮件验证,但我想禁用电子邮件验证。如何?基本上,我需要对上述代码进行哪些更改才能拥有一个具有以下功能的简单身份验证系统:

  • 无需访问控制
  • 所有控制器和所有操作都需要 Auth
  • 通过用户名/密码进行身份验证。
  • 登录/注销/记住我。
4

1 回答 1

2

我想我做错了什么。我的用户插件位于Plugins目录中,并且可能已加载该插件,CakePlugin::loadAll()这就是这种有趣行为的原因。删除了该插件,现在它按预期工作。

故事的寓意: 如果蛋糕没有按照应有的方式行事,那是因为插件

于 2012-11-05T18:02:59.763 回答