【CakePHP】OpenIDをAuthComponentにトッピングしてみる を参考にcakePHPでOpenID対応サイトを作ってみた。
詳しい説明は上記サイトに記載があるので、説明が省略されているところを中心に記載してみたい。
1.
cakePHPをダウンロードする。まだ開発途中である1.2をダウンロード。
2.
コンテンツを展開したいフォルダに解凍して、config にある database.php.default を database.php に変更して内容を編集する。
3.
chmod -R 777 app/tmp/
で権限付与を行う。
4.
app/config/core.php にある
Configure::write(‘Security.salt’, ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’);
のXXの部分をランダムな値に変更する。
5.
以下のSQLを実行してテーブルを作成する。
CREATE TABLE `users` (
`id` int(10) NOT NULL auto_increment,
`nickname` varchar(50) NOT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`flag` int(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
6.
OpenID component for CakePHP
を参考にしてOpenIDコンポーネントとPHP Open ID Library、EAUT libraryをダウンロードして設定する。
7.
cake/console/cake bake
を実行する
Welcome to CakePHP v1.2.0.7692 RC3 Console
—————————————————————
App : app
Path: /(pathname)/app
—————————————————————
Interactive Bake Shell
—————————————————————
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
> M
—————————————————————
Bake Model
Path: /(pathname)/app/models/
—————————————————————
Possible Models based on your current database:
1. User
Enter a number from the list above, type in the name of another model, or ‘q’ to exit
[q] >1
Would you like to supply validation criteria for the fields in your model? (y/n)
[y] > n
Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)? (y/n)
[y] > n
—————————————————————
The following Model will be created:
—————————————————————
Name: User
Associations:
—————————————————————
Look okay? (y/n)
[y] > y
Baking model class for User…
Creating file /(pathname)/app/models/user.php
Wrote /(pathname)/app/models/user.php
Cake test suite not installed. Do you want to bake unit test files anyway? (y/n)
[y] > n
—————————————————————
Interactive Bake Shell
—————————————————————
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
> C
—————————————————————
Bake Controller
Path: /(pathname)/app/controllers/
—————————————————————
Possible Controllers based on your current database:
1. Users
Enter a number from the list above, type in the name of another controller, or ‘q’ to exit
[q] > 1
—————————————————————
Baking UsersController
—————————————————————
Would you like to build your controller interactively? (y/n)
[y] > y
Would you like to use scaffolding? (y/n)
[n] > n
Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)
[n] > y
Would you like to create the methods for admin routing? (y/n)
[n] > n
Would you like this controller to use other helpers besides HtmlHelper and FormHelper? (y/n)
[n] > n
Would you like this controller to use any components? (y/n)
[n] > y
Please provide a comma separated list of the component names you’d like to use.
Example: ‘Acl, Security, RequestHandler’
> Auth,Openid
Would you like to use Sessions? (y/n)
[y] > y
—————————————————————
The following controller will be created:
—————————————————————
Controller Name: Users
Components: Auth, Openid
—————————————————————
Look okay? (y/n)
[y] > y
Creating file /(pathname)/app/controllers/users_controller.php
Wrote /(pathname)/app/controllers/users_controller.php
Cake test suite not installed. Do you want to bake unit test files anyway? (y/n)
[y] > n
—————————————————————
Interactive Bake Shell
—————————————————————
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
> V
—————————————————————
Bake View
Path: /(pathname)/app/views/
—————————————————————
Possible Controllers based on your current database:
1. Users
Enter a number from the list above, type in the name of another controller, or ‘q’ to exit
[q] > 1
Would you like to create some scaffolded views (index, add, view, edit) for this controller?
NOTE: Before doing so, you’ll need to create your controller and model classes (including associated models). (y/n)
[n] > y
Would you like to create the views for admin routing? (y/n)
[y] > n
Creating file /(pathname)/app/views/users/index.ctp
Wrote /(pathname)/app/views/users/index.ctp
Creating file /(pathname)/app/views/users/view.ctp
Wrote /(pathname)/app/views/users/view.ctp
Creating file /(pathname)/app/views/users/add.ctp
Wrote /(pathname)/app/views/users/add.ctp
Creating file /(pathname)/app/views/users/edit.ctp
Wrote /(pathname)/app/views/users/edit.ctp
—————————————————————
View Scaffolding Complete.
app/views/users/login.ctpを追加して以下のように記述する。
<?php
if (isset($message)) {
echo ‘‘.$message.’
‘;
}
echo $form->create(‘User’, array(‘type’ => ‘post’, ‘action’ => ‘login’));
echo $form->input(‘OpenidUrl.openid’, array(‘label’ => false));
echo $form->end(‘Login’);
?>
8.
以降のコードは【CakePHP】OpenIDをAuthComponentにトッピングしてみる を参考にするとできる。
【動作確認】
http://(cakePHPを配置したパスに到達するためのURL)/users/login
でログインページを出すことができるので、たとえば、
mixi.jp
yahoo.co.jp
www.google.com/accounts/o8/id
などを入力するとそれぞれのサイトのOpenID認証サイトに遷移して、そこで認証がOKだとニックネーム登録画面に遷移して、登録を続けるとDBにユーザー情報が登録できる。
http://(cakePHPを配置したパスに到達するためのURL)/users/
とアクセスすると登録状況を照会できる。
ユーザー認証の画面を作るのは結構面倒だけれど、cakecPHPを使えばここまで簡単にできる。