我想在一个控制器(home)上跳过Pundit 的policy_scope要求.我试过这个:
class ApplicationController < ActionController::Base
include Pundit
after_action :verify_authorized, :except => :index, unless: :devise_controller?
after_action :verify_policy_scoped, :only => :index, unless: controller.controller_name == "home"
end
class HomeController < ApplicationController
def index
redirect_to (new_user_session_path) unless user_signed_in?
if user_signed_in?
@user=current_user
end
end
end
Run Code Online (Sandbox Code Playgroud)
但我认为控制器尚未定义或其他什么?有什么想法或建议吗?
在我的一个项目中,我开始使用 pundit gem,并且我有一个非常简单的策略,如下所示:
class CompanyPolicy < ApplicationPolicy
def index?
true if user.is_a? Administrator
end
def new?
true if user.is_a? Administrator
end
def create?
new?
end
def edit?
true if user.is_a? Administrator
end
def update?
edit?
end
end
Run Code Online (Sandbox Code Playgroud)
问题是我怎样才能避免重复这样的事情:
true if user.is_a? Administrator
Run Code Online (Sandbox Code Playgroud) 我的提案控制器有以下操作:
def show
@proposal = Proposal.find(params[:id])
authorize @proposal
end
Run Code Online (Sandbox Code Playgroud)
我有以下政策:
class ProposalPolicy
attr_reader :current_user, :proposal
Run Code Online (Sandbox Code Playgroud)
如何重定向到特定页面.如果在尝试转到显示页面时拒绝权限,请说索引提案或根页?
当我在没有正确权限的情况下导航到它时,我只会得到一个带有以下内容的rails错误页面:
not allowed to show? this<proposal OBJ ispsum lorem>
Run Code Online (Sandbox Code Playgroud)
我只是希望他们有一个简单的通知并重定向到另一个页面.什么是最好的方法呢?我在show视图中猜测某种if语句但到目前为止还没有任何工作.
def initialize(current_user, proposal)
@current_user = current_user
@proposal = proposal
end
def show?
@proposal.published? or @proposal.proposer == @current_user
end
end
Run Code Online (Sandbox Code Playgroud) model-view-controller authorization ruby-on-rails ruby-on-rails-4 pundit
我的创建、更新和销毁“喜欢”的政策要求用户登录。
我对该政策的措辞如下:
class LikePolicy < ApplicationPolicy
def create?
user && record.user_id == user.id
end
def update?
create?
end
def destroy?
create?
end
end
Run Code Online (Sandbox Code Playgroud)
我的 like#create 控制器操作如下:
def create
@article = Article.find(params[:article_id])
@like = @article.likes.new(user_id: current_user.id, value: params[:value] == 1 : 1 ? -1)
authorize(@like)
@like.save
redirect_to @article
end
Run Code Online (Sandbox Code Playgroud)
然而,使用策略来确认用户已登录是不合逻辑的,因为代码将在前面的代码中失败,例如引用 current_user 的地方。
这是如何授权包含 user_id 的记录的公认做法吗?
我使用这个Pundit宝石相当新,但似乎无法理解政策系统.从我读过的所有内容看起来都是正确的,尽管我仍然收到错误
应用控制器
class ApplicationController < ActionController::Base
include Pundit
protect_from_forgery
before_filter :authenticate_person!
# Verify that controller actions are authorized. Optional, but good.
after_filter :verify_authorized, except: :index
after_filter :verify_policy_scoped, only: :index
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
private
def pundit_user
Person.find_by_id(current_person)
end
def user_not_authorized
flash[:alert] = "You are not authorized to perform this action."
# redirect_to(request.referrer || root_path)
end
end
Run Code Online (Sandbox Code Playgroud)
申请政策
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
raise Pundit::NotAuthorizedError, "must be logged in" unless user
@user = user
@record = record
end …
Run Code Online (Sandbox Code Playgroud) 我有一个现有的Rails应用程序,用于Devise
验证User
模型并Pundit
根据Enrollment
链接User
到我的Company
模型的模型进行身份验证.双方User
并Company
都在公寓宝石的公共架构.我不怀疑公寓是问题的一部分,但我想我会提到它.
我使用AdminUser类添加了Active Admin - 我希望将我的管理员用户与应用用户分开.
如果我尝试访问/admin
或/admin/dashboard
获得:
Pundit::PolicyScopingNotPerformedError at /admin/users
Pundit::PolicyScopingNotPerformedError
Run Code Online (Sandbox Code Playgroud)
如果我尝试像/admin/users
Pundit 这样的模型似乎忽略了active_admin策略并转到主应用程序策略.在我的情况下,应用程序会抛出异常,因为它是在等一个Enrollment
VS的AdminUser
.
如果我禁用:
##/config/initializers/active_admin.rb
config.authorization_adapter = ActiveAdmin::PunditAdapter
##/controllers/application_controller
after_action :verify_authorized, except: [:landing, :dashboard], unless: :devise_controller?
after_action :verify_policy_scoped, only: [:index]
Run Code Online (Sandbox Code Playgroud)
一切正常,但后来我在主应用程序中失去了Pundit等.
这是我的代码的要点:
https://gist.github.com/jasper502/4b2f1b8b6f21a26c64a5
以下是可以在此问题上找到的相关帖子:
https://gorails.com/forum/using-pundit-with-activeadmin
我想在这篇文章中一起禁用Pundit(你可以用Devise和Active Admin禁用Pundit吗?)但是这样做会很好.
UPDATE
我已经解决了,但我仍然不知道这是否应该开箱即用,我有一些奇怪的问题导致所有这一切.要点已更新.
我最终使用:
https://viget.com/extend/8-insanely-useful-activeadmin-customizations
还有一点:
有条件的before_action/before_filter的文档
以下是一点答案.我在过滤器中使用过强制AA来强制AA对AA内的资源和集合进行授权.接下来是添加策略范围,但我的大脑现在伤得太厉害了.
我还必须添加另一个过滤器来绕过仪表板上的身份验证,因为它是无头的.到目前为止似乎工作.
更新2
嗯......我想我说的太快了.这一切都只有在我作为常规登录时才有效User
- 如果我退出,它会再次崩溃.
我目前正在使用带有CanCanCan 1.13.1的Rails 4.1.14,并在模型/记录级别上定义了细化权限.管理员可以管理所有文章,但用户只能编辑他们撰写的文章.
为了防止普通用户编辑特定字段,我根据角色在rails_admin中显示字段.
visible do
bindings[:object].id == bindings[:view].current_user.roles.include? :admin
end
Run Code Online (Sandbox Code Playgroud)
我也使用https://github.com/aasm/aasm gem并创建自定义操作,以便用户可以将记录移动到新状态.
但我真正想要的是根据用户的角色/记录启用字段级权限.我在CanCanCan或https://github.com/elabs/pundit页面上找不到任何文档.
有人有经验吗?
我有2个控制器,1个用户,1个用于管理员.
控制器/ articles_controller.rb
class ArticlesController < ActionController::Base
...
def show
@article = Article.find(parmas[:id])
authorize @article
end
...
end
Run Code Online (Sandbox Code Playgroud)
控制器/管理/ articles_controller.rb
class Admin::ArticlesController < AdminController
...
def show
@article = Article.find(parmas[:id])
authorize @article
end
...
end
Run Code Online (Sandbox Code Playgroud)
我有2个文件策略策略/ article_policy.rb
class ArticlePolicy
extend ActiveSupport::Autoload
autoload :Admin
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
def show?
# allow show for every user.
true
end
end
Run Code Online (Sandbox Code Playgroud)
和一个文件policy/admin/article_policy.rb
class Admin::ArticlePolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record …
Run Code Online (Sandbox Code Playgroud)
我正在使用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"
Run Code Online (Sandbox Code Playgroud)
申请政策:
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
end
def new?
create?
end
def update?
false
end
def edit?
update?
end
def destroy?
false
end
def rails_admin?(action)
case action
when :dashboard …
Run Code Online (Sandbox Code Playgroud) 我正在使用带有 Rails 的Pundit,并且我有一个控制器,我需要完全限制特定用户角色。我的角色是“员工”和“消费者”。工作人员应该拥有对控制器的完全访问权限,但消费者应该没有访问权限。
有没有一种方法可以做到这一点,而不是一个一个地限制每个动作?
例如,这是我的政策:
class MaterialPolicy < ApplicationPolicy
attr_reader :user, :material
def initialize(user, material)
@user = user
@material = material
end
def index?
user.staff?
end
def show?
index?
end
def new?
index?
end
def edit?
index?
end
def create?
index?
end
def update?
create?
end
def destroy?
update?
end
end
Run Code Online (Sandbox Code Playgroud)
还有我的控制器:
class MaterialsController < ApplicationController
before_action :set_material, only: [:show, :edit, :update, :destroy]
# GET /materials
def index
@materials = Material.all
authorize @materials
end
# GET /materials/1
def …
Run Code Online (Sandbox Code Playgroud)