&tag(Rails/認証);
*目次 [#y9be1089]
#contents
*関連ページ [#a845864c]
*参考情報 [#x5ebd43e]

*before_filterとbefore_actionの違いって? [#b2424dac]
*ベーシック認証を実装する [#i0537ea2]
--[[Action Controller の概要 | Rails ガイド:http://railsguides.jp/action_controller_overview.html]]
-管理者用のコントローラーで実装する
#pre{{
class AdminController < ApplicationController
  before_action :authenticate

  def authenticate
    # BASIC認証
    authenticate_or_request_with_http_basic do |name, password|
      name == "testuser2" && password == "testpass2"
    end
  end

  def index
  end
end
}}

*ダイジェスト認証を実装する [#kf575bed]
-[[Action Controller の概要 | Rails ガイド:http://railsguides.jp/action_controller_overview.html]]
-authenticate_or_request_with_http_basicがauthenticate_or_request_with_http_digestにかわる。
#pre{{
class AdminsController < ApplicationController
  USERS = { "lifo" => "world" }
 
  before_action :authenticate
 
  private
 
    def authenticate
      authenticate_or_request_with_http_digest do |username|
        USERS[username]
      end
    end
end
}}
-パスワードはハッシュ化されたものでもいいらしい。
--[[Basic認証とDigest認証についての基礎知識 - チャレンジ目標:http://d.hatena.ne.jp/kazooto/20131204/1386171917]]
--[[ActionController::HttpAuthentication::Digest:http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Digest.html]]
#pre{{
require 'digest/md5'
class PostsController < ApplicationController
  REALM = "SuperSecret"
  USERS = {"dhh" => "secret", #plain text password
           "dap" => Digest::MD5.hexdigest(["dap",REALM,"secret"].join(":"))}  #ha1 digest password

  before_action :authenticate, except: [:index]

  def index
    render plain: "Everyone can see me!"
  end

  def edit
    render plain: "I'm only accessible if you know the password"
  end

  private
    def authenticate
      authenticate_or_request_with_http_digest(REALM) do |username|
        USERS[username]
      end
    end
end

}}


*複数の認証を組み合わせる [#w6acbb9e]
-before_actionで認証を分割するか、一つの認証メソッドで失敗したらredirect_to & returnする(要return)。


**IPアドレス認証と組み合わせる [#i61eb54d]
-[[ActiveAdminでのBasic認証+アルファ - 徹夜で日記:https://matometa.link/blog/2015/05/28/activeadmindefalsebasicren-zheng-plus-aruhua/]]: redirectではなく例外を投げないととまらない?
=>例外が必要なのではなくて、おそらくredirect_to '/' したあとreturnしてないのが原因ではないだろうか。


**足跡機能の実装 [#d8bfeb3c]
-[[before_filterの実行順番について - 技術めも:http://d.hatena.ne.jp/oovu70/20111115/p1]]にあるように実装する?

*Tips [#y832ff8f]

**before_filterとbefore_actionの違いって? [#b2424dac]
-[[「壊れてねぇなら直すな」という発想はRailsにはないのかも - QA@IT公式ブログ:http://qa-it.tumblr.com/post/52191914259/%E5%A3%8A%E3%82%8C%E3%81%A6%E3%81%AD%E3%81%87%E3%81%AA%E3%82%89%E7%9B%B4%E3%81%99%E3%81%AA%E3%81%A8%E3%81%84%E3%81%86%E7%99%BA%E6%83%B3%E3%81%AFrails%E3%81%AB%E3%81%AF%E3%81%AA%E3%81%84%E3%81%AE%E3%81%8B%E3%82%82]]によると単に名前がかわったもの。
-before_actionを使えばいいらしい。

*複数のbefore_filterを適用する [#m363a8d6]
**複数のbefore_filterを適用する [#m363a8d6]
-controllerに複数列挙すれば順次適用される。
-途中でredirect_toすればそこで処理が止まる(return falseで止まるというのは過去の情報らしい?)。
--[[Railsのbefore_filterとメソッド返り値 - web-k.log:http://web-k.github.io/blog/2012/10/09/rails-filter/]]
--[[Railsでbefore_filter/before_actionがアクションを中止する仕組みを読んでみる | TechRacho:http://techracho.bpsinc.jp/baba/2013_08_06/12650]]
-books_controller.rb
#pre{{
class BooksController < ApplicationController
end
}}

*IPアドレス認証と組み合わせる [#i61eb54d]
-[[ActiveAdminでのBasic認証+アルファ - 徹夜で日記:https://matometa.link/blog/2015/05/28/activeadmindefalsebasicren-zheng-plus-aruhua/]]: redirectではなく例外を投げないととまらない?


*足跡機能の実装 [#d8bfeb3c]
-[[before_filterの実行順番について - 技術めも:http://d.hatena.ne.jp/oovu70/20111115/p1]]にあるように実装する?



トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS