我試圖給一個已經存在的相當大的應用程序添加授權,但我必須混淆一下細節。對角色的declarative_authorization權限
這裏的背景:
在我們的應用程序,我們有一個是分層的數字或角色,大致是這樣的:
BasicUser -> SuperUser -> Admin -> SuperAdmin
若需授權每個用戶模型實例有一個屬性「角色」這對應於以上。
我們有一個RESTful控制器「用戶」,在後臺下命名空間。簡而言之,它是Backoffice :: UsersController。
class Backoffice::UsersController < ApplicationController
filter_access_to :all
#... RESTful actions + some others
end
所以這裏的問題:
我們希望用戶能夠給用戶編輯用戶,但只有當他們有一個「小」的角色比他們目前擁有的權限。我創建了一個authorization_rules.rb
authorization do
role :basic_user do
has_permission_on :backoffice_users, :to => :index
end
role :super_user do
includes :basic_user
has_permission_on :backoffice_users, :to => :edit do
if_attribute :role => is_in { %w(basic_user) }
end
end
role :admin do
includes :super_user
end
role :super_admin do
includes :admin
end
end
以下不幸的是這是據我得到的,規則似乎並沒有得到應用。
- 如果我評論的排除,沒有人可以編輯
- 如果我離開了規則,你可以編輯大家
我也試了一對夫婦在if_attribute變化:
if_attribute :role => is { 'basic_user' }
if_attribute :role => 'basic_user'
並且它們獲得相同的效果。有人有任何建議嗎?