In the devise_for module we have right now, we have at least two roles: admin and user. My goal is that at any given time, there is one admin, and only admin can create/delete other users. I've followed this post so that I have to login and have permissions to create a new user, since by default the sign_up
page needs no permission. However, right now there is no difference between an admin and a user, meaning that both roles can create others, which is not my desired functionality. What should I do so that only an admin can create other users, i.e., when accessing /users/sign_up under the role of a user, it will pop out an error like "not enough permission?"
Let me give you what I have right now:
app/policies/user_policy.rb:
class UserPolicy
attr_reader :current_user, :model
def initialize(current_user, model)
@current_user = current_user
@user = model
end
def index?
@current_user.admin?
end
def new?
@current_user.admin?
end
def show?
@current_user.admin? or @current_user == @user
end
def create?
@current_user.admin?
end
def update?
@current_user.admin?
end
def destroy?
return false if @current_user == @user
@current_user.admin?
end
end
app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
prepend_before_action :require_no_authentication, only: [:cancel]
prepend_before_action :authenticate_scope!, only: [:new, :create, :edit, :update, :destroy]
def new
super
end
end
config/routes.rb:
...
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users
...
P.S. I try to see what I can do for the original code devise/registrations_controller.rb
[link], but didn't see anything obviously enough for me to change...