- 認証プラグインとしてデファクトスタンダードな「RESTfulAuthentication」には権限の概念がない
- timcharperさんが作った「[[role_requirement:http://github.com/timcharper/role_requirement]]」プラグインを追加してやれば権限管理ができるようになる!
- 参考: Usage - role_requirement - GitHub http://wiki.github.com/timcharper/role_requirement/usage
** インストール [#e74f99d6]
- 次のコマンドを実行
script/plugin install git://github.com/technoweenie/restful-authentication.git
script/plugin install git://github.com/timcharper/role_requirement.git
** セットアップ [#mab2b4d4]
- 次のコマンドを実行
./script/generate authenticated user sessions
./script/generate roles Role User
- Userは、authenticatedに指定したモデルクラス名
- Roleは、権限をあらわすモデル名
- 次のファイルが作らる
test/fixtures/users.yml
test/fixtures/roles.yml
app/models/role.rb
lib/role_requirement_system.rb
lib/role_requirement_test_helper.rb
lib/hijacker.rb
db/migrate/20100111002832_create_roles.rb
- テーブルを作る
rake db:migrate
- 次のテーブルができた
-- roles
-- roles_users
** 使い方 [#e34f21f2]
- rolesとusersはhabtmの関係
- rolesには「name」というカラムがあるだけ
- rolesに「admin」「user」などの値を持つレコードをいれておく
- コントローラー内に宣言的に記述
-- このコントローラーのすべてのアクションはadmin権限が必要
require_role "admin"
-- このコントローラーのindexアクション以外はadmin権限が必要
require_role "admin", :for_all_except => :index
-- このコントローラーのindexアクションはadmin権限が必要
require_role "admin", :for => :index
-- このコントローラーのすべてのアクションは「admin」と「executive」権限が必要
require_role "admin"
require_role "executive"
-- このコントローラーのすべてのアクションは「admin」と「executive」権限が必要。ただし、「index」アクションのみ「admin」権限のみでOK
require_role "admin"
require_role "executive", :except => [:index]
-- このコントローラーのすべてのアクションは「admin」権限が必要。さらに「create」「update」「edit」「destroy」は「executive」権限も必要
require_role "admin"
require_role "executive", :only => [:create, :update, :edit, :destroy]
-- このコントローラーのすべてのアクションは「admin」権限、または「executive」権限が必要
require_role ["admin", "executive"]
-- このコントローラーの「list」アクションと「show」アクション以外はadmin権限が必要
require_role "admin", :except => [:list, :show]
-- このコントローラーの「delete」アクションと「edit」アクションのみadmin権限が必要
require_role "admin", :only => [:delete, :edit]
- current_userに指定されたroleが紐づいていれば、要求されたアクションが実行される
- 紐づいていなければ AuthenticatedSystemのaccess_denied が呼ばれる
** 常に「admin」というroleの人はなんでも許可されるようにしたい [#p98a2311]
- デフォルトでそうなっている
- app/model/user.rbに次のメソッドが追加されるているため
def has_role?(role_in_question)
@_list ||= self.roles.collect(&:name)
# return true if @_list.include?("admin") #This is the culprit.
(@_list.include?(role_in_question.to_s) )
end
#blikifooter(志田)
tag: [[Ruby on Rails>tag/Ruby on Rails]]