CakePHPのACL(Access Control List)の解説を砕いてみる~アプリケーションの準備~



CakePHPのACL(Access Control List)の解説を砕いてみる~ACL を制御するシンプルなアプリケーション~の続き。

ここでやるべきこと。

  1. 最新版のCakePHPをダウンロード
  2. database.php をセットアップ
  3. app/config/core.php の Security.salt の値を変更
  4. アプリ用のテーブル作成
  5. Bakeを使用してモデル・コントローラ・ビューを作成する


123.はすでに済んでいるという過程で、4.5.を行ってみる。

以下のsqlを実行するらしいが、自分なりにコメントを入れてみる。

とりあえずユーザを管理する為のテーブルを作る。

/*ユーザを管理する為のテーブル*/
CREATE TABLE users (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,  -- ユーザID
    username VARCHAR(255) NOT NULL UNIQUE,  -- 外から見たユーザIDとなるもの
    password CHAR(40) NOT NULL,  -- パスワード
    group_id INT(11) NOT NULL,  -- グループID
    created DATETIME,  -- 作成日付
    modified DATETIME -- 更新日付
);

次にユーザが所属するグループを管理するテーブルを作る。

/*グループを管理するテーブル*/
CREATE TABLE groups (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,    -- グループID
    name VARCHAR(100) NOT NULL,   -- グループ名
    created DATETIME,   -- 作成日付
    modified DATETIME    -- 更新日付
);

「ロールと権限」*1みたいな関係になるんだろうか?



で、以下はテスト用?の為のテーブル(だと思っている。。。)

CREATE TABLE posts (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    user_id INT(11) NOT NULL,
    title VARCHAR(255) NOT NULL,
    body TEXT,
    created DATETIME,
    modified DATETIME
);

CREATE TABLE widgets (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    part_no VARCHAR(12),
    quantity INT(11)
);


とりあえず上記SQLを実行して次に、Bake.phpを実行して、まずModelを作成してみる。

$> php ~/www/1.2.x.x/cake/console/cake.php bake -app ~/www/1.2.x.x/app

Welcome to CakePHP v1.2.0.7692 RC3 Console
---------------------------------------------------------------
App : app
Path: ~/www/1.2.x.x/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: ~/www/1.2.x.x/app/models/
---------------------------------------------------------------
Use Database Config: (default/test)
[default] > default
Possible Models based on your current database:
1. Group
2. Post
3. User
4. Widget
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] > y
One moment while the associations are detected.
---------------------------------------------------------------
Please confirm the following associations:
---------------------------------------------------------------
Group hasMany User? (y/n)
[y] > y
Group hasOne User? (y/n)
[y] > y
Would you like to define some additional model associations? (y/n)
[n] > n

---------------------------------------------------------------
The following Model will be created:
---------------------------------------------------------------
Name:       Group
Associations:
                        Group hasOne    User
                        Group hasMany   User
---------------------------------------------------------------
Look okay? (y/n)
[y] > y

Baking model class for Group...

Creating file ~/www/1.2.x.x/app/models/group.php
Wrote ~/www/1.2.x.x/app/models/group.php
Cake test suite not installed.  Do you want to bake unit test files anyway? (y/n)
[y] > y

You can download the Cake test suite from http://cakeforge.org/projects/testsuite/

Baking test fixture for Group...

Creating file ~/www/1.2.x.x/app/tests/fixtures/group_fixture.php
File exists, overwrite? ~/www/1.2.x.x/app/tests/fixtures/group_fixture.php (y/n/q)
[n] > n
Skip ~/www/1.2.x.x/app/tests/fixtures/group_fixture.php

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

同様にPost・User・Widgetも作成する。
「Would you like to supply validation criteria for the fields in your model? (y/n)」(バリデーション関係)
の質問のみ「n」で返事してみた。

次に、Controllerの作成。

$> php ~/www/1.2.x.x/cake/console/cake.php bake -app ~/www/1.2.x.x/app

Welcome to CakePHP v1.2.0.7692 RC3 Console
---------------------------------------------------------------
App : app
Path: ~/www/1.2.x.x/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)
> c
---------------------------------------------------------------
Bake Controller
Path: ~/www/1.2.x.x/app/controllers/
---------------------------------------------------------------
Possible Controllers based on your current database:
1. Group
2. Post
3. User
4. Widget
Enter a number from the list above, type in the name of another controller, or 'q' to exit
[q] > 1
---------------------------------------------------------------
Baking GroupsController
---------------------------------------------------------------
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] > n
Would you like to use Sessions? (y/n)
[y] > n

---------------------------------------------------------------
The following controller will be created:
---------------------------------------------------------------
Controller Name:  Groups
---------------------------------------------------------------
Look okay? (y/n)
[y] > y

Creating file /~/www/1.2.x.x/app/controllers/groups_controller.php
Wrote ~/www/1.2.x.x/app/controllers/groups_controller.php
Cake test suite not installed.  Do you want to bake unit test files anyway? (y/n)
[y] > y

You can download the Cake test suite from http://cakeforge.org/projects/testsuite/

Baking unit test for Groups...

Creating file ~/www/1.2.x.x/app/tests/cases/controllers/groups_controller.test.php
File exists, overwrite? ~/www/1.2.x.x/app/tests/cases/controllers/groups_controller.test.php (y/n/q)
[n] > n
Skip ~/www/1.2.x.x/app/tests/cases/controllers/groups_controller.test.php

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

「Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)」がyで、
「Would you like to use Sessions? (y/n)」がn。

同様にPost・User・Widgetも作成する。


最後にViewを作成。

$> php ~/www/1.2.x.x/cake/console/cake.php bake -app ~/www/1.2.x.x/app

Welcome to CakePHP v1.2.0.7692 RC3 Console
---------------------------------------------------------------
App : app
Path: ~/www/1.2.x.x/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)
> v
---------------------------------------------------------------
Bake View
Path: ~/www/1.2.x.x/app/views/
---------------------------------------------------------------
Possible Controllers based on your current database:
1. Group
2. Post
3. User
4. Widget
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 ~/www/1.2.x.x/app/views/groups/index.ctp
Wrote ~/www/1.2.x.x/app/views/groups/index.ctp

Creating file ~/www/1.2.x.x/app/views/groups/view.ctp
Wrote ~/www/1.2.x.x/app/views/groups/view.ctp

Creating file ~/www/1.2.x.x/app/views/groups/add.ctp
Wrote ~/www/1.2.x.x/app/views/groups/add.ctp

Creating file ~/www/1.2.x.x/app/views/groups/edit.ctp
Wrote ~/www/1.2.x.x/app/views/groups/edit.ctp
---------------------------------------------------------------

View Scaffolding Complete.

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

「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)」がy
同様にPost・User・Widgetも作成する。

次のページはまた今度(ぁ)
次のページへ進む。