3

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

Naim Rajiv
  • 3,324
  • 2
  • 17
  • 23
gkolan
  • 1,571
  • 2
  • 20
  • 37

1 Answers1

0

I had to add :read along with :create permissions to access new/create actions, then parse down :read access

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
E. Ro
  • 1
  • 2