Tag: pundit

使用Pundit的各种角色的索引视图限制

我正在尝试为三个角色创建一个show view。 管理员,超级用户和用户。 管理员应该看到所有用户。 超级用户应该只看到用户,用户不应该看到任何人。 当我在解析else user.super_user?时使用注释掉的策略方法else user.super_user? 会给我unsupported: TrueClass错误。 欢迎任何建议。 用户控制器 def index @users = policy_scope(User) authorize User end 用户政策 class UserPolicy attr_reader :current_user, :model def initialize(current_user, model) @current_user = current_user @user = model end class Scope attr_reader :user, :scope def initialize(user, scope) @user = user @scope = scope end def resolve if user.admin? […]

Active Model Serializer和Pundit在显示CRUD操作期间删除记录

好的,这里有些东西被严重破坏了…… 我正在为我的Rails 5 JSONAPI服务器使用Active Model Serializer和Pundit,为我的前端应用程序使用Ember。 我有User模型和User模型的Pundit策略,可以防止非作者查看未发表的故事和章节。 目前,我看到一个奇怪的问题,如下所示: 1. UserA creates StoryA, and two published chapters Chapter1 and Chapter2 2. UserA then creates two unpublished chapters Chapter3 and Chapter4 3. UserA logouts 4. UserB logins 5. UserB views the same story created by UserA 6. Server policy kicks in and scope the results to only […]

为什么before_action:authorize以’错误的参数数’失败?

我已经和Devise一起成立了Pundit,以便对我的申请进行授权。 在我的一个控制器中,我有before_action :authorize 。 然后我进行了以下测试: describe SomeController do before(:each) do login_user(FactoryGirl.create(:user, :user_type => :admin)) end describe “GET index” do it “it retrieves the index” do something = FactoryGirl.create(:Something) get :index assigns(:something).should eq([something]) end end end 我收到错误: wrong number of arguments (0 for 1) 登录助手非常简单: module ControllerMacros def login_user(user) if user.nil? user = FactoryGirl.create(:user) end @request.env[“devise.mapping”] […]

Rails_admin和pundit:#RailsAdmin :: MainController的未定义方法`policy’

我正在使用rails 5,我正在尝试使用pundit为我的rails_admin面板实现授权。 所以我在我的应用程序控制器中包含了pundit并安装了rails_admin_pundit gem ,你可以在我的Gemfile的这个片段中看到: gem ‘devise’ gem ‘devise-i18n’ gem ‘rails_admin’, ‘~> 1.0’ gem ‘rails_admin-i18n’ gem ‘rails_admin_tag_list’, github: ‘kryzhovnik/rails_admin_tag_list’ gem ‘pundit’ gem “rails_admin_pundit”, :github => “sudosu/rails_admin_pundit” 申请政策: class ApplicationPolicy attr_reader :current_user, :record def initialize(current_user, record) @user = current_user @record = record end def index? false end def show? scope.where(:id => record.id).exists? end def create? false […]

Rails 4 – Pundit – 制定政策

我正试图弄清楚如何在我的Rails 4应用程序中使用pundit。 我有一个配置文件视图,我想在其中显示一个链接来创建一个新项目,但需要获得权威授权。 我尝试了以下每种配方: “btn btn-info” %> 项目和简介之间的关联是: 项目 belongs_to :profile 轮廓 has_many :projects, dependent: :destroy 我的项目政策有: def new? true # create? end def create? true end 当我在配置文件视图中尝试使用此行时: 我收到一条错误消息: 错误的参数数量(给定2,预期为0) 当我在配置文件视图中尝试使用此行时: 我收到一条错误消息: 错误的参数数量(给定2,预期为0) 当我在配置文件视图中尝试使用此行时: 我收到一条错误消息: 无法找到零的政策 当我在配置文件视图中尝试使用此行时: 我收到一条错误消息: 未定义的局部变量或方法`project’for 你的意思是? project_url 如果视图页面在不同的模型中(例如,配置文件,测试该配置文件的授权是否可以创建项目),我是否需要做一些特殊的测试项目授权? 我被困在猜测如何解决这个问题。 在我的项目控制器中,我有一个创建方法: def create @project = Project.new(project_params) @project.profile = current_user.profile respond_to do |format| […]