Skip to content

Commit 2c50866

Browse files
committed
Perf: Refactor to check user role in memory not db
1 parent de827ae commit 2c50866

17 files changed

+27
-26
lines changed

app/controllers/admin/broadcast_announcements_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Admin::BroadcastAnnouncementsController < AdminController
33
before_action :require_admin
44

55
def require_admin
6-
verboten! unless current_user.has_role?(Role::SUPER_ADMIN)
6+
verboten! unless current_user.has_cached_role?(Role::SUPER_ADMIN)
77
end
88

99
def index

app/controllers/admin_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class AdminController < ApplicationController
33
before_action :require_admin
44

55
def require_admin
6-
verboten! unless current_user.has_role?(Role::SUPER_ADMIN)
6+
verboten! unless current_user.has_cached_role?(Role::SUPER_ADMIN)
77
end
88

99
def dashboard

app/controllers/application_controller.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,21 @@ def dashboard_path_from_current_role
5959
def authorize_user
6060
return unless params[:controller] # part of omniauth controller flow
6161
verboten! unless params[:controller].include?("devise") ||
62-
current_user.has_role?(Role::SUPER_ADMIN) ||
63-
current_user.has_role?(Role::ORG_USER, current_organization) ||
64-
current_user.has_role?(Role::ORG_ADMIN, current_organization) ||
65-
current_user.has_role?(Role::PARTNER, current_partner)
62+
current_user.has_cached_role?(Role::SUPER_ADMIN) ||
63+
current_user.has_cached_role?(Role::ORG_USER, current_organization) ||
64+
current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) ||
65+
current_user.has_cached_role?(Role::PARTNER, current_partner)
6666
end
6767

6868
def authorize_admin
69-
verboten! unless current_user.has_role?(Role::SUPER_ADMIN) ||
70-
current_user.has_role?(Role::ORG_ADMIN, current_organization)
69+
verboten! unless current_user.has_cached_role?(Role::SUPER_ADMIN) ||
70+
current_user.has_cached_role?(Role::ORG_ADMIN, current_organization)
7171
end
7272

7373
def log_active_user
7474
if current_user && should_update_last_request_at?
7575
# we don't want the user record to validate or run callbacks when we're tracking activity
7676
current_user.update_columns(last_request_at: Time.now.utc)
77-
7877
end
7978
end
8079

app/controllers/distributions_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def edit
171171
@distribution = Distribution.includes(:line_items).includes(:storage_location).find(params[:id])
172172
@distribution.initialize_request_items
173173
if (!@distribution.complete? && @distribution.future?) ||
174-
current_user.has_role?(Role::ORG_ADMIN, current_organization)
174+
current_user.has_cached_role?(Role::ORG_ADMIN, current_organization)
175175
@distribution.line_items.build if @distribution.line_items.size.zero?
176176
@items = current_organization.items.alphabetized
177177
@partner_list = current_organization.partners.alphabetized

app/controllers/organizations_controller.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def remove_user
8181
private
8282

8383
def authorize_user
84-
verboten! unless current_user.has_role?(Role::SUPER_ADMIN) ||
85-
current_user.has_role?(Role::ORG_USER, current_organization)
84+
verboten! unless current_user.has_cached_role?(Role::SUPER_ADMIN) ||
85+
current_user.has_cached_role?(Role::ORG_USER, current_organization)
8686
end
8787

8888
def organization_params
@@ -121,7 +121,7 @@ def request_type_formatter(params)
121121
end
122122

123123
def user_update_redirect_path
124-
if current_user.has_role?(Role::SUPER_ADMIN)
124+
if current_user.has_cached_role?(Role::SUPER_ADMIN)
125125
admin_organization_path(current_organization.id)
126126
else
127127
organization_path

app/helpers/application_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def menu_open?(controller_action_names)
2323
end
2424

2525
def can_administrate?
26-
current_user.has_role?(Role::ORG_ADMIN, current_organization)
26+
current_user.has_cached_role?(Role::ORG_ADMIN, current_organization)
2727
end
2828

2929
def navigation_link_to(*args)

