forked from fixin.me/fixin.me
Add access control and :forbidden error handling
This commit is contained in:
parent
461f0bb812
commit
634ba7e901
@ -1,6 +1,15 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
helper_method :current_user_at_least
|
||||
|
||||
before_action :authenticate_user!
|
||||
|
||||
class AccessForbidden < StandardError
|
||||
end
|
||||
|
||||
def current_user_at_least(status)
|
||||
User.statuses[current_user.status] >= User.statuses[status]
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def after_sign_out_path_for(scope)
|
||||
|
@ -1,7 +1,9 @@
|
||||
class UsersController < ApplicationController
|
||||
before_action :set_user, only: %i[ show edit update destroy ]
|
||||
before_action :find_user, only: [:show, :edit, :update, :destroy]
|
||||
before_action do
|
||||
raise AccessForbidden unless (current_user == @user) || current_user_at_least(:admin)
|
||||
end
|
||||
|
||||
# GET /users
|
||||
def index
|
||||
@users = User.all
|
||||
end
|
||||
@ -47,7 +49,7 @@ class UsersController < ApplicationController
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_user
|
||||
def find_user
|
||||
@user = User.find(params[:id])
|
||||
end
|
||||
|
||||
|
@ -34,7 +34,10 @@
|
||||
<div class="nav-menu">
|
||||
<% if user_signed_in? %>
|
||||
<div class="right">
|
||||
<%= image_link_to t('.users'), "account-multiple-outline", users_path, current: :active %>
|
||||
<% if current_user_at_least(:admin) %>
|
||||
<%= image_link_to t('.users'), "account-multiple-outline", users_path,
|
||||
current: :active %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
@ -1,21 +1,23 @@
|
||||
<table class="items" id="users">
|
||||
<tr>
|
||||
<th><%= User.human_attribute_name(:email).capitalize %></th>
|
||||
<th><%= User.human_attribute_name(:status).capitalize %></th>
|
||||
<th><%= User.human_attribute_name(:created_at).capitalize %> <sup>UTC</sup></th>
|
||||
<th><%= User.human_attribute_name(:confirmed_at).capitalize %></th>
|
||||
<th><%= t :actions %></th>
|
||||
</tr>
|
||||
<% @users.each do |user| %>
|
||||
<% if current_user_at_least(:admin) %>
|
||||
<table class="items" id="users">
|
||||
<tr>
|
||||
<td><%= user.email %></td>
|
||||
<td><%= user.status %></td>
|
||||
<td><%= user.created_at.to_fs(:db_without_sec) %></td>
|
||||
<td class="svg">
|
||||
<%= svg_tag "pictograms/checkbox-marked-outline.svg#icon" if user.confirmed_at.present? %>
|
||||
</td>
|
||||
<td class="actions"><%= image_link_to "Delete", "account-remove-outline", user_path(user),
|
||||
data: { turbo: true, turbo_method: :delete } %></td>
|
||||
<th><%= User.human_attribute_name(:email).capitalize %></th>
|
||||
<th><%= User.human_attribute_name(:status).capitalize %></th>
|
||||
<th><%= User.human_attribute_name(:created_at).capitalize %> <sup>UTC</sup></th>
|
||||
<th><%= User.human_attribute_name(:confirmed_at).capitalize %></th>
|
||||
<th><%= t :actions %></th>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
<% @users.each do |user| %>
|
||||
<tr>
|
||||
<td><%= user.email %></td>
|
||||
<td><%= user.status %></td>
|
||||
<td><%= user.created_at.to_fs(:db_without_sec) %></td>
|
||||
<td class="svg">
|
||||
<%= svg_tag "pictograms/checkbox-marked-outline" if user.confirmed_at.present? %>
|
||||
</td>
|
||||
<td class="actions"><%= image_link_to "Delete", "account-remove-outline", user_path(user),
|
||||
data: { turbo: true, turbo_method: :delete } %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
<% end %>
|
||||
|
@ -31,6 +31,8 @@ module FixinMe
|
||||
# config.time_zone = "Central Time (US & Canada)"
|
||||
# config.eager_load_paths << Rails.root.join("extras")
|
||||
|
||||
config.action_dispatch.rescue_responses['ApplicationController::AccessForbidden'] = :forbidden
|
||||
|
||||
# SETUP: Below settings need to be updated on a per-installation basis.
|
||||
#
|
||||
# URL to use in sent e-mails.
|
||||
|
Loading…
x
Reference in New Issue
Block a user