diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index 5be9d8f3b..6490745bf 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -3,12 +3,11 @@ class BookmarksController < ApplicationController def index per_page = params[:number_of_items_per_page] || 25 - unfiltered = Bookmark.includes(bookmarkable: [ :primary_asset, :gallery_assets, :windows_type ]) - filtered = unfiltered.search(params) - filtered = filtered.sorted(params[:sort]) + base_scope = authorized_scope(Bookmark.includes(bookmarkable: [ :primary_asset, :gallery_assets, :windows_type ])) + filtered = base_scope.search(params).sorted(params[:sort]) @bookmarks = filtered.paginate(page: params[:page], per_page: per_page).decorate - @bookmarks_count = unfiltered.length + @bookmarks_count = base_scope.length @windows_types_array = WindowsType::TYPES set_index_variables respond_to do |format| diff --git a/app/controllers/community_news_controller.rb b/app/controllers/community_news_controller.rb index d963e1dd3..d78e01267 100644 --- a/app/controllers/community_news_controller.rb +++ b/app/controllers/community_news_controller.rb @@ -6,22 +6,15 @@ class CommunityNewsController < ApplicationController def index if turbo_frame_request? per_page = params[:number_of_items_per_page].presence || 12 - unfiltered = - if current_user&.admin? - CommunityNews.all - elsif current_user - CommunityNews.published - else - CommunityNews.publicly_visible - end - filtered = unfiltered.search_by_params(params) - @community_news = filtered&.includes([ :bookmarks, :primary_asset, :author, :project, author: :facilitator ]) - &.paginate(page: params[:page], per_page: per_page)&.decorate + base_scope = authorized_scope(CommunityNews.includes([ :bookmarks, :primary_asset, :author, :project, author: :facilitator ])) - @count_display = if filtered.count == unfiltered.count - unfiltered.count + filtered = base_scope.search_by_params(params) + @community_news = filtered.paginate(page: params[:page], per_page: per_page).decorate + + @count_display = if filtered.count == base_scope.count + base_scope.count else - "#{filtered.count}/#{unfiltered.count}" + "#{filtered.count}/#{base_scope.count}" end render :index_lazy else diff --git a/app/controllers/facilitators_controller.rb b/app/controllers/facilitators_controller.rb index fd546f1af..663cfee3b 100644 --- a/app/controllers/facilitators_controller.rb +++ b/app/controllers/facilitators_controller.rb @@ -4,10 +4,15 @@ class FacilitatorsController < ApplicationController def index per_page = params[:number_of_items_per_page].presence || 25 - facilitators = Facilitator - .searchable + + base_scope = authorized_scope(Facilitator + .includes(:user, + :avatar_attachment, + :sectorable_items, + user: [ :avatar_attachment, :projects ]).references(:user)) + + facilitators = base_scope .search_by_params(params.to_unsafe_h) - .includes(:user, :avatar_attachment, :sectorable_items, user: [ :avatar_attachment, :projects ]).references(:user) .order(:first_name, :last_name) @count_display = facilitators.size @facilitators = facilitators.paginate(page: params[:page], per_page: per_page) diff --git a/app/controllers/faqs_controller.rb b/app/controllers/faqs_controller.rb index 734d9fad4..fa9d5797b 100644 --- a/app/controllers/faqs_controller.rb +++ b/app/controllers/faqs_controller.rb @@ -3,10 +3,10 @@ class FaqsController < ApplicationController before_action :set_faq, only: [ :show, :edit, :update, :destroy ] def index - faqs = current_user&.super_user? ? Faq.all : (current_user ? Faq.published : Faq.publicly_visible) + faqs = authorized_scope(Faq.all) @faqs = faqs.search_by_params(params.to_unsafe_h.slice("query", "published")) - .by_position - .page(params[:page]) + .by_position + .page(params[:page]) end def show diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index f57da3d13..451c961b8 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -6,16 +6,16 @@ class StoriesController < ApplicationController def index if turbo_frame_request? per_page = params[:number_of_items_per_page].presence || 12 - unpaginated = current_user.super_user? ? Story.all : Story.published - filtered = unpaginated.includes(:windows_type, :project, :workshop, :created_by, :bookmarks, :primary_asset) - .search_by_params(params) - .order(created_at: :desc) + base_scope = authorized_scope(Story.includes(:windows_type, :project, :workshop, :created_by, :bookmarks, :primary_asset)) + filtered = base_scope.search_by_params(params) + .order(created_at: :desc) + @stories = filtered.paginate(page: params[:page], per_page: per_page).decorate - @count_display = if filtered.count == unpaginated.count - unpaginated.count + @count_display = if filtered.count == base_scope.count + base_scope.count else - "#{filtered.count}/#{unpaginated.count}" + "#{filtered.count}/#{base_scope.count}" end render :index_lazy else diff --git a/app/controllers/tutorials_controller.rb b/app/controllers/tutorials_controller.rb index 8aa999bac..5622a2642 100644 --- a/app/controllers/tutorials_controller.rb +++ b/app/controllers/tutorials_controller.rb @@ -5,9 +5,10 @@ class TutorialsController < ApplicationController def index per_page = params[:number_of_items_per_page].presence || 25 - unfiltered = current_user&.super_user? ? Tutorial.all : current_user ? Tutorial.published : Tutorial.publicly_visible - filtered = unfiltered.search_by_params(params) - @count_display = filtered.count == unfiltered.count ? unfiltered.count : "#{filtered.count}/#{unfiltered.count}" + base_scope = authorized_scope(Tutorial.all) + filtered = base_scope.search_by_params(params) + + @count_display = filtered.size == base_scope.size ? base_scope.size : "#{filtered.count}/#{base_scope.count}" @tutorials = filtered.order(:position).paginate(page: params[:page], per_page: per_page).decorate end diff --git a/app/controllers/workshop_logs_controller.rb b/app/controllers/workshop_logs_controller.rb index 28890f1b8..be82f3740 100644 --- a/app/controllers/workshop_logs_controller.rb +++ b/app/controllers/workshop_logs_controller.rb @@ -4,17 +4,13 @@ class WorkshopLogsController < ApplicationController def index @per_page = params[:number_of_items_per_page].presence || 10 params[:workshop_id] ||= @workshop&.id - permitted_logs = - if current_user.super_user? - WorkshopLog.all - else - WorkshopLog.where(created_by_id: current_user.id) - .or(WorkshopLog.project_id(current_user.project_ids)) - end - @workshop_logs_unpaginated = permitted_logs.includes(:workshop, :user, :windows_type) - .search(params) - @workshop_logs_count = @workshop_logs_unpaginated.size + + @workshop_logs_unpaginated = authorized_scope(WorkshopLog.includes(:workshop, :user, :windows_type) + .search(params)) + @workshop_logs = @workshop_logs_unpaginated.paginate(page: params[:page], per_page: @per_page) + @workshop_logs_count = @workshop_logs&.total_entries + set_index_variables end diff --git a/app/controllers/workshops_controller.rb b/app/controllers/workshops_controller.rb index 6f1f52701..d8041acb2 100644 --- a/app/controllers/workshops_controller.rb +++ b/app/controllers/workshops_controller.rb @@ -13,12 +13,11 @@ def index track_index_intent(Workshop, search_service.workshops, params) - @workshops = search_service.workshops + @workshops = authorized_scope(search_service.workshops .includes(:categories, :windows_type, :user, :images, :bookmarks, :age_ranges, - user: [ :facilitator ], primary_asset: [ :file_attachment ]) + user: [ :facilitator ], primary_asset: [ :file_attachment ])) .paginate(page: params[:page], per_page: params[:per_page] || 12) - @workshops_count = search_service.workshops.size render :workshop_results else @@ -190,9 +189,9 @@ def search private def set_show - @quotes = Quote.where(workshop_id: @workshop.id).active + @quotes = Quote.where(workshop_id: @workshop.id).published @leader_spotlights = @workshop.associated_resources.leader_spotlights.where(published: true) - @workshop_variations = @workshop.workshop_variations.active + @workshop_variations = @workshop.workshop_variations.published @sectors = @workshop.sectorable_items.published.map { |item| item.sector if item.sector.published? }.compact if @workshop.sectorable_items.any? end diff --git a/app/models/user.rb b/app/models/user.rb index ac1a40aad..b1a057614 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -70,6 +70,7 @@ def self.search_by_params(params) results end + # TODO Remove once all view's use ActionPolicy def admin? super_user end diff --git a/app/policies/ahoy_activity_policy.rb b/app/policies/ahoy_activity_policy.rb index 127213ef3..669270675 100644 --- a/app/policies/ahoy_activity_policy.rb +++ b/app/policies/ahoy_activity_policy.rb @@ -17,7 +17,7 @@ def visits? # See https://actionpolicy.evilmartians.io/#/scoping # # relation_scope do |relation| - # next relation if user.admin? + # next relation if admin? # relation.where(user: user) # end end diff --git a/app/policies/application_policy.rb b/app/policies/application_policy.rb index 23e5b6768..76f197cc7 100644 --- a/app/policies/application_policy.rb +++ b/app/policies/application_policy.rb @@ -20,7 +20,7 @@ def manage? # Define shared methods useful for most policies. def admin? - user&.admin? + user&.super_user end def owner? diff --git a/app/policies/banner_policy.rb b/app/policies/banner_policy.rb index f920a5db4..d6717961f 100644 --- a/app/policies/banner_policy.rb +++ b/app/policies/banner_policy.rb @@ -13,7 +13,7 @@ def show? # See https://actionpolicy.evilmartians.io/#/scoping # # relation_scope do |relation| - # next relation if user.admin? + # next relation if admin? # relation.where(user: user) # end end diff --git a/app/policies/facilitator_policy.rb b/app/policies/facilitator_policy.rb index f2323f90d..669e50a8b 100644 --- a/app/policies/facilitator_policy.rb +++ b/app/policies/facilitator_policy.rb @@ -18,6 +18,6 @@ def update? relation_scope do |relation| next relation if admin? - relation.published.searchable + relation.published end end diff --git a/app/policies/faq_policy.rb b/app/policies/faq_policy.rb index d1605544e..b9336ae12 100644 --- a/app/policies/faq_policy.rb +++ b/app/policies/faq_policy.rb @@ -13,7 +13,7 @@ def show? # See https://actionpolicy.evilmartians.io/#/scoping # relation_scope do |relation| - next relation if user.admin? + next relation if admin? if authenticated? relation.published else diff --git a/app/policies/project_status_policy.rb b/app/policies/project_status_policy.rb index 76ed926c0..e836785ed 100644 --- a/app/policies/project_status_policy.rb +++ b/app/policies/project_status_policy.rb @@ -10,7 +10,7 @@ class ProjectStatusPolicy < ApplicationPolicy # See https://actionpolicy.evilmartians.io/#/scoping # # relation_scope do |relation| - # next relation if user.admin? + # next relation if admin? # relation.where(user: user) # end end diff --git a/app/policies/story_idea_policy.rb b/app/policies/story_idea_policy.rb index d1b893be7..bfb8e8fc0 100644 --- a/app/policies/story_idea_policy.rb +++ b/app/policies/story_idea_policy.rb @@ -21,7 +21,7 @@ def show? # See https://actionpolicy.evilmartians.io/#/scoping # # relation_scope do |relation| - # next relation if user.admin? + # next relation if admin? # relation.where(user: user) # end end diff --git a/app/policies/tutorial_policy.rb b/app/policies/tutorial_policy.rb index 7a3c75649..db3571ce3 100644 --- a/app/policies/tutorial_policy.rb +++ b/app/policies/tutorial_policy.rb @@ -13,7 +13,7 @@ def show? # See https://actionpolicy.evilmartians.io/#/scoping # relation_scope do |relation| - next relation if user.admin? + next relation if admin? if authenticated? relation.published else diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index 0af8990f3..73354c1af 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -10,7 +10,7 @@ class UserPolicy < ApplicationPolicy # See https://actionpolicy.evilmartians.io/#/scoping # # relation_scope do |relation| - # next relation if user.admin? + # next relation if admin? # relation.where(user: user) # end end diff --git a/app/views/faqs/_faq.html.erb b/app/views/faqs/_faq.html.erb index 11595ed10..25993b646 100644 --- a/app/views/faqs/_faq.html.erb +++ b/app/views/faqs/_faq.html.erb @@ -8,7 +8,7 @@