diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index bc3b687..c226bf9 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -19,13 +19,13 @@ class MatchesController < ApplicationController # GET /matches/1/edit def edit - if user_signed_in? - else - redirect_to root_path - end if params[:match] @match = Match.find (params[:match]) end + if current_user == @match.tournament.user + else + redirect_to root_path + end if @match @w1 = Wrestler.find(@match.w1) @w2 = Wrestler.find(@match.w2) @@ -55,7 +55,7 @@ class MatchesController < ApplicationController # PATCH/PUT /matches/1 # PATCH/PUT /matches/1.json def update - if user_signed_in? + if current_user == @match.tournament.user else redirect_to root_path end diff --git a/app/controllers/mats_controller.rb b/app/controllers/mats_controller.rb index fbe955f..88d0dc9 100644 --- a/app/controllers/mats_controller.rb +++ b/app/controllers/mats_controller.rb @@ -18,6 +18,9 @@ class MatsController < ApplicationController if params[:tournament] @tournament_field = params[:tournament] @tournament = Tournament.find(params[:tournament]) + if current_user != @tournament.user + redirect_to root_path + end end end @@ -29,12 +32,11 @@ class MatsController < ApplicationController # POST /mats # POST /mats.json def create - if user_signed_in? - else - redirect_to root_path - end @mat = Mat.new(mat_params) @tournament = Tournament.find(mat_params[:tournament_id]) + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @mat.save format.html { redirect_to @tournament, notice: 'Mat was successfully created.' } @@ -49,11 +51,10 @@ class MatsController < ApplicationController # PATCH/PUT /mats/1 # PATCH/PUT /mats/1.json def update - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@mat.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @mat.update(mat_params) format.html { redirect_to @tournament, notice: 'Mat was successfully updated.' } @@ -68,11 +69,10 @@ class MatsController < ApplicationController # DELETE /mats/1 # DELETE /mats/1.json def destroy - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@mat.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end @mat.destroy respond_to do |format| format.html { redirect_to @tournament } diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb index 42f647f..4715e7a 100644 --- a/app/controllers/schools_controller.rb +++ b/app/controllers/schools_controller.rb @@ -32,12 +32,11 @@ class SchoolsController < ApplicationController # POST /schools # POST /schools.json def create - if user_signed_in? - else - redirect_to root_path - end @school = School.new(school_params) @tournament = Tournament.find(school_params[:tournament_id]) + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @school.save format.html { redirect_to @tournament, notice: 'School was successfully created.' } @@ -52,11 +51,10 @@ class SchoolsController < ApplicationController # PATCH/PUT /schools/1 # PATCH/PUT /schools/1.json def update - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@school.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @school.update(school_params) format.html { redirect_to @tournament, notice: 'School was successfully updated.' } @@ -71,11 +69,10 @@ class SchoolsController < ApplicationController # DELETE /schools/1 # DELETE /schools/1.json def destroy - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@school.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end @school.destroy respond_to do |format| format.html { redirect_to @tournament } diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index eb400de..77e45f9 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -68,12 +68,11 @@ class StaticPagesController < ApplicationController end def createCustomWeights - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(params[:tournament]) - @custom = params[:customValue].to_s + if current_user != @tournament.user + redirect_to root_path + end + @custom = params[:customValue].to_s @tournament.createCustomWeights(@custom) redirect_to "/tournaments/#{@tournament.id}" @@ -92,6 +91,9 @@ class StaticPagesController < ApplicationController elsif user_signed_in? if params[:tournament] @tournament = Tournament.find(params[:tournament]) + if current_user != @tournament.user + redirect_to root_path + end end if @tournament @tournament.generateMatchups @@ -108,6 +110,9 @@ class StaticPagesController < ApplicationController end if params[:tournament] @tournament = Tournament.find(params[:tournament]) + if current_user != @tournament.user + redirect_to root_path + end @tournament_id = @tournament.id @tournament_name = @tournament.name end diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 364e121..324802b 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -46,10 +46,9 @@ class TournamentsController < ApplicationController # PATCH/PUT /tournaments/1 # PATCH/PUT /tournaments/1.json def update - if user_signed_in? - else - redirect_to root_path - end + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @tournament.update(tournament_params) format.html { redirect_to @tournament, notice: 'Tournament was successfully updated.' } @@ -64,10 +63,9 @@ class TournamentsController < ApplicationController # DELETE /tournaments/1 # DELETE /tournaments/1.json def destroy - if user_signed_in? - else - redirect_to root_path - end + if current_user != @tournament.user + redirect_to root_path + end @tournament.destroy respond_to do |format| format.html { redirect_to tournaments_url } diff --git a/app/controllers/weights_controller.rb b/app/controllers/weights_controller.rb index 12a0c34..6fc3f3c 100644 --- a/app/controllers/weights_controller.rb +++ b/app/controllers/weights_controller.rb @@ -37,13 +37,11 @@ class WeightsController < ApplicationController # POST /weights # POST /weights.json def create - if user_signed_in? - else - redirect_to root_path - end @weight = Weight.new(weight_params) @tournament = Tournament.find(weight_params[:tournament_id]) - + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @weight.save format.html { redirect_to @tournament, notice: 'Weight was successfully created.' } @@ -58,11 +56,10 @@ class WeightsController < ApplicationController # PATCH/PUT /weights/1 # PATCH/PUT /weights/1.json def update - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@weight.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @weight.update(weight_params) format.html { redirect_to @tournament, notice: 'Weight was successfully updated.' } @@ -77,11 +74,10 @@ class WeightsController < ApplicationController # DELETE /weights/1 # DELETE /weights/1.json def destroy - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@weight.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end @weight.destroy respond_to do |format| format.html { redirect_to @tournament } diff --git a/app/controllers/wrestlers_controller.rb b/app/controllers/wrestlers_controller.rb index be87ce9..8454a32 100644 --- a/app/controllers/wrestlers_controller.rb +++ b/app/controllers/wrestlers_controller.rb @@ -43,11 +43,10 @@ class WrestlersController < ApplicationController # POST /wrestlers # POST /wrestlers.json def create - if user_signed_in? - else - redirect_to root_path - end @wrestler = Wrestler.new(wrestler_params) + if current_user != @wrestler.tournament.user + redirect_to root_path + end @school = School.find(wrestler_params[:school_id]) respond_to do |format| if @wrestler.save @@ -63,10 +62,9 @@ class WrestlersController < ApplicationController # PATCH/PUT /wrestlers/1 # PATCH/PUT /wrestlers/1.json def update - if user_signed_in? - else - redirect_to root_path - end + if current_user != @wrestler.tournament.user + redirect_to root_path + end @school = School.find(@wrestler.school_id) respond_to do |format| if @wrestler.update(wrestler_params) @@ -82,10 +80,9 @@ class WrestlersController < ApplicationController # DELETE /wrestlers/1 # DELETE /wrestlers/1.json def destroy - if user_signed_in? - else - redirect_to root_path - end + if current_user != @wrestler.tournament.user + redirect_to root_path + end @school = School.find(@wrestler.school_id) @wrestler.destroy respond_to do |format| diff --git a/app/models/tournament.rb b/app/models/tournament.rb index da20537..df4bb9e 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -8,6 +8,7 @@ class Tournament < ActiveRecord::Base has_many :mats, dependent: :destroy has_many :wrestlers, through: :weights has_many :matches, dependent: :destroy + belongs_to :user def tournament_types ["Pool to bracket"] diff --git a/app/models/user.rb b/app/models/user.rb index c822027..abb45d7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,8 @@ class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable + has_many :tournaments + devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 22af7b3..67e15e9 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -1,5 +1,4 @@