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?
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?
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:
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.
config/routes.rb
:
devise_for :users, controllers: {sessions: '[scope]/sessions'}
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