2012-11-05 92 views
0

我在Cakephp應用程序中實現身份驗證。爲什麼Cakephp中的簡單身份驗證需要驗證郵件?

在該應用程序中,我開始通過以下教程實現身份驗證:Simple Authentication and Authorization Application但本教程需要驗證電子郵件才能發送,不知道原因。這裏是我的代碼:

用戶模型

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


} 

UsersController

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

這種解決方案需要電子郵件驗證,但我想禁用電子郵件驗證。怎麼樣?基本上,我需要對上述代碼的所有變化有一個簡單的身份驗證系統具有以下特點:

  • 沒有訪問控制要求
  • 所有控制器&所有操作通過用戶名/密碼需要驗證
  • 驗證。
  • 登錄/註銷/記住我。

回答

1

我想我做錯了什麼。我有用戶插件位於Plugins目錄&那可能會加載CakePlugin::loadAll()這是這種有趣的行爲的原因。 刪除該插件&現在它按預期工作。

道德故事: 如果蛋糕不行爲的方式應該,它會因爲插件