2010-05-27 61 views
2

我正在使用authlogic(2.1.3)和declarative_authorization(0.4.1)來控制對我的應用程序的訪問。用戶模型聲明授權'if_attribute'問題

所有授權按預期工作被分配編輯者角色的用戶不能更改他們(由authlogic提供的current_user)配置文件設置(用戶模型的一部分)。

「訪客」角色的工作方式與「管理員」一樣。

我正在使用已分配編輯器角色的用戶(名爲'bob')。在數據庫和IRB會話中驗證。

authorization_rules.rb文件的相關內容:

role :guest do 

    # allow anonymous 'user' to create an account 
    has_permission_on :users, :to => [:new, :create] 

    # allow anonymous 'user' 'read-only' actions 
    has_permission_on :users, :to => [:index, :show] 

    end 

    role :editor do 

    # allow authenticated User to see other users 
    has_permission_on :users, :to => [:index, :show] 

    # allow authenticated User to update profile; doesn't work 
    has_permission_on :user, :to => [:edit, :update] do 
     if_attribute :user => is { user } 
    end 

    end 

    role :administrator do 

    # 'full control'  
    has_permission_on :users, :to => [:index, :show, :new, :create, :edit, :update, :destroy] 

    end 

我相信問題涉及if_attribute:用戶=> {用戶}。 if_attribute似乎暗示:用戶應該是測試對象的屬性(或屬性),在這種情況下是用戶模型,而不是事物本身。我尋找一個if_self方法或類似的東西,但我什麼也沒看到。

幫助表示讚賞。

回答

3

找到解決方案;它確實與我懷疑的if_attribute方法有關。正確的角色設置是:

role :editor do 

    # allow authenticated user to see other users 
    has_permission_on :users, :to => [:index, :show] 

    # allow authenticated user to update profile 
    has_permission_on :users, :to => [:edit,:update] do 
    if_attribute :id => is { user.id } 
    end 

end