【问题标题】:CanCan authorization issueCanCan授权问题
【发布时间】:2011-05-26 07:20:14
【问题描述】:

我正在为我的应用程序使用 cancan

我的能力.rb 类是

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user

    if user.role? :admin
      can :manage, :all
    elsif user.role? :operations
      can :manage, :all
    elsif user.role? :customer_support
      can :read, :all
    else
      user.role? :marketing
    can :read, :all
    end
 end
end

我在 user.rb 中添加方法

def role?(role)
    self.roles.include? role.to_s
  end

我还添加 我的控制器中的 load_and_authorize_resource 说 products_controller 可以授权用户并允许他在此控制器中执行某些操作, 但我的问题是当用户以管理员身份登录时,他无法添加新产品,它给出了 cancan 的拒绝访问错误。

我的看法是

<% if can? :create, Product %>
              <td class="action"><%= link_to 'Show', product %></td>
              <td class="action"><%= link_to 'Edit', edit_product_path( product) %></td>
              <td class="action"><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %></td>
          <% end %>

它也没有向管理员显示此链接,因为所有管理员都可以访问,但他仍然无法访问此操作?

我还缺少什么?

请帮忙?

【问题讨论】:

    标签: ruby-on-rails authorization cancan


    【解决方案1】:

    您是否按照 cancan wiki 中的说明进行操作? https://github.com/ryanb/cancan/wiki/Role-Based-Authorization.

    Cancan 为每个用户存储角色的默认策略是使用位掩码,但 wiki 在这里提到了不同的解决方案:https://github.com/ryanb/cancan/wiki/Separate-Role-Model

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多