Skip to content
This repository was archived by the owner on Jul 24, 2020. It is now read-only.

Commit 5f76eb4

Browse files
committed
Merge pull request #1187 from YaleSTC/1185_fix_banned_users_v34
[v3.4] #1185 Fix Banned Users
2 parents ff732fa + d8f0b38 commit 5f76eb4

6 files changed

+42
-6
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ Changelog
55
* This file will be updated whenever a new release is put into production.
66
* Any problems should be reported via the "report an issue" link in the footer of the application.
77

8+
### v3.4.9
9+
*Released on 16 March 2015*
10+
#### Bug Fixes
11+
* Banned users can no longer have reservations created for them or equipment checked out to them ([#1185](https://github.com/YaleSTC/reservations/issues/1185)).
12+
813
### v3.4.8
914
*Released on 26 October 2014*
1015
#### Bug Fixes

app/controllers/reservations_controller.rb

+14-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class ReservationsController < ApplicationController
1313

1414
def set_user
1515
@user = User.find(params[:user_id])
16+
return unless @user.role == 'banned'
17+
flash[:error] = 'This user is banned and cannot check out equipment.'
1618
end
1719

1820
def set_reservation
@@ -53,7 +55,10 @@ def new
5355
# error handling
5456
@errors = cart.validate_all
5557
unless @errors.empty?
56-
if can? :override, :reservation_errors
58+
if @errors[0].include?('banned')
59+
flash[:error] = 'Reservations cannot be created for banned users.'
60+
redirect_to root_path
61+
elsif can? :override, :reservation_errors
5762
flash[:error] = 'Are you sure you want to continue? Please review the errors below.'
5863
else
5964
flash[:error] = 'Please review the errors below. If uncorrected, any reservations with errors will be filed as a request, and subject to administrator approval.'
@@ -75,7 +80,7 @@ def create
7580
notes = params[:reservation][:notes]
7681
requested = !@errors.empty? && (cannot? :override, :reservation_errors)
7782

78-
if !@errors.blank? && notes.blank?
83+
if !@errors.blank? && notes.blank? && !@errors[0].include?('banned')
7984
# there were errors but they didn't fill out the notes
8085
flash[:error] = "Please give a short justification for this reservation #{requested ? 'request' : 'override'}"
8186
@notes_required = true
@@ -159,7 +164,11 @@ def checkout
159164
params[:reservations].each do |reservation_id, reservation_hash|
160165
if reservation_hash[:equipment_object_id].present?
161166
# update attributes for all equipment that is checked off
162-
r = Reservation.find(reservation_id)
167+
r = Reservation.includes(:reserver).find(reservation_id)
168+
if r.reserver.role == 'banned'
169+
flash[:error] = 'Banned users cannot check out equipment.'
170+
redirect_to(root_path) && return
171+
end
163172
r.checkout_handler = current_user
164173
r.checked_out = Time.now
165174
r.equipment_object_id = reservation_hash[:equipment_object_id]
@@ -308,13 +317,15 @@ def upcoming
308317
end
309318

310319
def manage # initializer
320+
redirect_to(root_path) && return unless flash[:error].nil?
311321
@check_out_set = @user.due_for_checkout
312322
@check_in_set = @user.due_for_checkin
313323

314324
render :manage, layout: 'application'
315325
end
316326

317327
def current
328+
redirect_to(root_path) && return unless flash[:error].nil?
318329
@user_overdue_reservations_set = [Reservation.overdue.for_reserver(@user)].delete_if{|a| a.empty?}
319330
@user_checked_out_today_reservations_set = [Reservation.checked_out_today.for_reserver(@user)].delete_if{|a| a.empty?}
320331
@user_checked_out_previous_reservations_set = [Reservation.checked_out_previous.for_reserver(@user)].delete_if{|a| a.empty?}

app/controllers/users_controller.rb

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def index
2929
end
3030

3131
def show
32+
if @user.role == 'banned' && @user.id != current_user.id
33+
flash[:error] = 'Please note that this user is banned.'
34+
end
3235
@user_reservations = @user.reservations
3336
@all_equipment = Reservation.active.for_reserver(@user)
3437
@show_equipment = { checked_out: @user.reservations.checked_out,

app/models/cart_validations.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def validate_all(renew = false)
1111
# if passed with true argument doesn't run validations that should be
1212
# skipped when validating renewals
1313
errors = []
14+
errors += check_banned
1415
errors += check_start_date_blackout
1516
errors += check_due_date_blackout
1617
errors += check_overdue_reservations unless renew
@@ -30,6 +31,16 @@ def validate_all(renew = false)
3031
return errors.uniq.reject{ |a| a.blank? }
3132
end
3233

34+
def check_banned
35+
errors = []
36+
reserver = User.find_by_id(reserver_id)
37+
if reserver && reserver.role == 'banned'
38+
errors << 'The reserver is banned and cannot reserve additional '\
39+
'equipment.'
40+
end
41+
errors
42+
end
43+
3344
def check_start_date_blackout
3445
# check that the start date is not on a blackout date
3546
# 1 query
@@ -187,5 +198,4 @@ def check_requirements(items = self.get_items)
187198
end
188199
return ["#{user.name} is missing the following certifications: #{unfulfilled_req_text.to_sentence}"]
189200
end
190-
191201
end

app/models/reservation.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Reservation < ActiveRecord::Base
1313
validates :equipment_model, :start_date, :due_date, presence: true
1414
validate :start_date_before_due_date
1515
validate :matched_object_and_model
16-
validate :not_in_past, :available, on: :create
16+
validate :not_in_past, :available, :check_banned, on: :create
1717

1818
nilify_blanks only: [:notes]
1919

@@ -136,6 +136,9 @@ def is_eligible_for_renew?
136136
# determines if a reservation is eligible for renewal, based on how many days before the due
137137
# date it is and the max number of times one is allowed to renew
138138
#
139+
140+
return false if reserver.role == 'banned'
141+
139142
self.times_renewed ||= 0
140143

141144
# you can't renew a checked in reservation, or one without an equipment model

app/models/reservation_validations.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ def available
3939
# Does not run on checked out, checked in, overdue, or missed Reservations
4040
def not_in_past
4141
if due_date < Date.today || start_date < Date.today
42-
errors.add(:base, "Cannot create reservation in the past\n")
42+
errors.add(:base, "Cannot create reservation in the past.\n")
4343
end
4444
end
4545

46+
def check_banned
47+
return unless reserver.role == 'banned'
48+
errors.add(:base, "Reserver cannot be banned.\n")
49+
end
4650
end

0 commit comments

Comments
 (0)