如何将 CanCan 与 Rails admin 一起使用来检查所有权

发布于 2024-12-22 08:23:55 字数 234 浏览 4 评论 0原文

当我的应用程序中的用户不是管理员用户时,我只想让他们看到他们拥有所有权的字段。

是否有一个如此设置的 can :see 或类似的内容,以便它只显示使用“可以看到”的字段,或者我应该有一个名为 can 的能力:oversee 表示他们可以看到所有内容。

我想检查用户是否是rails admin中的管理员要容易得多,因此将rails admin设置为仅提取当前用户的记录。

When a user in my app isn't an admin user i want to only let them see the fields that they have ownership of.

Is there a so set can :see or something like that on a per field basis so that it displays just the fields that that use "can see", or should I have an ability called can :oversee to state that they can see everything instead.

I suppose it's much easier to just check if the user is admin or not in rails admin, so where set rails admin to only pull the current user's records.

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

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

发布评论

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

评论(1

人间不值得 2024-12-29 08:23:55

使用cancan,您可以像这样检查对象的权限:

if can?(:read, order)
  # do something!
end

if can?(:email, order)
  # do something!
end

但这仅指对象级别的可见性。

在 RailsAdmin 中,您可以通过传入块来设置字段可见性,如此处所述。

例如:

RailsAdmin.config do |config|
  config.model Order do
    list do
      field :name
      field :profit do
        visible do
          current_user.roles.include?(:admin)
        end
      end
    end
  end
end

With cancan, you can check permissions on objects like this:

if can?(:read, order)
  # do something!
end

if can?(:email, order)
  # do something!
end

But that's only referring to visibility at the object level.

In RailsAdmin, you can set field visibility by passing in a blocks, described here.

For example:

RailsAdmin.config do |config|
  config.model Order do
    list do
      field :name
      field :profit do
        visible do
          current_user.roles.include?(:admin)
        end
      end
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文