app/views/distributions/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
</div>
8484
<div class="card-footer">
8585
<%= update_button_to picked_up_distribution_path(@distribution), {text: "Distribution Complete", size: "md"} if @distribution.scheduled? %>
86-
<% if @distribution.future? || current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
86+
<% if @distribution.future? || current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
8787
<%= edit_button_to edit_distribution_path(@distribution), {
8888
text: "Make a Correction",
8989
enabled: !@distribution.has_inactive_item?,

app/views/donations/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
enabled: !@donation.has_inactive_item?,
7676
size: "md" } %>
7777
<%= new_button_to new_distribution_path(donation_id: @donation.id, storage_location_id: @donation.storage_location_id), { text: "Start a new Distribution" } %>
78-
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
78+
<% if current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
7979
<%= delete_button_to donation_path(@donation), {
8080
size: "md",
8181
enabled: !@donation.has_inactive_item?,

app/views/item_categories/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<tr>
7878
<td><%= item.name %> </td>
7979
<td><%= view_button_to item_path(item) %>
80-
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
80+
<% if current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
8181
<%= delete_button_to(remove_category_item_path(item), method: :patch, text: "Remove from category") %>
8282
<% end %>
8383
</td>

app/views/items/_form.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<%= f.input_field :distribution_quantity, class: "form-control" %>
3131
<% end %>
3232

33-
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
33+
<% if current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
3434
<%= f.input :name, label: "On hand minimum quantity", wrapper: :input_group do %>
3535
<%= f.input_field :on_hand_minimum_quantity, input_html: {value: 0}, class: "form-control" %>
3636
<% end %>

app/views/layouts/_lte_navbar.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<i class="fa fa-repeat text-aqua"></i><%= "Switch to: #{role.resource&.name || "Super Admin"}" %>
5151
<% end %>
5252
<% end %>
53-
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
53+
<% if current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
5454
<div class="dropdown-divider"></div>
5555
<%= link_to users_path, class:"dropdown-item" do %>
5656
<i class="fas fa-users mr-2"></i> My Co-Workers

app/views/layouts/_lte_sidebar.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
<i class="nav-icon fa fa-circle-o"></i> Inventory Adjustments
114114
<% end %>
115115
</li>
116-
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
116+
<% if current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
117117
<li class="nav-item <%= active_class(['audits']) %>">
118118
<%= link_to(audits_path, class: "nav-link #{active_class(['audits'])}") do %>
119119
<i class="nav-icon fa fa-circle-o"></i> Inventory Audit
@@ -246,7 +246,7 @@
246246
</li>
247247
</ul>
248248
</li>
249-
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
249+
<% if current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
250250
<li class="nav-item <%= 'active' if current_page?(organization_path) %>">
251251
<%= link_to(organization_path, class: "nav-link #{'active' if current_page?(organization_path)}") do %>
252252
<i class="nav-icon fas fa-home"></i>

app/views/partners/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<h2 class="card-title">Partner Actions</h2>
3535
</div>
3636
<div class="card-body p-3">
37-
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
37+
<% if current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
3838
<%= link_to partner_users_path(@partner) do %>
3939
<div class="btn btn-app bg-success">
4040
<i class="fas fa-users"></i> Manage Users

app/views/product_drives/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
</p>
9191
<div class="card-footer clearfix">
9292
<%= edit_button_to edit_product_drive_path(@product_drive), { text: "Make a correction", size: "md" } %>
93-
<%= delete_button_to product_drive_path(@product_drive), { confirm: "Are you sure you want to permanently remove this product drive?", size: "md" } if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
93+
<%= delete_button_to product_drive_path(@product_drive), { confirm: "Are you sure you want to permanently remove this product drive?", size: "md" } if current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
9494
</div>
9595
<!-- /.card-footer-->
9696
</div>

app/views/purchases/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<%= edit_button_to edit_purchase_path(@purchase), { text: "Make a correction",
8282
enabled: !@purchase.has_inactive_item?,
8383
size: "md" } %>
84-
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
84+
<% if current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
8585
<%= delete_button_to purchase_path(@purchase), {
8686
size: "md",
8787
enabled: !@purchase.has_inactive_item?,

app/views/users/_organization_user.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</button>
1717
<ul class="dropdown-menu">
1818
<li>
19-
<% if current_user.has_role?(Role::SUPER_ADMIN) %>
19+
<% if current_user.has_current_role?(Role::SUPER_ADMIN) %>
2020
<%= edit_button_to(edit_admin_user_path(user), { text: 'Edit User' }) %>
2121
<% else %>
2222
<%= edit_button_to(

config/initializers/devise.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@
328328
end
329329

330330
Warden::Manager.after_set_user do |user, auth, opts|
331-
if user.roles.empty?
331+
# Use blank instead of #empty? to load roles in memory for future
332+
# current_user.has_cached_role? checks
333+
if user.roles.blank?
332334
auth.logout
333335
throw(:warden)
334336
end

0 commit comments

Comments
 (0)