最近の Rails ではどんな認証プラグインを使えば良のかわからず、助けを得るために Rails Wiki を見ると、次の 3 つが挙げられていた。
restful-authentication がデファクトのようだが、へそ曲がりの血が騒ぐので Authlogic か Clearance に惹かれる。Github の見方が良く分からないせいかもしれないが、 Clearance の方が良くメンテされていそうだったので Clearance をいじってみよう。
まずはインストール。Rails Wiki には
$ sudo gem install thoughtbot-clearance -s http://gems.github.com
と、 Github からインストールするように書いてあるが、普通に gem をインストールしてみた。
$ sudo gem install clearance
Successfully installed clearance-0.8.6
1 gem installed
Installing ri documentation for clearance-0.8.6...
Installing RDoc documentation for clearance-0.8.6...
$
まずは良さそう。README によれば次に generator を使う。ちょっと長いけど次のようになった。
$ ./script/generate clearance
exists config/initializers
create config/initializers/clearance.rb
insert include Clearance::Authentication into app/controllers/application_controller.rb
exists app/models
create app/models/user.rb
insert Clearance::Routes.draw(map) into config/routes.rb
create test/factories
create test/factories/clearance.rb
create db/migrate
create db/migrate/20100221092116_clearance_create_users.rb
readme README
*******************************************************************************
Next:
1. Configure default url options for the mailer to generate URLs in emails.
In production.rb it must be the actual host your application is deployed to.
In config/environments/test.rb and config/environments/development.rb:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
2. Migrate:
rake db:migrate
4. Make sure you're displaying flashes somewhere. For instance, in a layout:
<% flash.each do |key, value| -%>
<%=h value %>
<% end -%>
*******************************************************************************
$
README には次の記述がある。
inserts Clearance::User into your User model
inserts Clearance::Authentication into your ApplicationController
inserts Clearance::Routes.draw(map) into your config.routes.rb
created a migration that either creates a users table or adds only missing columns
prints further instructions
まずは User モデルに [#include Clearance::User#] を挿入。
$ less app/models/user.rb
class User < ActiveRecord::Base
include Clearance::User
end
次に ApplicationController に [#include Clearance::Authentication#] を挿入。
$ less app/controllers/application_controller.rb
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
include Clearance::Authentication
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
end
次に [#config/routes.rb#] に [#Clearance::Routes.draw(map)#] を挿入。
$ less config/routes.rb
ActionController::Routing::Routes.draw do |map|
Clearance::Routes.draw(map)
# The priority is based upon order of creation: first created -> highest priority.
(略)
生成されたマイグレーションファイルは次のようになっている。今回は User モデルが無いところで実行しているので [#createtable#] になっているが、既にある場合は [#addcolumn#] になるのだろう。
$ less db/migrate/20100221092116_clearance_create_users.rb
class ClearanceCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.string :email
t.string :encrypted_password, :limit => 128
t.string :salt, :limit => 128
t.string :confirmation_token, :limit => 128
t.string :remember_token, :limit => 128
t.boolean :email_confirmed, :default => false, :null => false
t.timestamps
end
add_index :users, [:id, :confirmation_token]
add_index :users, :email
add_index :users, :remember_token
end
def self.down
drop_table :users
end
end
次に行うのが [#defaulturloptions#] の設定。[#config/environments/test.rb#] と [#config/environments/development.rb#] には下記の設定を、[#config/environments/production.rb#] にはデプロイ先に合わせて設定しろとのこと。
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
メールサーバの設定はしてないなぁ。どうするか。ちゃんとメールとパスワードで認証って README に書いてあるもんな。失敗か。
Rails authentication with email & password.
あとは DB のマイグレートと flashes の表示だけなんだけどな。タイトルは「その 1 」だけど続かないかも。。
でもメールで認証したい人には良さげだ。

RailsによるアジャイルWebアプリケーション開発【Sam Ruby】
※これの第 2 版が手元にある。第 1 版も英語版も実は買った。第 3 版も買う運命なのか。いつまでも入門でも無いのだが。