Ruby on rails 4 如何使用Pundit和Rolify限制对RubyonRails4中模型的访问

Ruby on rails 4 如何使用Pundit和Rolify限制对RubyonRails4中模型的访问,ruby-on-rails-4,devise,rolify,pundit,awesome-nested-set,Ruby On Rails 4,Devise,Rolify,Pundit,Awesome Nested Set,在过去的几个月里,我刚刚跳槽,在我目前的项目中遇到了第一个真正的障碍,我一直无法找到答案 resources :categories do resources :articles do resources :microposts, only: [:new, :create, :edit, :destroy end end 我的目标是实现一些细粒度的控制,控制哪些用户角色/组(由Rolify生成并由Pundit控制)可以通过Upmin管理控制台访问特定的类别、子类别或文章。类别/

在过去的几个月里,我刚刚跳槽,在我目前的项目中遇到了第一个真正的障碍,我一直无法找到答案

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
我的目标是实现一些细粒度的控制,控制哪些用户角色/组(由Rolify生成并由Pundit控制)可以通过Upmin管理控制台访问特定的类别、子类别或文章。类别/文章应能够允许多个用户角色/组访问其内容

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
答案演示了将角色范围限定到模型的特定实例,这很好,但我也希望通过我的应用程序管理控制台Upmin_admin中的一个简单复选框表单从模型实例获得该控件,以供感兴趣的人使用

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
我认为,除了在管理控制台中创建一个category/article实例视图partial之外,没有太多的功能可以实现,该视图列出了该特定category/article实例的所有角色/组及其当前CRUD设置。还是我遗漏了一些中间步骤

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
如果能朝着正确的方向轻推一下,我们将不胜感激。 谢谢

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
我的应用程序的一些背景:

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
在我的web应用程序中,我有嵌套的资源、类别、文章和注释,如下所示:

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
我知道嵌套资源的深度不能超过一个级别,但是我只是跳上了Rails的火车,而且:shallow=true没有提供我想要的结果

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
类别作为嵌套集,由Awesome嵌套集提供,能够容纳类别和文章以及扩展注释

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end

通过LDAP服务器使用Desive对用户进行身份验证-我将在不久的将来配置此功能,以自动将用户分配到正确的组/角色。

通过重新阅读Rolify,我找到了解决问题的开始

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
每当用户创建类别、子类别或文章时,我都会运行

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
user.add_role :current_role Model.Instance_id
然后我可以通过获取实例id从管理门户查询。然后通过查询系统中的所有用户角色并将它们与实例关联的角色进行比较,我可以为管理控制台创建部分视图

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
model_instance = Model.find(instance_id)
model_instance.roles #returns all of the roles associated with that instance
我还需要创建一些方法来处理角色的MassaAssignment/reassignment,以便在取消选中复选框时达到预期结果,例如向实例添加角色/组,反之亦然。可能有一些类似于ruby风格的伪代码要遵循

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
users = User.with_any_role(:some_role)

def assignRoleToModel(model_instance, users, role)
  if model_instance.roles.empty?
    users.each { |u| u.add_role creatorRole model_instance }
  end
  flash[:warning] = "#{Model_instance.name} already has that role assigned to it!"
end
其中model_实例是我要控制组/角色访问权限的模型的实例,users是具有我要添加到model_实例的角色的用户列表,role是我希望允许访问model_实例的角色

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
更新

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end
如何通过表单控制角色的示例

resources :categories do
  resources :articles do
    resources :microposts, only: [:new, :create, :edit, :destroy
  end
end