CakePHP3でのログイン機能はID/Passwordの認証が簡単に実装できます。
一方で、ASPサービスなどで企業コード+ID+Passwordの認証が必要な場合の実装例が見つからなかったのでサンプルを公開します。
CakePHP3でのログイン機能はID/Passwordの認証が以下のように簡単に実装できます。
src/Controller/AppController.php
class AppController extends Controller
{
public function initialize()
{
parent::initialize();$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [ // Authコンポーネントの読み込み
'authorize' => 'Controller',
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
],
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password',
],
'scope' => [
'status' => 1
]
]
],
'loginRedirect' => [ // ログイン後に遷移するアクションを指定
'controller' => 'Pages',
'action' => 'display'
],
'logoutRedirect' => [ // ログアウト後に遷移するアクションを指定
'controller' => 'Users',
'action' => 'login',
],
'authError' => 'ログインできませんでした。ログインしてください。', // ログインに失敗したときのFlashメッセージを指定(省略可)
]);
}}
src/Controller/UsrsController.php
class UsersController extends AppController
{
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error(__('Invalid username or password, try again'));
}
}
}
public function logout()
{
$this->request->session()->destroy();
return $this->redirect($this->Auth->logout());
}
}
src/Template/Users/login.ctp
<?= $this->Form->create(); ?>
<fieldset>
<legend><?= __('Login User') ?></legend>
<?php
echo $this->Form->input('email');
echo $this->Form->input('password');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
CakePHP3のインストール・セットアップの方法をググれば大抵ここまでは紹介されています。
ここに、企業コードを追加して3項目での認証にしてみます。
src/Template/Users/login.ctp
<?= $this->Form->create(); ?>
<fieldset>
<legend><?= __('Login User') ?></legend>
<?php
echo $this->Form->input('organization');
echo $this->Form->input('email');
echo $this->Form->input('password');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
src/Controller/UsrsController.php
public function login()
{
if ($this->request->is('post')) {
$organization = empty($this->request->data['organization']) ? 0 : $this->request->data['organization'];
$this->Auth->config('authenticate.Form.scope.organization_id', $organization, true);
$this->Auth->constructAuthenticate();
$user = $this->Auth->identify();
if ($user) {$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error(__('Invalid username or password, try again'));
}
}
}
src/Controller/AppController.php
$this->loadComponent('Auth', [ // Authコンポーネントの読み込み
'authorize' => 'Controller',
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
],
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password',
],
'scope' => [
'organization_id' => 0,
'status' => 1
]
]
],
'loginRedirect' => [ // ログイン後に遷移するアクションを指定
'controller' => 'Pages',
'action' => 'display'
],
'logoutRedirect' => [ // ログアウト後に遷移するアクションを指定
'controller' => 'Users',
'action' => 'login',
],
'authError' => 'ログインできませんでした。ログインしてください。', // ログインに失敗したときのFlashメッセージを指定(省略可)
]);
デフォルトの指定だと、organization_idを無視してワイルドカード状態になってしまうので、AppControllerのscope指定や、UsrsControllerでPOST内容を取り出す際のデフォルト値には存在しない企業コード(この例では企業コード0は存在しないID)を設定しておきます。
たったこれだけの処理なのですが、なぜか実装例やQ&Aが見つからなかったので、$this->Auth->constructAuthenticate();の部分でちょっと苦戦してしまいました。。。