How to allow only the administrator to register user with Devise

1

I wanted to know if there is how only the administrator register user, with Devise. I can not program this. And where is the controller with Devise's methods in the project?

    
asked by anonymous 16.04.2015 / 14:00

1 answer

0

Devise is an engine. What is an engine? Although it looks a lot like a normal plugin, an engine is actually a kind of application that you "mount" in your application.

Normal plugins only add features to your app. Already engines, can have their own controllers, models, views, etc. It's just that you can not see it because it's in a separate, invisible namespace for you.

According to the Devise documentation, you can overwrite the Devise SessionsController as follows:

  • Run rails generate devise:controllers [scope] , where "scope" is the name of your user table (as users )
  • Now Devise has created for you a controller in app/controllers/[scope]/ , which inherits the original controller inheritance, and allows you to overwrite its methods.

  • Configure routes to use this new driver, not the original:
  • config/routes.rb :

    devise_for :users, controllers: {sessions: '[scope]/sessions'}
    
  • If you are using custom views in app/views/devise , copy to app/views/[scope]
  • To solve your problem, I think a before_action resolves:

    class Users::SessionsController < Devise::SessionsController
      before_action :verifica_permissao, only: [...] # configure os métodos que você quer
    
      def verifica_permissao
        possui_permissao? || redirect_to('/')
      end
    
      def possui_permissao?
        # aqui você verifica se o usuário é admin
      end
    end
    

    References:

    16.04.2015 / 16:19