将Cancan权限序列化为emberjs

时间:2012-11-11 08:35:41

标签: ruby-on-rails-3.2 ember.js

我想知道是否有人知道如何将CanCan权限序列化为emberjs。我在Jo_Liss看到了这个StackOverflow Q& A,她解释了如何 Serialize permissions (e.g. CanCan) with active_model_serializers ,但在那个级别停了下来。同样 this article linked to here, talks on how to check permission on the emberjs side ,但没有关于如何将权限从后端序列化为emberjs的方法。

我希望有3种用户角色,即'admin','author','user',并且希望在emberjs上使用CanCan权限来控制他们在登录路线中可以访问的内容。

1 个答案:

答案 0 :(得分:9)

您可以使用UserSerializer序列化用户CanCan Ability:

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :email, :ability

  def ability
    Ability.new(self).as_json
  end
end

这将返回类似的内容:

user: {
  id: 1,
  name: "xxx",
  email: "xxx",
  ability: rules: [
    {
      match_all: false,
      base_behavior: false,
      actions: [
        "edit"
      ],
      subjects: [
        "user"
      ],
      conditions: { },
      block: null
    },
    {
      match_all: false,
      base_behavior: true,
      actions: [
        "read"
      ],
      subjects: [
        "user"
      ],
      conditions: { },
      block: null
    }
  ]
}

您需要实施某种罐头吗?不能?帮助javascript来处理这些功能。

希望有所帮助。