CanCan 中的嵌套资源

发布于 2024-11-17 15:31:27 字数 331 浏览 4 评论 0原文

我有这些资源

resources :companies do
  resources :stands
end

,并且我想控制公司展位的访问权限。在能力课程中,我写了

   can :manage, :all if user.has_role? Role.super_admin

    can :manage, Company do |c|
      user.has_role? Role.company_admin, c
    end

如何控制公司展位的访问?例如,公司管理员只能搜索他的公司展位。谢谢

I have that resources

resources :companies do
  resources :stands
end

And i want to control access for company stand. In Ability class i write

   can :manage, :all if user.has_role? Role.super_admin

    can :manage, Company do |c|
      user.has_role? Role.company_admin, c
    end

How i can control access for company stands? For example company admin can search only his company stands. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找我们的幸福 2024-11-24 15:31:27

Cancan 将帮助我们设置操作权限。它不会根据为Ability.rb 设置的权限来获取您的记录

Ex:

URL:
/stands/搜索

def search
  authorize! :search, Stand
  current_user.company.search_stands('some-query') # This will get the stands only for the current-users's company  
end

能力.rb
可以:搜索、站立
用户.has_role?角色.company_admin
其他示例


/companies/1/stands/搜索

def search
  @company = Company.find(params[:company_id])
  authorize! :search_stands, @company
  @stands = @company.search_stands('some-query')
end

能力.rb

can :search_stands, Company do |c|
  user.has_role? Role.company_admin # Only admin has the permission to search stands.
end

Cancan will help us in setting permissions for actions. It wont fetch you records based on the permissions set it the Ability.rb

Ex:

URL:
/stands/search

def search
  authorize! :search, Stand
  current_user.company.search_stands('some-query') # This will get the stands only for the current-users's company  
end

Ability.rb
can :search, Stand
user.has_role? Role.company_admin
end

Other Example:
/companies/1/stands/search

def search
  @company = Company.find(params[:company_id])
  authorize! :search_stands, @company
  @stands = @company.search_stands('some-query')
end

Ability.rb

can :search_stands, Company do |c|
  user.has_role? Role.company_admin # Only admin has the permission to search stands.
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文