简体   繁体   中英

cancan ability : allow site admin with rolify role create_user to create or sign_up new devise users

For the application, I am trying to implement job_code style access using cancan/cancancan, devise and rolify.

Only site admins with job_code :create_user will be able to create new users

Following is the code:

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

in routes.rb, I have

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

Application 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

When I use, the following code in abilities.rb

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

it works perfectly . It takes me to User sign_up page

but, when I have this code

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

it says Access Denied (403 page)

I am having a hard time to figure out, what should I use instead of User in can :create, User

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM