2

我正在使用 authlogic (2.1.3) 和 declarative_authorization (0.4.1) 来控制对我的应用程序的访问。

除了分配了 Editor 角色的用户无法更改其(authlogic 提供的 current_user)配置文件设置(用户模型的一部分)之外,所有授权都按预期工作。

'Guest' 角色按预期工作,'Administrator' 也是如此。

我正在使用已分配编辑角色的用户(名为“bob”)。在数据库和 IRB 会话中验证。

authentication_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 :user => { user } 有关。if_attribute 似乎暗示 :user 应该是被测试事物的属性(或属性),在这种情况下是用户模型,而不是事物本身。我寻找了 if_self 方法或类似的方法,但我什么也没看到。

帮助表示赞赏。

4

1 回答 1

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
于 2010-05-27T19:46:54.153 回答