ruby-on-rails - 用户模型声明性授权 'if_attribute'问题

标签 ruby-on-rails authlogic declarative-authorization

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

所有授权均按预期工作,异常(exception),被分配编辑者角色的用户无法更改其(由 authlogic 提供的 current_user)配置文件设置(用户模型的一部分)。

“ guest ”角色按预期工作,“管理员”也是如此。

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

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 :user => { user } 有关。 if_attribute 似乎表明 :user 应该是正在测试的事物的属性(或属性),在本例中是用户模型,而不是事物本身。我寻找了 if_self 方法或类似的方法,但我没有看到任何东西。

感谢帮助。

最佳答案

找到了解决方案;正如我怀疑的那样,它确实与 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

关于ruby-on-rails - 用户模型声明性授权 'if_attribute'问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2924043/

相关文章:

ruby-on-rails - authlogic 和 oauth-plugin

ruby-on-rails - Authlogic 超时自动注销

ruby-on-rails - 使用声明性授权对嵌套资源进行属性检查

ruby-on-rails - 如何覆盖 Rails 中验证错误中的 html 格式?

ruby-on-rails - Ruby on Rails - 缓存变量

ruby-on-rails - Rails 缓存不返回对象,而是字符串

ruby-on-rails - 如何将参数发送到回形针处理器

ruby-on-rails - Redis Rails 身份验证如何工作?

ruby-on-rails - 所有具有 declarative_authorization 的权限

ruby-on-rails - 创建完成后,设置角色