2008-04-29
restful_authenticationの設定
なんかこれからはacts_as_authenticatedではなくて、restful_authenticationが推奨のようですね。設定で詰まったりしたので、メモ書き。
以下のページを参考にしてみました。
restful_authentication - あとで読むRailsのススメ - ZDNet Japan
restful_authentication - happy lie, happy life
インストール
script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication/
1.普通にログイン認証
ごく普通に新規ユーザーを作成し、ログイン認証だけを行いたい場合は以下ようにgenerateします。
script/generate authenticated user sessions rake db:migrate
app/controllers/application.rbのApplicationController内に
include AuthenticatedSystem
を追加。
login認証の必要なController内に
before_filter :login_required
を追加します。
これで基本はOK。URLは以下のようになります
| ユーザー作成 | /users/new |
| ログイン | /sessions/new |
| ログアウト | /sessions/destroy |
このままではURLが解りにくいのでroute.rbに以下のようにマッピングします
map.signup '/signup', :controller => 'users', :action => 'new' map.login '/login', :controller => 'sessions', :action => 'new' map.logout '/logout', :controller => 'sessions', :action => 'destroy'
これで完了。
2.メールアクティベーションしたい場合
インストール後、以下のように「--include-activation」オプション付きでgenerateします。
script/generate authenticated user sessions --include-activation rake db:migrate
*注意:オプション無しでgenerateしている場合にはmigrateで作成されている00x_create_usersファイルをいったん消してから再度作成します(modelにactivation関係が追加されます)
app/controllers/application.rbのApplicationController内に
include AuthenticatedSystem
を追加します。
environment.rbの中にobserverを追加し、ActionMailerのdelivery_methodを設定します
Rails::Initializer.run do |config| ... config.active_record.observers = :user_observer ... end ActionMailer::Base.delivery_method = :sendmail
これが無いとメールが飛んで行かず、アクティベートできません。
login認証の必要なController内に
before_filter :login_required
を追加します。
1と同様にroute.rbに以下のようにマッピングします。また、アクティベーション用のマッピングも追加します。
map.signup '/signup', :controller => 'users', :action => 'new' map.login '/login', :controller => 'sessions', :action => 'new' map.logout '/logout', :controller => 'sessions', :action => 'destroy' map.connect "activate/:activation_code", :controller => "users", :action => "activate"
- 58 http://xdig.net/ant/
- 51 http://d.hatena.ne.jp/spitfire_tree/20080221/1203610276
- 14 http://www.google.com/search?client=safari&rls=ja-jp&q=どこでもMyMac Airmac+Express&ie=UTF-8&oe=UTF-8
- 11 http://www.google.co.jp/url?sa=t&rct=j&q=restful_authentication sendmail&source=web&cd=1&ved=0CB0QFjAA&url=http://d.hatena.ne.jp/bellx2/20080429/p1&ei=kdS9Ts_oOIfbmAXw3ayoBA&usg=AFQjCNFMNAd5HMWTA2896GflA5AXRpURKg&sig2=li9tjl-nYdEMckdRR
- 9 http://takuya.miyai.jp/programing/69.html
- 8 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja:official&hs=g0E&q=acts_as_authenticated+restful_authentication&btnG=検索&lr=lang_ja
- 7 http://paramushir.sakura.ne.jp/mypim/rss_read.php?id=07bf5ced7aa731d81e7a6ed020cf8bd6
- 5 http://r.tabelog.com/tokyo/A1319/A131903/13006607/
- 5 http://search.yahoo.co.jp/search?p=restful_authentication&search.x=1&fr=top_ga1_sa&tid=top_ga1_sa&ei=UTF-8&aq=&oq=
- 5 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4ADBR_jaJP267JP267&q=time+capsule+購入