Move the access control to a dedicated helper

This commit is contained in:
Jyri-Petteri Paloposki 2020-08-18 11:51:16 +03:00
parent 95cb7ec799
commit 2d4ba48ad4
2 changed files with 8 additions and 6 deletions

View file

@ -154,6 +154,13 @@ class ApplicationController < ActionController::Base
end
end
def admin_or_self_login_required
unless User.find(session['user_id']).is_admin || session['user_id'] == params[:id]
render :body => t('errors.user_unauthorized'), :status => 401
return false
end
end
def redirect_back_or_home
respond_to do |format|
format.html { redirect_back_or_default root_url }

View file

@ -1,6 +1,7 @@
class UsersController < ApplicationController
before_action :admin_login_required, :only => [ :index, :show ]
before_action :admin_or_self_login_required, :only => [ :destroy ]
skip_before_action :login_required, :only => [ :new, :create ]
prepend_before_action :login_optional, :only => [ :new, :create ]
@ -132,12 +133,6 @@ class UsersController < ApplicationController
def destroy
@deleted_user = User.find(params[:id])
# Check that the user has access (logged in as admin or the target user.)
unless current_user && (current_user.is_admin || current_user == @deleted_user)
render :body => t('errors.user_unauthorized'), :status => 401
return
end
# Remove the user
@saved = @deleted_user.destroy