2009年3月25日水曜日

restful-authentication プラグインを使ってみる(1)



前から個人的にサービスを作ろうと思っていて、時間が作れなくて進めていませんでしたが、少し時間が作れそうなので本格的にはじめました。
とりあえずログイン認証から作る!ということで、Railsでデファクトの「restful-authentication」を利用してみます。


1. プロジェクト作成
rails -d mysql hogehoge

2. git コマンドのパスを通す
コマンドプロンプトから git が利用できるようにパスを通しておきます。

3. restful-authentication インストール
ruby script
/plugin install git://github.com/technoweenie/restful-authentication.git

4. aasm インストール
ruby script/plugin install git://github.com/rubyist/aasm.git

5. forgot-password インストール
ruby script/plugin install git://github.com/greenisus/forgot_password.git

6. user と session モデルを作成する

ruby script
/generate authenticated user sessions --include-activation --aasm

routes.rb に以下が追加される
 map.forgot_password '/forgot_password', :controller => 'passwords', :action => 'new'
 map.change_password '/change_password/:reset_code', :controller => 'passwords', :action => 'reset'
 map.resources :passwords
 map.logout '/logout', :controller => 'sessions', :action => 'destroy'
 map.login '/login', :controller => 'sessions', :action => 'new'
 map.register '/register', :controller => 'users', :action => 'create'
 map.signup '/signup', :controller => 'users', :action => 'new'
 map.resources :users
 map.resource :session

7. password モデルを作成する

ruby script
/generate forgot_password password user

8. aasm を読み込ませる
aasm はそのままでは読み込めないので、読み込めるように修正します。

File : vendor/plugins/restful-authentication/rails/init.rb
require File.join(File.dirname(__FILE__), "..", "lib", "authorization", "aasm_roles")

9. アクティベーション用の route を追加する
File : config/routes.rb
map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil
map.resources :users, :member => { :suspend => :put, :unsuspend => :put, :purge => :delete }

10. user モデルのオブザーバを定義する
File : config/environment.rb
config.active_record.observers = :user_observer

11. 認証システムをインクルードする
File : app/controller/application.rb
include
AuthenticatedSystem

12. データベースを作成する

13. テーブルを作成する
rake db:migrate

14. サーバを起動する
ruby script\server


これでユーザ管理の一通りの機能が利用できます。
  • 新規登録
http://localhost:3000/signup
  • ログイン
http://localhost:3000/login
  • ログアウト
http://localhost:3000/logout

次回は、アクティベーションメール送信、パスワード再設定、アプリケーション実装を行います。

0 件のコメント: