いろいろ、理由があって、データベースはPostgreSQL。
認証ライブラリを試してみる。いろいろなサイトで見た感じでは、Dx_Authが良いらしいので、以下の環境で構築した。
* CentOS 5.5 (final)
* PHP 5.3.8
* CodeIgniter 2.0.3
* Dx_Auth
* PostgreSQL 8.1
実際の作業は試行錯誤の連続だったのだが、記録なのでサクッと書いておく。
Postgresqlをインストール
PHPはremiリポジトリを使って最新バージョン5.3.8をインストールしたため、ドライバも対応したものをremiを使ってインストール。# yum install postgresql-server.x86_64 postgresql-contrib.x86_64 postgresql-devel.x86_64 # yum install php-pgsql.x86_64 --enablerepo=remi # /etc/init.d/httpd restart # テスト用のデータベースも作成 [root@] su - postgres -bash-3.2$ createdb -E UNICODE -O testman -U postgres cidb
CodeIgniter 2.x
CodeIgniterは、codeigniter 2.0.3 と 日本語パッケージ を導入した。基本的にはダウンロードして、解凍して、ドキュメントルートに持ってきて、設定を少しいじるだけ。簡単なので、以下を参考してください。
http://codeigniter.jp/
Dx Authのインストール
ここを参考にhttp://dexcell.shinsengumiteam.com/dx_auth/
ソースは、CodeIgniter 1.7 で検証されているそうだが、CodeIgniterは、2.xになっていろいろと変わった。修正の方法は、以下のサイトを参考にさせていただいた。
http://d.hatena.ne.jp/ozawa34/20091223/1261581994
日本語化やCI 2.x系の修正済みソースを提供されている方もいらしたので参考に
http://d.hatena.ne.jp/ozawa34/20091223/1261581994
PostgreSQL用にスキーマを変更
MySQLで試すと、あっさりと動作したので、いよいよPostgreSQLへ。application/config/database.php のドライバ設定部分を"postgre"に変更。
//$db['default']['dbdriver'] = 'mysql'; $db['default']['dbdriver'] = 'postgre';
schema.sqlがDxAuthについているが、もちろんPostgreSQLでは、使えません。
フォーラムの以下のスレッドを参考に、PostgreSQL用のスキーマ作成SQL文をつくってみました。
http://codeigniter.com/forums/viewthread/186278/
- -- --------------------------------------------------------
- --
- -- Table structure for table ci_sessions
- --
- CREATE TABLE ci_sessions (
- session_id varchar(40) NOT NULL DEFAULT '0',
- ip_address varchar(16) NOT NULL DEFAULT '0',
- user_agent varchar(150) NOT NULL,
- last_activity integer NOT NULL DEFAULT '0',
- user_data text,
- PRIMARY KEY (session_id)
- );
- -- --------------------------------------------------------
- --
- -- Table structure for table login_attempts
- --
- CREATE TABLE login_attempts (
- id serial,
- ip_address varchar(40) NOT NULL,
- time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- primary key(id)
- );
- -- --------------------------------------------------------
- --
- -- roles
- --
- create table roles(
- id serial,
- parent_id integer not null default '0',
- name varchar(30) not null,
- primary key(id)
- );
- INSERT INTO roles (parent_id, name) VALUES(0, 'User');
- INSERT INTO roles (parent_id, name) VALUES(0, 'Admin');
- -- --------------------------------------------------------
- --
- -- persissions
- --
- create table permissions(
- id serial,
- role_id integer not null,
- data text,
- primary key(id)
- );
- -- --------------------------------------------------------
- --
- -- Table structure for table user_autologin
- --
- CREATE TABLE user_autologin (
- key_id char(32) NOT NULL,
- user_id integer NOT NULL DEFAULT '0',
- user_agent varchar(150) NOT NULL,
- last_ip varchar(40) NOT NULL,
- last_login timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (key_id,user_id)
- );
- -- --------------------------------------------------------
- --
- -- Table structure for table user_profiles
- --
- CREATE TABLE user_profile (
- id serial,
- user_id integer NOT NULL,
- country varchar(20) DEFAULT NULL,
- website varchar(255) DEFAULT NULL,
- primary key(id)
- );
- -- --------------------------------------------------------
- --
- -- Table structure for table users
- --
- CREATE TABLE users (
- id serial,
- role_id integer not null default '1',
- username varchar(25) NOT NULL,
- password varchar(34) NOT NULL,
- email varchar(100) NOT NULL,
- activated smallint NOT NULL DEFAULT '1',
- banned smallint NOT NULL DEFAULT '0',
- ban_reason varchar(255) DEFAULT NULL,
- newpass varchar(34) default null,
- newpass_key varchar(32) default null,
- newpass_time timestamp DEFAULT CURRENT_TIMESTAMP,
- last_ip varchar(40) NOT NULL,
- last_login timestamp DEFAULT CURRENT_TIMESTAMP,
- created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- primary key(id)
- );
- -- --------------------------------------------------------
- --
- -- user_temp
- --
- create table user_temp (
- id serial,
- username varchar(255) not null,
- password varchar(34) not null,
- email varchar(100) not null,
- activation_key varchar(50) not null,
- last_ip varchar(40) NOT NULL,
- created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- primary key(id)
- );
- -- --------------------------------------------------------
- --
- -- Update datetime columns on update
- --
- CREATE OR REPLACE FUNCTION update_modified_column_time()
- RETURNS TRIGGER AS $$
- BEGIN
- NEW.time = now();
- RETURN NEW;
- END;
- $$ LANGUAGE 'plpgsql';
- CREATE TRIGGER update_login_attempts_time BEFORE UPDATE
- ON login_attempts FOR EACH ROW EXECUTE PROCEDURE
- update_modified_column_time();
- CREATE OR REPLACE FUNCTION update_modified_column_user_autologin()
- RETURNS TRIGGER AS $$
- BEGIN
- NEW.last_login = now();
- RETURN NEW;
- END;
- $$ LANGUAGE 'plpgsql';
- CREATE TRIGGER update_login_attempts_user_autologin BEFORE UPDATE
- ON user_autologin FOR EACH ROW EXECUTE PROCEDURE
- update_modified_column_user_autologin();
- CREATE OR REPLACE FUNCTION update_modified_column_users()
- RETURNS TRIGGER AS $$
- BEGIN
- NEW.modified = now();
- RETURN NEW;
- END;
- $$ LANGUAGE 'plpgsql';
- CREATE TRIGGER update_login_attempts_users BEFORE UPDATE
- ON users FOR EACH ROW EXECUTE PROCEDURE
- update_modified_column_users();
- -- -----------------------------------------------------------------
- -- no params
- CREATE OR REPLACE FUNCTION unix_timestamp() RETURNS BIGINT AS '
- SELECT EXTRACT(EPOCH FROM CURRENT_TIMESTAMP(0))::bigint AS result;
- ' LANGUAGE 'SQL';
- -- timestamp without time zone (i.e. 1973-11-29 21:33:09)
- CREATE OR REPLACE FUNCTION unix_timestamp(TIMESTAMP) RETURNS BIGINT AS '
- SELECT EXTRACT(EPOCH FROM $1)::bigint AS result;
- ' LANGUAGE 'SQL';
- -- timestamp with time zone (i.e. 1973-11-29 21:33:09+01)
- CREATE OR REPLACE FUNCTION unix_timestamp(TIMESTAMP WITH TIME zone) RETURNS BIGINT AS '
- SELECT EXTRACT(EPOCH FROM $1)::bigint AS result;
- ' LANGUAGE 'SQL';
その他
最初にMySQLで試したのだが、その時の記録MySQL。使わない、使えないユーザは消した。そしてDxAuthに必要なテーブルの一覧。
mysql> delete from user where user = ''; mysql> grant select,insert,update,delete,create,drop on cidb.* to miyabiman; Query OK, 0 rows affected (0.06 sec) [baker@www dx_auth] $ mysql -u miyabiman -h localhost cidb -p < schema.sql mysql> show tables; +----------------+ | Tables_in_cidb | +----------------+ | ci_sessions | | login_attempts | | permissions | | roles | | user_autologin | | user_profile | | user_temp | | users | +----------------+ 8 rows in set (0.00 sec)
作業の過程でつまずいたことなど。メモとして。
- ログが書き込みされない
- Session key 作成
- PL/PGSQLが必要 # create language plpgsql;
- テーブルが足りない
- Primary Keyを用意する
0 件のコメント:
コメントを投稿