|
1 | 1 | module Scrims |
| 2 | + # OpponentTeams Controller |
| 3 | + # |
| 4 | + # Manages opponent team records which are shared across organizations. |
| 5 | + # Security note: Update and delete operations are restricted to organizations |
| 6 | + # that have used this opponent team in scrims. |
| 7 | + # |
2 | 8 | class OpponentTeamsController < ApplicationController |
3 | 9 | include TierAuthorization |
4 | 10 |
|
5 | 11 | before_action :set_opponent_team, only: [:show, :update, :destroy, :scrim_history] |
| 12 | + before_action :verify_team_usage!, only: [:update, :destroy] |
6 | 13 |
|
7 | 14 | # GET /api/v1/scrims/opponent_teams |
8 | 15 | def index |
@@ -75,18 +82,43 @@ def update |
75 | 82 |
|
76 | 83 | # DELETE /api/v1/scrims/opponent_teams/:id |
77 | 84 | def destroy |
| 85 | + # Check if team has scrims from other organizations before deleting |
| 86 | + other_org_scrims = @opponent_team.scrims.where.not(organization_id: current_organization.id).exists? |
| 87 | + |
| 88 | + if other_org_scrims |
| 89 | + return render json: { |
| 90 | + error: 'Cannot delete opponent team that is used by other organizations' |
| 91 | + }, status: :unprocessable_entity |
| 92 | + end |
| 93 | + |
78 | 94 | @opponent_team.destroy |
79 | 95 | head :no_content |
80 | 96 | end |
81 | 97 |
|
82 | 98 | private |
83 | 99 |
|
| 100 | + # Finds opponent team by ID |
| 101 | + # Security Note: OpponentTeam is a shared resource across organizations. |
| 102 | + # Deletion is restricted to teams without cross-org usage (see destroy action). |
| 103 | + # Consider adding organization_id in future for proper multi-tenancy. |
84 | 104 | def set_opponent_team |
85 | 105 | @opponent_team = OpponentTeam.find(params[:id]) |
86 | 106 | rescue ActiveRecord::RecordNotFound |
87 | 107 | render json: { error: 'Opponent team not found' }, status: :not_found |
88 | 108 | end |
89 | 109 |
|
| 110 | + # Verifies that current organization has used this opponent team |
| 111 | + # Prevents organizations from modifying/deleting teams they haven't interacted with |
| 112 | + def verify_team_usage! |
| 113 | + has_scrims = current_organization.scrims.exists?(opponent_team_id: @opponent_team.id) |
| 114 | + |
| 115 | + unless has_scrims |
| 116 | + render json: { |
| 117 | + error: 'You cannot modify this opponent team. Your organization has not played against them.' |
| 118 | + }, status: :forbidden |
| 119 | + end |
| 120 | + end |
| 121 | + |
90 | 122 | def opponent_team_params |
91 | 123 | params.require(:opponent_team).permit( |
92 | 124 | :name, |
|
0 commit comments