devisecustomizationdeclarative-authorization

Devise + Declarative_authorization + role_model + different users model name : undefined method `current_user'


I do have this famous error : "undefined method `current_user'" with declarative authorization, though I set up this variable in the application_controller.rb :

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :set_current_user

  protected

    def set_current_user
      Authorization.current_user = current_admin_utilisateur
    end
end

I'm using a table called "admin_utilisateurs" instead of "users". Which was activated in Devise with : "rails generate devise admin_utilisateur"

Devise is working great.

For info, I customized my users table (admin_utilisateurs) with "roles_model" gem, So that I do have an attribut roles_mask that allows me to manage different roles while providing a role_symbols method for declarative authorization.

The problem is now that I got this strange error though the Authorization.current_user is set by the application_controller.rb.

This is the begning of one my resource controllers that procude the error :

class PubResponsablesController < ApplicationController

  before_filter :authenticate_admin_utilisateur!
  filter_resource_access

  ...
end

I search by google for this error, but none of the results provide a working solution. Could anybody help me on this ?

Many Thanks


Solution

  • Ok this is the final answer.

    I modified my app/controller/application_controller.rb because I don't use the @current_user instance variable in the views :

    class ApplicationController < ActionController::Base
      protect_from_forgery
    
      # This is mandatory if you want to secure as well your app/models
      before_filter :set_current_user
    
      # This method is required by declarative_authorization on every controller
      #  that is using filter_resource_access (or any other declarative_auth.. mechanism)
      def current_user
        current_admin_utilisateur
      end
    
      protected
    
      def set_current_user
        Authorization.current_user = current_admin_utilisateur
      end
    end
    

    As I said I'm using the following gem in collaboration :

    gem devise for the authentication The user-model-name is "admin_utilisateur" instead of "user", but it could have been : account, member, group or what you need.

    gem role_model to provide a brillant role method "role_symbols" to my user model *The method role_symbols was returning a "Set" subclass instead of an "Array" but after quick post on Github, the developer (martinrehfeld) fixed this compatibility issue in a lightning matter of minutes. Great !*

    gem declarative_authorization to provide access management based on roles. My will to use a different model name than "user" is confirmed to work by the following post.

    The only thing that declarative_authorization needs is the current_user method on each controller. As I'm using a different model name with Devise (such as admin_utilisateur, account, member, ...) the helper created by devise have a different name. Instead of current_user, it is current_admin_utilisateur (or current_account, current_member). So I have to create my own current_user method.

    The role_symbols method required by declarative_authorization is provided by role_model gem.

    I hope this will help other developer cause I spent two days to sort out how all this fabric works together. Devise took me even more with routing issues.

    My few cents to RoRrrr ;-)