fc2ブログ

2023.11 «  - - - - - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 - - - - - - » 2024.01
TOP > CATEGORY > generator/plugin

TOP | NEXT

Acts as authenticated 体験記 その6【まとめ】 

2007年11月26日 ()
その1【取り敢えずインストール】
その2【必要ファイルを自動生成】
その3【画面表示を見てみる】
その4【日本語化をしてみる】
その5【ログイン許可・不許可】
その6【まとめ】



体験記、ダラダラ書いたので、Acts as authenticated を使うには何をすればいいか、をまとめてみよう、と思いました。

1.script/plugin コマンドで、Acts as authenticated をインストール
2.script/generate authenticated コマンドで、コントローラとビューなどを生成
3.rake db:migrate コマンドで、DB にテーブルを作る
4.app/controllers/account_controller.rb から app/controllers/application.rb へ、4 行カット&ペースト
5.Ruby-GetText で日本語化
6.必要ならば、before_filter などでログインが必要なコントローラとアクションを設定



ですね。

はぁ、1ヶ月弱かけてご紹介しました。これはツカエルプラグインだと思いましたので、是非皆様もお試しあれ!

【広告】

スポンサーサイト



[2007.11.26(Mon) 19:49] generator/pluginTrackback(0) | Comments(0) 見る▼
↑TOPへ

Acts as authenticated 体験記 その5【ログイン許可・不許可】 

2007年11月25日 ()
注:今回は、こうすれば出来ますよ!と順序立てて書いたものではなく、指向を変えて、私はこのように実験しました、というまさに恥ずかしい部分まで見せる生のドキュメンタリー体験記を書きます。ですので、まどろっこしいハズ!ご了承ください。


その1【取り敢えずインストール】
その2【必要ファイルを自動生成】
その3【画面表示を見てみる】
その4【日本語化をしてみる】
その5【ログイン許可・不許可】
その6【まとめ】



今回は、ページごとに、ログインを許可したり不許可にしたりするのはどーやるの?という疑問を解決したいと思います。

ちょっくら調べてみると、やり方が 2 通り見つかりました。

1. コントローラの各アクションごとに logged_in? メソッドを使って調べる
2. コントローラごとに before_filter や skip_before_filter を使って指定する




まず、

1. コントローラの各アクションごとに logged_in? メソッドを使って調べる



これなのですが、コントローラのアクションごとに、logged_in? メソッドを使って、ユーザがログインしている時としていない時の動作を自分で書きます。

例えば、account コントローラのアクションが 3 つあったとします。action1, action2, action3 という名前にします。

