[英]declarative_authorization permissions on roles
我正在嘗試將授權添加到已經存在的相當大的應用程序中,但我必須稍微模糊一些細節。
這是背景:
在我們的應用程序中,我們有一些或多個層次結構的角色,大致如下:
BasicUser -> SuperUser -> Admin -> SuperAdmin
對於授權,每個用戶模型實例都具有與上面對應的屬性“角色”。
我們有一個RESTful控制器“Users”,它在Backoffice下命名。 所以簡而言之就是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'
他們得到同樣的效果。 有人有什么建議嗎?
我相信你現在已經解決了這個問題,但是我們遇到了類似的問題並找到了可能有所幫助的解決方案。 可能無法僅在聲明性授權DSL中處理此案例,但您可以利用DSL在模型和視圖中執行正確的操作。 基本上,我們需要訪問角色層次結構圖。
線索是declarative_authorization有一個漂亮的控制器,它生成一個顯示角色層次結構的圖表。 使用相同的支持代碼,您可以輕松訪問任何角色的祖先:
class Role < ActiveRecord::Base
require 'declarative_authorization/development_support/analyzer'
has_many :assignments
has_many :users, :through => :assignments
validates :name, :presence => true
validates :name, :uniqueness => true
def ancestors
Authorization::DevelopmentSupport::AnalyzerEngine::Role.for_sym(self.name.to_sym,
Authorization::Engine.instance).ancestors.map { |r| r.instance_variable_get("@role") }
end
def self_and_ancestors
ancestors << self.name.to_sym
end
end
然后,您可以使用此選項來執行僅在用戶編輯器中提供與current_user角色相同或較差的角色選擇,並拒絕訪問或不允許將模型更改為試圖不恰當地提升用戶的人。 它在聲明性授權DSL本身的上下文中沒有那么有用,因為它需要首先被解析,創建一種循環引用。
希望這可以幫助那些需要它的人。
我在我的應用程序中有以下方法,它的工作原理
role :super_user do
includes :basic_user
has_permission_on :backoffice_users do
to :edit
if_attribute :role => is {"basic_user"}
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.