0

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


}

ユーザーコントローラー

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

このソリューションにはメール認証が必要ですが、メール認証を無効にしたいと考えています。どのように?基本的に、次の機能を備えた単純な認証システムを作成するには、上記のコードにどのような変更を加える必要がありますか:

  • アクセス制御は不要
  • すべてのコントローラーとすべてのアクションには認証が必要です
  • ユーザー名/パスワードによる認証。
  • ログイン/ログアウト/覚えておいてください。
4

1 に答える 1

2

私は自分が間違っていたことを理解しました。ユーザープラグインがPluginsディレクトリにあり、おそらくそれがロードされCakePlugin::loadAll()たことが、この奇妙な動作の理由です。そのプラグインを削除すると、期待どおりに動作するようになりました。

教訓: Cake が本来の 動作をしない場合、それはプラグインのせいです

于 2012-11-05T18:02:59.763 に答える