action1 (http://localhost:3000/account/action1)
 は、ログインしていてもしていなくても見られるページ

action2 (http://localhost:3000/account/action2)
 は、ログインしていないと、action1 に飛ばします。ログインしていると見られます。

action3 (http://localhost:3000/account/action3)
 は、ログインしいないと見られます。ログインしていると action2 に飛ばします。

みたいなことをしたいと思ったら、以下のようになります。

app/controllers/account_controller.rb に追加

  def action1
    # ログインしていてもしていなくても見られるページ
  end

  def action2
    if not logged_in?
      # ログインしていないと、action1 に飛ばします。
      redirect_to(:controller => '/account', :action => 'action1')
    end

    # ログインしていると見られます。
  end

  def action3
    if logged_in?
      # ログインしていると action2 に飛ばします。
      redirect_to(:controller => '/account', :action => 'action2')
    end

    # ログインしいないと見られます。
  end



もちろん、対応する rhtml を新規作成するのをお忘れなく!ちなみにお節介すぎるかもですが、ファイル名は以下のようになります。
app/views/account/action1.rhtml
app/views/account/action2.rhtml
app/views/account/action3.rhtml


次に、

2. コントローラごとに before_filter や skip_before_filter を使って指定する



別のやり方ですね。before_filter や skip_before_filter に、:login_required を指定して、コントローラ全体に、一気にログイン制限を掛けたり取っ払ったりできます。

例えば、app/controllers/honyarara_controller.rb があったとして、このコントローラのアクション全てで、ログインしているときだけページが見られるようにするには、

class HonyararaController < ApplicationController

  before_filter :login_required

  def action1
  end

  def action2
  end

  def action3
  end

end



という 1 行を付け加えます。

さて、ここで、もしログインしていない状態で、この honyarara にアクセスしようとした場合(例:http://localhost:3000/honyarara/action1/) は、/account/login に飛ばされます。

というのは、lib/authenticated_system.rb ファイルの access_denied メソッドに書いてありますので、この飛び先を変えたい場合、そこを変更すれば大丈夫です。

でもって、before_filter や skip_before_filter の使用例を挙げておきますので、参考にしてください。


・このコントローラ全てのアクションでログインが必要

before_filter :login_required



・このコントローラの action1 と action2 アクションでのみログインが必要

before_filter :login_required, :only => [ :action1, :action2 ]



・このコントローラの action1 と action2 以外のアクションでログインが必要

before_filter :login_required, :except => [ :action1, :action2 ]



・このコントローラ全てのアクションでログインは必要ない

skip_before_filter :login_required




と、終了!次のエントリでは、まとめ、というかやったことの手順を書いてみて〆ることにします。

【広告】

[2007.11.25(Sun) 19:28] generator/pluginTrackback(0) | Comments(0) 見る▼
↑TOPへ

Acts as authenticated 体験記 その4【日本語化をしてみる】 

2007年11月23日 ()
注:今回は、こうすれば出来ますよ!と順序立てて書いたものではなく、指向を変えて、私はこのように実験しました、というまさに恥ずかしい部分まで見せる生のドキュメンタリー体験記を書きます。ですので、まどろっこしいハズ!ご了承ください。


その1【取り敢えずインストール】
その2【必要ファイルを自動生成】
その3【画面表示を見てみる】
その4【日本語化をしてみる】
その5【ログイン許可・不許可】
その6【まとめ】



今回は、今あるログイン機能の日本語化をしてみたいと思います。

と、日本語化をする前に、たぶんこうすれば日本語化できるんじゃあないかな、という目処は立っています。

まず、Rails の日本語環境を整える。

そして、Ruby-GetText を使う。


てな具合かな、と思います。

それでは、まず Rails の日本語環境を整える、ですが、こちらのエントリーを参考にというかそのまんまの手順をやります。

と、いうことで、この手順は省略。

そして、次に、Ruby-GetText を使います。

この手順はべつのエントリーに書いてありましたので、そちらを参考に、Ruby-GetText を require したり app/controllers/application.rb に init_gettext を指定したりします。

init_gettext の後ろには、テキストドメイン名というものを指定しますが、今回はテキトーに 'my_log_auth_textdomain' と指定しました。

そして、Web サーバを再起動です。


アハアハ、http://localhost:3000/account/signup エラーメッセージが日本語化されました。





そしたら、今回はコントローラとビューを良く言えばすぐ多言語に対応できるローカライズ方法、悪く言えばまどろっこしい方法である、_('xxxxxxxxxxxxxxxxxx') の形式に書き換えて、po ファイル mo ファイルの準備をします。うまくいくかな?

app/controllers/account_controller.rb

class AccountController < ApplicationController

  # say something nice, you goof! something sweet.
  def index
    redirect_to(:action => 'signup') unless logged_in? || User.count > 0
  end

  def login
    return unless request.post?
    self.current_user = User.authenticate(params[:login], params[:password])
    if logged_in?
      if params[:remember_me] == "1"
        self.current_user.remember_me
        cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
      end
      redirect_back_or_default(:controller => '/account', :action => 'index')
      flash[:notice] = "Logged in successfully"
    end
  end

  def signup
    @user = User.new(params[:user])
    return unless request.post?
    @user.save!
    self.current_user = @user
    redirect_back_or_default(:controller => '/account', :action => 'index')
    flash[:notice] = "Thanks for signing up!"
  rescue ActiveRecord::RecordInvalid
    render :action => 'signup'
  end

  def logout
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    reset_session
    flash[:notice] = "You have been logged out."
    redirect_back_or_default(:controller => '/account', :action => 'index')
  end
end



↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

class AccountController < ApplicationController

  # say something nice, you goof! something sweet.
  def index
    redirect_to(:action => 'signup') unless logged_in? || User.count > 0
  end

  def login
    return unless request.post?
    self.current_user = User.authenticate(params[:login], params[:password])
    if logged_in?
      if params[:remember_me] == "1"
        self.current_user.remember_me
        cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
      end
      redirect_back_or_default(:controller => '/account', :action => 'index')
      flash[:notice] = _("Logged in successfully")
    end
  end

  def signup
    @user = User.new(params[:user])
    return unless request.post?
    @user.save!
    self.current_user = @user
    redirect_back_or_default(:controller => '/account', :action => 'index')
    flash[:notice] = _("Thanks for signing up!")
  rescue ActiveRecord::RecordInvalid
    render :action => 'signup'
  end
 
  def logout
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    reset_session
    flash[:notice] = _("You have been logged out.")
    redirect_back_or_default(:controller => '/account', :action => 'index')
  end
end






app/views/account/signup.rhtml

<%= error_messages_for :user %>
<% form_for :user do |f| -%>
<p><label for="login">Login</label><br/>
<%= f.text_field :login %></p>

<p><label for="email">Email</label><br/>
<%= f.text_field :email %></p>

<p><label for="password">Password</label><br/>
<%= f.password_field :password %></p>

<p><label for="password_confirmation">Confirm Password</label><br/>
<%= f.password_field :password_confirmation %></p>

<p><%= submit_tag 'Sign up' %></p>
<% end -%>



↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

<%= error_messages_for :user %>
<% form_for :user do |f| -%>
<p><label for="login">_('Login')</label><br/>
<%= f.text_field :login %></p>

<p><label for="email">_('Email')</label><br/>
<%= f.text_field :email %></p>

<p><label for="password">_('Password')</label><br/>
<%= f.password_field :password %></p>

<p><label for="password_confirmation">_('Confirm Password')</label><br/>
<%= f.password_field :password_confirmation %></p>

<p><%= submit_tag _('Sign up') %></p>
<% end -%>






app/views/account/login.rhtml

<% form_tag do -%>
<p><label for="login">Login</label><br/>
<%= text_field_tag 'login' %></p>

<p><label for="password">Password</label><br/>
<%= password_field_tag 'password' %></p>

<!-- Uncomment this if you want this functionality
<p><label for="remember_me">Remember me:</label>
<%= check_box_tag 'remember_me' %></p>
-->

<p><%= submit_tag 'Log in' %></p>
<% end -%>



↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

<% form_tag do -%>
<p><label for="login">_('Login')</label><br/>
<%= text_field_tag 'login' %></p>

<p><label for="password">_('Password')</label><br/>
<%= password_field_tag 'password' %></p>

<!-- Uncomment this if you want this functionality
<p><label for="remember_me">_('Remember me'):</label>
<%= check_box_tag 'remember_me' %></p>
-->

<p><%= submit_tag _('Log in') %></p>
<% end -%>






lib/authenticated_system.rb から抜粋

    def login_from_cookie
      return unless cookies[:auth_token] && !logged_in?
      user = User.find_by_remember_token(cookies[:auth_token])
      if user && user.remember_token?
        user.remember_me
        self.current_user = user
        cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
        flash[:notice] = "Logged in successfully"
      end
    end



↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    def login_from_cookie
      return unless cookies[:auth_token] && !logged_in?
      user = User.find_by_remember_token(cookies[:auth_token])
      if user && user.remember_token?
        user.remember_me
        self.current_user = user
        cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
        flash[:notice] = _("Logged in successfully")
      end
    end





と、ここまで揃ったところで、Rakefile に 自作 rake コマンドを追加しなくちゃです。ここら辺は、こちらのエントリーを参考にしました。

テキストドメイン名は、上の方で勝手に命名した my_log_auth_textdomain を使用するということに特に注意しないとな、と。

以下を Rakefile に追加します。

Rakefile

desc "Update pot/po files."
task :updatepo do
  require 'gettext/utils'
  GetText.update_pofiles("my_log_auth_textdomain", #テキストドメイン名(init_gettextで使用した名前)
                         Dir.glob("{app,config,components,lib}/**/*.{rb,rhtml}"), #ターゲットとなるファイル
                         "blog 1.0.0" #アプリケーションのバージョン
                         )
end

desc "Create mo-files"
task :makemo do
  require 'gettext/utils'
  GetText.create_mofiles(true, "po", "locale")
end





ぷはー、結構骨が折れました。たぶんあと一息!

# rake updatepo
# mkdir po/ja
# cp po/my_log_auth_textdomain.pot po/ja/my_log_auth_textdomain.po



毎回ひっかかりそうになるのですが、この最後のコマンド、pot という拡張子のファイルを po という拡張子に変えつつのコピーです。

そして、私の大好きな(嘘50%)翻訳作業です。

po/ja/my_log_auth_textdomain.po

#: app/controllers/account_controller.rb:17 lib/authenticated_system.rb:108
msgid "Logged in successfully"
msgstr ""

#: app/controllers/account_controller.rb:27
msgid "Thanks for signing up!"
msgstr ""

#: app/controllers/account_controller.rb:36
msgid "You have been logged out."
msgstr ""

#: app/models/user.rb:-
msgid "user"
msgstr ""

#: app/models/user.rb:-
msgid "User|Login"
msgstr ""

#: app/models/user.rb:-
msgid "User|Email"
msgstr ""

#: app/models/user.rb:-
msgid "User|Crypted password"
msgstr ""

#: app/models/user.rb:-
msgid "User|Salt"
msgstr ""

#: app/models/user.rb:-
msgid "User|Created at"
msgstr ""

#: app/models/user.rb:-
msgid "User|Updated at"
msgstr ""

#: app/models/user.rb:-
msgid "User|Remember token"
msgstr ""

#: app/models/user.rb:-
msgid "User|Remember token expires at"
msgstr ""

#: app/views/account/signup.rhtml:15
msgid "Sign up"
msgstr ""

#: app/views/account/login.rhtml:13
msgid "Log in"
msgstr ""



↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

#: app/controllers/account_controller.rb:17 lib/authenticated_system.rb:108
msgid "Logged in successfully"
msgstr "ログインできました"

#: app/controllers/account_controller.rb:27
msgid "Thanks for signing up!"
msgstr "ご登録ありがとうございます!"

#: app/controllers/account_controller.rb:36
msgid "You have been logged out."
msgstr "ログアウトしました"

#: app/models/user.rb:-
msgid "user"
msgstr "ユーザ"

#: app/models/user.rb:-
msgid "User|Login"
msgstr "ログインID"

#: app/models/user.rb:-
msgid "User|Email"
msgstr "メールアドレス"

#: app/models/user.rb:-
msgid "User|Crypted password"
msgstr "暗号化されたパスワード"

#: app/models/user.rb:-
msgid "User|Salt"
msgstr "塩(嘘でーす)"

#: app/models/user.rb:-
msgid "User|Created at"
msgstr "作成日"

#: app/models/user.rb:-
msgid "User|Updated at"
msgstr "更新日"

#: app/models/user.rb:-
msgid "User|Remember token"
msgstr "ログイン記憶用トークン"

#: app/models/user.rb:-
msgid "User|Remember token expires at"
msgstr "ログイン記憶用トークン期限"

#: app/views/account/signup.rhtml:15
msgid "Sign up"
msgstr "ユーザ登録"

#: app/views/account/login.rhtml:13
msgid "Log in"
msgstr "ログイン"



へぇー、全く同じメッセージは、まとめて1回の翻訳で済むのか。便利便利。


この po ファイルをバイナリ化してツカエルようにします。

# rake makemo




それで画面を確認すると・・・

あああああああああああああ オッパッピーーーーー

view ファイルがうまく行かなかった。_('Login') や _('Password') と、モロ表示されてもうた。

モロ表示されて失敗、の画面↓


ので rhtml を書き直す、からの再挑戦です。

app/views/account/login.rhtml

<% form_tag do -%>
<p><label for="login">_('Login')</label><br/>
<%= text_field_tag 'login' %></p>

<p><label for="password">_('Password')</label><br/>
<%= password_field_tag 'password' %></p>

<!-- Uncomment this if you want this functionality
<p><label for="remember_me">_('Remember me'):</label>
<%= check_box_tag 'remember_me' %></p>
-->

<p><%= submit_tag _('Log in') %></p>
<% end -%>



↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

<% form_tag do -%>
<p><label for="login"><%= _('Login') %></label><br/>
<%= text_field_tag 'login' %></p>

<p><label for="password"><%= _('Password') %></label><br/>
<%= password_field_tag 'password' %></p>

<!-- Uncomment this if you want this functionality
<p><label for="remember_me">_('Remember me'):</label>
<%= check_box_tag 'remember_me' %></p>
-->

<p><%= submit_tag _('Log in') %></p>
<% end -%>






app/views/account/signup.rhtml

<%= error_messages_for :user %>
<% form_for :user do |f| -%>
<p><label for="login">_('Login')</label><br/>
<%= f.text_field :login %></p>

<p><label for="email">_('Email')</label><br/>
<%= f.text_field :email %></p>

<p><label for="password">_('Password')</label><br/>
<%= f.password_field :password %></p>

<p><label for="password_confirmation">_('Confirm Password')</label><br/>
<%= f.password_field :password_confirmation %></p>

<p><%= submit_tag _('Sign up') %></p>
<% end -%>



↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

<%= error_messages_for :user %>
<% form_for :user do |f| -%>
<p><label for="login"><%= _('Login') %></label><br/>
<%= f.text_field :login %></p>

<p><label for="email"><%= _('Email') %></label><br/>
<%= f.text_field :email %></p>

<p><label for="password"><%= _('Password') %></label><br/>
<%= f.password_field :password %></p>

<p><label for="password_confirmation"><%= _('Confirm Password') %></label><br/>
<%= f.password_field :password_confirmation %></p>

<p><%= submit_tag _('Sign up') %></p>
<% end -%>





# rake updatepo



と打つと、なななんと、po/my_log_auth_textdomain.pot はもとより、po/ja/my_log_auth_textdomain.po ファイルまで更新してくれました。pot ファイルの更新を po に自動的に判定してくださるなんて感謝です。

さて大好きな(嘘50%)翻訳作業と行きます。

po/ja/my_log_auth_textdomain.po

#: app/views/account/signup.rhtml:3 app/views/account/login.rhtml:2
#, fuzzy
msgid "Login"
msgstr "ログイン"

#: app/views/account/signup.rhtml:6
#, fuzzy
msgid "Email"
msgstr "メールアドレス"

#: app/views/account/signup.rhtml:9 app/views/account/login.rhtml:5
msgid "Password"
msgstr ""

#: app/views/account/signup.rhtml:12
msgid "Confirm Password"
msgstr ""



↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

#: app/views/account/signup.rhtml:3 app/views/account/login.rhtml:2
#, fuzzy
msgid "Login"
msgstr "ログイン"

#: app/views/account/signup.rhtml:6
#, fuzzy
msgid "Email"
msgstr "メールアドレス"

#: app/views/account/signup.rhtml:9 app/views/account/login.rhtml:5
msgid "Password"
msgstr "パスワード"

#: app/views/account/signup.rhtml:12
msgid "Confirm Password"
msgstr "確認用に同じパスワードをもう一回"




fuzzy があるとうまく反映されないので、fuzzy の行は消します。(なぜ fuzzy が付いたかは不明・・・)

# rake makemo



コマンドを打って画面を再確認・・・・

再確認中・・・

・・・

万事OK!

かと思ったら、まだ不完全でした。

もー、ずるずると、このエントリーは続くなあ。

account/signup の時にエラーメッセージを表示させると、まだ英語のままのカラム名が・・・



これどーするの、と思ったら、本家のサイトに N_('XXXXXX') を使うよーって載ってましたので試します。

app/models/user.rb に N_('XXXXXXXX') を追加


class User < ActiveRecord::Base
  # Virtual attribute for the unencrypted password
  attr_accessor :password

  N_("User|Password")
  N_("User|Password confirmation")

  validates_presence_of :login, :email



# rake updatepo



po/ja/my_log_auth_textdomain.po

#: app/models/user.rb:6
#, fuzzy
msgid "User|Password"
msgstr "パスワード"

#: app/models/user.rb:7
msgid "User|Password confirmation"
msgstr ""



↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

#: app/models/user.rb:6
msgid "User|Password"
msgstr "パスワード"

#: app/models/user.rb:7
msgid "User|Password confirmation"
msgstr "確認用のパスワード"



# rake makemo




そして、再度確認。

やったー、デキター。


それにしても今回のエントリは、多分今までの中で最長と思われます。ここまで読んでくださった方ありがとうございました。

次回は、ログインしていなくては表示できないページと、ログインしていなくても表示できるページはどうやって区別して設定するか、なんてやろうかと思います。

【広告】

[2007.11.23(Fri) 03:31] generator/pluginTrackback(0) | Comments(0) 見る▼
↑TOPへ

Acts as authenticated 体験記 その3【画面表示を見てみる】 

2007年11月15日 ()
注:今回は、こうすれば出来ますよ!と順序立てて書いたものではなく、指向を変えて、私はこのように実験しました、というまさに恥ずかしい部分まで見せる生のドキュメンタリー体験記を書きます。ですので、まどろっこしいハズ!ご了承ください。


その1【取り敢えずインストール】
その2【必要ファイルを自動生成】
その3【画面表示を見てみる】
その4【日本語化をしてみる】
その5【ログイン許可・不許可】
その6【まとめ】



画面はデフォルトの状態ではどうなっとんのよ、と早速アクセスすることにしました。


# script/server



http://localhost:3000/account



まあ、なんで account という URI を指定するか、というと言わずとしれたコントローラ名が account だからです。

言い換えると、app/controllers/account_controller.rb というファイルがあるからです。と、また先生口調になってみる。

それにアクセスしたら、突然 URL が http://localhost:3000/account/signup になりました。




どうやらリダイレクトされた模様。config/routes.rb ファイルは特にいぢられてないようなので、コントローラ内で何かのリダイレクトが走ったと考えられます。

http://localhost:3000/account にアクセスすると、ror の機能で、コントローラの index メソッドが呼ばれることになっています。

app/controllers/account_controller.rb から抜粋

  def index
    redirect_to(:action => 'signup') unless logged_in? || User.count > 0
  end



今、users テーブルには誰も登録されていませんから、signup にリダイレクトされるという訳なのですね。

と、解説がいちいち詳しすぎる?

でもいいのです、今回はだらーんと力を抜いてやってます。

じゃあ、この画面でユーザを新規登録してみようっと。

最初はわざと間違えて、2つめの確認用パスワードを1つめのパスワードと違うのを入れます。

と、案の定エラーメッセージがでました。




そこで、今度は2つのパスワードを一緒のモノを入れます。




そして、submit ボタンを押します。えいっ。




わぁ、訳分からん詩が表示されました。一瞬 LAN 内クラッキングでも起きたのかと思いました。

詩は、韻を踏んであって、ちょっといい感じです。Woman ... のくだりは切ないですね。

などと、話が横道にそれてしまうのでした。

users テーブルにも、今登録したデータが入っているようですし、このパスワードは暗号化されて DB へ保存されますね。強度とかは、いかほどに?

さて、強度の話は分からずじまいにしておいて、他にどんなアクションがあるのか見ていきます。

今までは、index と signup アクションを見ました。

account_controller には、その他に、login アクションと logout アクションが定義されています。

login アクションは、ログイン画面を表示、logout アクションは、ログアウトしてから index アクションにリダイレクトしています。

ほうほう、これで道具はそろったという訳ですな。

と思ったら、account_controller にこういうコメントとコードが書いてありました。

# Be sure to include AuthenticationSystem in Application Controller instead
include AuthenticatedSystem
# If you want "remember me" functionality, add this before_filter to Application Controller
before_filter :login_from_cookie



キャー、application controller にカット&ペーストしろよ、と強要しています。

remember me 機能も、一応使う、という方向にしておくということで、こちらも application controller にカット&ペーストします。

というわけで、以上の 4 行を、app/controllers/account_controller.rb から、app/controllers/application.rb にカット&ペーストします。

ちなみに、application.rb つーのは、コントローラ全てに共通した処理を行うためのスーパーコントローラみたいなものです。

app/controllers/application.rb

class ApplicationController < ActionController::Base
  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_MyLogAuth_session_id'
  # Be sure to include AuthenticationSystem in Application Controller instead
  include AuthenticatedSystem
  # If you want "remember me" functionality, add this before_filter to Application Controller
  before_filter :login_from_cookieend

end



これで落ち着きました。ハァ。

次回は、日本語化でもがんばってやってみるか!と考えています。

【広告】

[2007.11.15(Thu) 00:42] generator/pluginTrackback(0) | Comments(0) 見る▼
↑TOPへ

Acts as authenticated 体験記 その2【必要ファイルを自動生成】 

2007年11月12日 ()
注:今回は、こうすれば出来ますよ!と順序立てて書いたものではなく、指向を変えて、私はこのように実験しました、というまさに恥ずかしい部分まで見せる生のドキュメンタリー体験記を書きます。ですので、まどろっこしいハズ!ご了承ください。


その1【取り敢えずインストール】
その2【必要ファイルを自動生成】
その3【画面表示を見てみる】
その4【日本語化をしてみる】
その5【ログイン許可・不許可】
その6【まとめ】



じゃあ、前回インストールした直後にメッセージとして表示された使用例をそのままコマンドへぶち込みます。

# script/generate authenticated user account

exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/account
exists test/functional/
exists test/unit/
create app/models/user.rb
create app/controllers/account_controller.rb
create lib/authenticated_system.rb
create lib/authenticated_test_helper.rb
create test/functional/account_controller_test.rb
create app/helpers/account_helper.rb
create test/unit/user_test.rb
create test/fixtures/users.yml
create app/views/account/index.rhtml
create app/views/account/login.rhtml
create app/views/account/signup.rhtml
create db/migrate
create db/migrate/001_create_users.rb



ふむふむ、モデル・コントローラ・ヘルパ・テスト・ビュー・マイグレーション、のファイルができました。

user と指定した引数が、モデルになりましたね。account と指定した引数は、コントローラ・ヘルパ・ビューになりました。

へぇー。

なにからやろーかなー、と思いましたが、まだデータベース自体を作っていなかったので、作ります。

今回は、本腰を入れて、開発用・テスト用・本番用、と3つのデータベースを作ることにします。

# mysql -u root
mysql# create database MyLogAuth_development character set utf8;
mysql# create database MyLogAuth_test character set utf8;
mysql# create database MyLogAuth_production character set utf8;



ビシっと成功。

次に、いきなりテストをしてみようとする大胆なア・タ・ク・シ

# rake test/unit/user_test.rb



しかし、コマンドを打った後、何も返答が返ってこないで、コマンドプロンプト状態に戻ってしまう・・・悩むこと3日(誇張)・・・

あぁ、しまった rakeじゃなくてrubyだった・・・へたこいたー

気を取り直して、テストをしてみよう。

# ruby test/unit/user_test.rb



おぉ、今度はきちんとテストが実行されました。でもエラーばっか。

10 tests, 0 assertions, 0 failures, 10 errors



そりゃそうさ、users テーブルを DB に作っていないのだから。

というわけで、migration をします。migration のためのファイルは、先ほどの script/generate authenticated をした時点で db/migrate/001_create_users.rb として出来てますね。それをそのまま使うの巻。

# rake db:migrate



すると、users テーブルが、MyLogAuth_developement データベースにだけ作成されました。

_test データベースと _production テータベースはほったらかしのままです。

なので、MyLogAuth_test データベースにも、users テーブルを作ってさっきのテストコマンドをもう1回実行したいんですけどー。

どうやるのがいいのだろう、と考えること四十久里浜。

分からない、もしくは忘れたということで、Google 様、でておいでー。

ピロリロリン

# rake db:migrate RAILS_ENV=test



ついでに、_production データベースも migration っちゃいましょう。

ピロコロリン

# rake db:migrate RAILS_ENV=production




さて、では付属の単体テストツールを実行してみます。

# ruby test/unit/user_test.rb
Started
..........
Finished in 0.527608 seconds.

10 tests, 17 assertions, 0 failures, 0 errors



ハァーィ、全部成功しておりますわ!

ついでに、もう1つのテストファイルも実行してみようっと。

# ruby test/functional/account_controller_test.rb
Loaded suite test/functional/account_controller_test
Started
..............
Finished in 0.593564 seconds.

14 tests, 26 assertions, 0 failures, 0 errors



順調すぎてコワユス・・・・


ちなみに上記のテストの実施後、MyLogAuth_test データベースの user テーブルのデータを見たら、2行データ(quentin さんと aaron さん)が入っていました。

とこんなところで今日は自己満足。

次回は、実際に画面を見てみることにしてみようかなと。

【広告】

[2007.11.12(Mon) 00:29] generator/pluginTrackback(0) | Comments(0) 見る▼
↑TOPへ

TOP | NEXT