gpt4 book ai didi

ruby-on-rails - 康康舞能力 : allow site admin with rolify role create_user to create or sign_up new devise users

转载 作者:行者123 更新时间:2023-12-05 07:57:10 24 4
gpt4 key购买 nike

对于应用程序,我正在尝试使用 cancan/cancancan、devise 和 rolify 来实现 job_code 样式的访问。

只有拥有 job_code :create_user 的站点管理员才能创建新用户

代码如下:

class RegistrationsController < Devise::RegistrationsController 
before_filter :check_permissions, :only => [ :new, :create, :cancel ]
skip_before_filter :require_no_authentication

def check_permissions
authorize! :create, resource
end
end

class Ability
include CanCan::Ability

def initialize(user)
alias_action :create, :read, :update, :destroy, :to => :crud

if user.has_role? :create_user
can :create, User
end

if user.has_role? :create_annoucement
can :create, Announcement
end
end
end

在 routes.rb 中,我有

devise_for :users ,:controllers => { :registrations => "registrations" }, :path_names => {:sign_in => "login", :sign_out => "logout"}, :path => "account"

应用程序 Controller

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :authenticate_user!
check_authorization unless: :devise_controller?
before_filter :set_start_time if Rails.env.development?
before_action :configure_permitted_parameters, if: :devise_controller?

rescue_from CanCan::AccessDenied do |exception|
#https://github.com/ryanb/cancan/wiki/Devise
if current_user.nil?
session[:next] = request.fullpath
puts session[:next]
redirect_to new_user_session_path, :alert => "You have to log in to continue."
else
render file: "#{Rails.root}/public/403", formats: [:html], status: 403, layout: false
end
end

def set_start_time
@start_time = Time.now.usec
end

def configure_permitted_parameters
#devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:name, :email) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password, :password_confirmation) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password, :user_manual) }
end

end

我使用的时候,abilities.rb中有如下代码

if user.has_role? :create_user 
can :create, :all
end

完美地工作。它带我到用户注册页面

但是,当我有这段代码时

if user.has_role? :create_user 
can :create, User
end

它说访问被拒绝(403 页面)

我很难弄清楚,我应该用什么代替 can :create, User 中的 User

最佳答案

我必须添加 :read:create 访问新/创建操作的权限,然后解析 :read 访问

关于ruby-on-rails - 康康舞能力 : allow site admin with rolify role create_user to create or sign_up new devise users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27791928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com