merb-auth-slice-activation は、merbでemailアクティベーションを簡単に実装するための非常に便利なSliceです。 このSliceは、merb-auth-coreが提供するhookである、after_authenticationをフックする事によって、アクティベーションが済んでいないアカウントでのログインをブロックする仕様になっています。該当するコードは以下の通り。

merb-auth-slice-activation/lib/merb-auth-slice-activation.rb

   1      # Initialization hook - runs before AfterAppLoads BootLoader
   2      def self.init
   3        # Actually check if the user is active 
   4        ::Merb::Authentication.after_authentication do |user, *rest|
   5          if user.respond_to?(:active?)
   6            user.active? ? user : nil
   7          else
   8            user
   9          end
  10        end
  11      end

ただ、この実装では、ログインに失敗した場合に何のエラーメッセージも表示されないため、以下のようにExceptionsコントローラにコードを加え、エラーメッセージを表示するようにしてみます。

   1  class Exceptions < Merb::Controller
   2    (snip)
   3    def unauthenticated
   4      request.exceptions.each do |e|
   5        session.authentication.errors.add(:general, e.message)
   6      end if request.exceptions
   7      render :format => :html
   8    end
   9  end

Merbでは、アクションの実行中に例外が発生した場合、Exceptionsコントローラに処理が回ってきます。その場合、発生した例外オブジェクトは、request.exceptionsで参照出来ます。

あとは、view側で

   1  <%= error_messages_for session.authentication %>

を記述すればOK.

posted by Png genki on Thu 1 Jan 2009 at 21:31

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry1173
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