feat: Add authorization checks to admin and player actions
This commit is contained in:
@@ -7,9 +7,34 @@ module EuchreCamp
|
||||
class Index < EuchreCamp::Action
|
||||
include Deps[repo: 'repositories.matches']
|
||||
|
||||
def handle(_request, response)
|
||||
def handle(request, response)
|
||||
# Check authorization - view all matches
|
||||
user = current_user(request)
|
||||
unless user && EuchreCamp::Services::Authorization.can?(user, :view_all)
|
||||
response.flash[:error] = 'Access denied'
|
||||
response.redirect_to '/rankings'
|
||||
return
|
||||
end
|
||||
|
||||
matches = repo.all
|
||||
response.render(view, matches: matches)
|
||||
enhanced_matches = enhance_matches_with_player_names(matches)
|
||||
response.render(view, matches: enhanced_matches)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def enhance_matches_with_player_names(matches)
|
||||
rom = EuchreCamp::App["persistence.rom"]
|
||||
all_players = rom.relations[:players].to_a.to_h { |p| [p[:id], p] }
|
||||
|
||||
matches.map do |match|
|
||||
match.to_h.merge(
|
||||
team_1_p1_name: all_players[match.team_1_p1_id]&.[](:name) || 'Unknown',
|
||||
team_1_p2_name: all_players[match.team_1_p2_id]&.[](:name) || 'Unknown',
|
||||
team_2_p1_name: all_players[match.team_2_p1_id]&.[](:name) || 'Unknown',
|
||||
team_2_p2_name: all_players[match.team_2_p2_id]&.[](:name) || 'Unknown'
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user