@blog.justoneplanet.info

日々勉強

CakePHP2系で認証のユーザーモデルを変更する

ログアウト後のログインが上手くできなくてハマったのでメモしておく。

class AppController extends Controller {
    public $components = array(
        'Auth' => array(
            'loginRedirect' => array('controller' => 'hoge', 'action' => 'index'),
            'loginAction'   => array('controller' => 'hoge', 'action' => 'login'),
            'authenticate'  => array(
                'Form' => array(
                    'userModel' => 'Hoge',
                    'fields' => array(
                        'username' => 'email',
                    ),
                    'scope' => array(
                        'Hoge.is_public' => 1,
                    ),
                ),
            ),
        ),
        'Session'
    );

    public function beforeFilter() {
        parent::beforeFilter();
    }
}
class HogeController extends AppController {
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow(array('login', 'logout', 'fuga'));// loginとlogoutも記述しておく
    }
    public function login() {
        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());
    }
}

コメントはまだありません»

No comments yet.

RSS feed for comments on this post.TrackBack URL

Leave a comment