Skip to content

Commit faedf23

Browse files
Ap 602 refactor roles (#46)
* Adding role-based auth and refactor require_framework_admin * Tweak stackpass auth * Tweak proxy borrower; remove redundant authorize call from S.P. * Remove hardcoded admin functionality * Rolling back rubocop disables * Use authenticate_with_role in ref card; update specs
1 parent aaac2fe commit faedf23

23 files changed

Lines changed: 196 additions & 296 deletions

app/controllers/concerns/auth_support.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ def authenticate!
1212
raise Error::UnauthorizedError, "Endpoint #{controller_name}/#{action_name} requires authentication" unless authenticated?
1313
end
1414

15+
# Require current user be authenticated and have a role
16+
def authenticate_with_role!(*roles)
17+
authenticate!
18+
return true if current_user.any_role?(*roles)
19+
20+
raise Error::ForbiddenError,
21+
"Endpoint #{controller_name}/#{action_name} requires one of: #{roles.join(', ')}"
22+
end
23+
1524
# Return whether the current user is authenticated
1625
#
1726
# @return [Boolean]
@@ -46,11 +55,7 @@ def sign_out
4655
end
4756

4857
def require_framework_admin!
49-
authenticate!
50-
return true if framework_admin?
51-
52-
raise Error::ForbiddenError,
53-
"Endpoint #{controller_name}/#{action_name} requires framework admin CalGroup"
58+
authenticate_with_role!(:framework_admin)
5459
end
5560

5661
def framework_admin?

app/controllers/proxy_borrower_admin_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def init_form!; end
109109

110110
# You shall not pass....unless you're an admin
111111
def require_admin!
112-
@user_is_admin = current_user.role?(Role.proxyborrow_admin)
112+
@user_is_admin = current_user.any_role?(Role.proxyborrow_admin, :framework_admin)
113113
redirect_to proxy_borrower_forms_path unless @user_is_admin
114114
end
115115

app/controllers/proxy_borrower_forms_controller.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def index
99
# I think I want to get the users role now... if they're in the DB
1010
# then I'll want to pass that info so they have the
1111
# admin link...otherwise, NO admin link!
12-
@user_is_admin = current_user.role?(Role.proxyborrow_admin)
12+
@user_is_admin = current_user.any_role?(Role.proxyborrow_admin, :framework_admin)
1313
end
1414

1515
def dsp_form
@@ -25,9 +25,6 @@ def faculty_form
2525
department: @current_user.department_number)
2626
end
2727

28-
# TODO: do we still need this?
29-
def forbidden; end
30-
3128
# Processes a request from DSP form: (eventually dry this up)
3229
def process_dsp_request
3330
@form = ProxyBorrowerRequests.new form_params(:student_name, :dsp_rep)

app/controllers/reference_card_forms_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ def validate_recaptcha!
8484
end
8585

8686
def require_admin!
87-
authenticate!
88-
@user_is_admin = current_user.role?(Role.stackpass_admin)
89-
raise Error::ForbiddenError unless @user_is_admin
87+
@user_is_admin = authenticate_with_role!(Role.stackpass_admin, :framework_admin)
9088
end
9189
end

app/controllers/stack_pass_admin_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def init_form!; end
5151

5252
# You shall not pass....unless you're an admin
5353
def require_admin!
54-
@user_is_admin = current_user.role?(Role.stackpass_admin)
54+
@user_is_admin = current_user.any_role?(Role.stackpass_admin, :framework_admin)
5555
redirect_to stack_pass_forms_path unless @user_is_admin
5656
end
5757

app/controllers/stack_pass_forms_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ def validate_recaptcha!
8686
end
8787

8888
def require_admin!
89-
authenticate!
90-
@user_is_admin = current_user.role?(Role.stackpass_admin)
91-
raise Error::ForbiddenError unless @user_is_admin
89+
@user_is_admin = authenticate_with_role!(Role.stackpass_admin, :framework_admin)
9290
end
9391
end

app/controllers/stack_requests_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class StackRequestsController < ApplicationController
88
def forbidden; end
99

1010
def index
11-
@user_is_admin = current_user.role?(Role.stackpass_admin)
11+
@user_is_admin = current_user.any_role?(Role.stackpass_admin, :framework_admin)
1212
end
1313

1414
end

app/models/framework_users.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,4 @@ class FrameworkUsers < ActiveRecord::Base
2121
validates :role,
2222
presence: true
2323

24-
class << self
25-
# Hardcoded admins - so if for some reason all of the
26-
# admins in the DB are deleted, we still have a way of
27-
# getting in and managing things!
28-
HARDCODED_ADMIN_UIDS = [
29-
'7165', # Lisa Weber
30-
'1707532' # Steve Sullivan
31-
].freeze
32-
33-
def hardcoded_admin?(uid)
34-
HARDCODED_ADMIN_UIDS.include?(uid.to_s)
35-
end
36-
end
37-
3824
end

app/models/user.rb

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,26 @@ def primary_patron_record
7272
@primary_patron_record ||= find_primary_record
7373
end
7474

75-
# TODO: Unify this, faculty/staff checks, framework/alma admin checks
76-
# (and improve the design)
7775
def role?(role)
78-
# First check if user is a hardcoded admin
79-
return true if FrameworkUsers.hardcoded_admin?(uid)
76+
role_name = role_name_for(role)
77+
78+
case role_name
79+
when :framework_admin
80+
return true if framework_admin?
81+
when :alma_admin
82+
return true if alma_admin?
83+
end
8084

81-
# If user is not, then check if the user was added to the DB as an admin:
8285
user = FrameworkUsers.find_by(lcasid: uid)
8386
return false unless user
8487

8588
user.assignments.exists?(role:)
8689
end
8790

91+
def any_role?(*roles)
92+
roles.flatten.any? { |role| role?(role) }
93+
end
94+
8895
def ucb_faculty?
8996
affiliations&.include?('EMPLOYEE-TYPE-ACADEMIC')
9097
end
@@ -103,4 +110,17 @@ def uid_patron_record
103110
def find_primary_record
104111
uid_patron_record
105112
end
113+
114+
def role_name_for(role)
115+
role_value =
116+
if role.respond_to?(:role)
117+
role.role
118+
elsif role.respond_to?(:name)
119+
role.name
120+
else
121+
role
122+
end
123+
124+
role_value.to_sym
125+
end
106126
end

spec/calnet_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
require 'alma_helper'
33

44
module CalnetHelper
5-
# Lisa Weber's UID, hard-coded in FrameworkUsers
6-
STACK_REQUEST_ADMIN_UID = '7165'.freeze
5+
# UID used for test authentication
6+
TEST_UID = '7165'.freeze
77

88
# Mocks a calnet login as the specified patron, and stubs the corresponding
99
# Millennium patron dump file. Suitable for calling from a before() block.

0 commit comments

Comments
 (0)