Commit 57b75bab authored by David Lucadou's avatar David Lucadou

Addressing linter errors

parent 3ce97e97
Pipeline #61070784 failed with stage
in 14 minutes and 56 seconds
......@@ -8,7 +8,7 @@ class ReportsController < ApplicationController
before_action :verify_permissions
before_action :verify_ownership, only: [:edit, :update, :delete, :destroy]
helper_method :is_admin, :is_admin_view
# GET /reports
# GET /reports.json
def index
......@@ -29,8 +29,8 @@ class ReportsController < ApplicationController
# 24 hour time needs no modifications
end
@prefs[:per_page] = current_user.user_preference.search_results
# Extract params
report_type = params[:resolved] if params[:resolved] && params[:resolved].length > 0
report_reason_query = params[:reason] if params[:reason] && params[:reason].length > 0
......@@ -41,9 +41,9 @@ class ReportsController < ApplicationController
report_start_time = params[:start_time] if params[:start_time] && params[:start_time].length > 0
report_end_time = params[:end_time] if params[:end_time] && params[:end_time].length > 0
report_per_page = params[:per_page] if params[:per_page] && params[:per_page].length > 0
@SearchErrors = []
# Validate report_type
if report_type && report_statuses.key(report_type).nil?
@SearchErrors << "Invalid report status, please select an option from the dropdown menu"
......@@ -60,7 +60,7 @@ class ReportsController < ApplicationController
report_status = nil
end
end
# Validate report_reason_query
if report_reason_query && report_reason_query.length > 0
if report_reason_query[0] == '/' && report_reason_query[-1] == '/'
......@@ -75,7 +75,7 @@ class ReportsController < ApplicationController
end
end
end
# Validate report_msg_query
if report_msg_query && report_msg_query.length > 0
if report_msg_query[0] == '/' && report_msg_query[-1] == '/'
......@@ -87,7 +87,7 @@ class ReportsController < ApplicationController
end
end
end
# Validate report_msg_id
if report_msg_id && report_msg_id.length > 0
if (report_msg_id.to_i < 1 || report_msg_id.to_i.to_s != report_msg_id) && report_msg_id.to_i <= ChatLogsHelper.chatlog_max_id
......@@ -104,7 +104,7 @@ class ReportsController < ApplicationController
# the range of a 32-bit signed int ([-2^31, 2^31-1])
end
end
# Validate report_msg_channel
if report_msg_channel && !report_msg_channel.match(ChatLogsHelper.chatlog_channel_regex)
# Previously, I just checked if the channel length was <= 255 characters
......@@ -114,14 +114,14 @@ class ReportsController < ApplicationController
@SearchErrors << "Channel name is in an invalid format - #{ChatLogsHelper.chatlog_channel_regex_description}"
report_msg_channel = report_msg_channel[0, 255]
end
# Validate report_msg_sender
if report_msg_sender && !report_msg_sender.match(ChatLogsHelper.chatlog_sender_regex)
# Prevent errors saving record to DB - string columns can only hold 255 characters
@SearchErrors << "Sender name is in an invalid format - #{ChatLogsHelper.chatlog_sender_regex_description}"
report_msg_sender = report_msg_sender[0, 255]
end
# Validate report_start_time and report_end_time
user_dt_format = user_datetime_format(current_user.id, format: :datetime, seconds: false)
if report_start_time
......@@ -177,7 +177,7 @@ class ReportsController < ApplicationController
# If I did not include "&.is_a?(DateTime)", if report_start_time
# was a String, report_end_time would auto-cast to String for the
# >= comparison, which can cause unexpected behavior.
# Make sure end time is greater than start time (start < end)
@SearchErrors << "Ending time must be greater than the starting time"
end
......@@ -186,19 +186,19 @@ class ReportsController < ApplicationController
end
end
end
# Validate report_per_page
if report_per_page && (report_per_page.to_i < 10 || report_per_page.to_i > 1000 || report_per_page.to_i.to_s != report_per_page)
@SearchErrors << "Invalid results per page: must be a number from 10 to 1000."
end
# Determine query_type
if report_type.nil?
query_type = :report_browse_user
else
query_type = :report_query_user
end
query_notes = {}
if @SearchErrors.length == 0 && query_type == :report_query_user
begin
......@@ -229,8 +229,7 @@ class ReportsController < ApplicationController
# Filter to only reports sent by the current user
}
end
order = "#{report_sort_column} #{sort_direction}"
custom_per_page = per_page == params[:per_page]
Search.create(searcher: current_user.id, user_id: current_user.id,
query_type: query_type, query_notes: query_notes,
......@@ -246,7 +245,7 @@ class ReportsController < ApplicationController
sort_column: report_sort_column,
sort_direction: sort_direction,
custom_per_page: custom_per_page)
@reports = @reports.order("#{report_sort_column} #{sort_direction}").page(params[:page]).per(per_page) # Sort results with results per page specified by the user
end
......@@ -291,7 +290,6 @@ class ReportsController < ApplicationController
# GET /account/reports/1/edit
def edit
if request.patch? || request.post?
r_params = if params[:report] then report_params else {} end
if r_params.keys.length == 1 && r_params[:msg_text] && r_params[:msg_text].length > 0
@report.msg_text = r_params[:msg_text]
......@@ -387,7 +385,7 @@ class ReportsController < ApplicationController
end
end
end
def resolve
@report = Report.find_by_id(params[:id])
session[:return_to] = request.original_url if !request.original_url.nil?
......@@ -398,7 +396,6 @@ class ReportsController < ApplicationController
elsif request.patch? || request.post? # Report has been resolved
session[:return_to] = nil
if params[:report][:resolving_action]
r_params = {}
if @report.resolved # Report has already been resolved
success_message = "Updated report."
failure_message = "Failed to update report, see "
......@@ -410,7 +407,7 @@ class ReportsController < ApplicationController
@report.resolved = true
@report.resolving_action = params[:report][:resolving_action]
@report.resolver = current_user.id
respond_to do |format|
if @report&.save
flash[:success] = success_message
......@@ -436,7 +433,7 @@ class ReportsController < ApplicationController
end
private
# Use callbacks to share common setup or constraints between actions.
def set_report
begin
......@@ -457,7 +454,7 @@ class ReportsController < ApplicationController
def report_params
params.require(:report).permit(:reporter, :msg_id, :msg_channel, :sender, :msg_text, :report_reason, :resolved, :resolving_action, :resolver, :resolved_at, :start_time, :end_time, :page, :per_page, :utf8)
end
def verify_permissions
if permissions_list[action_name.to_sym]&.index(current_user.role.to_sym).nil?
# action_name is a Rails method for the controller method name
......@@ -471,28 +468,28 @@ class ReportsController < ApplicationController
# user who never has to see an unformatted redirect page.
end
end
def permissions_list
{ :index => [:admin, :user], :show => [:admin], :new => [:admin, :user], :edit => [:admin], :create => [:admin, :user], :update => [:admin], :destroy => [:admin, :user], :resolve => [:admin] }
end
def verify_ownership
unless is_admin || (@report && @report.reporter == current_user.id)
flash[:error] = I18n.t('.activerecord.errors.permissions.general.unauthorized')
redirect_to root_path
end
end
def is_admin
current_user.role == 'admin'
end
def is_admin_view
is_admin && request.path.split('/')[1] == 'admin'
# request.path is the relative path, so I expect something like this:
# "/admin/reports" -> ["", "admin", "reports"]
end
def from_admin_view
is_admin && (request.referrer.split('/')[3] == 'admin' || request.referrer.split('/')[5] == @report.id.to_s)
# request.referrer is the entire URL, not just the relative path, so
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment