application_controller.rb revision 46dd7ca898be6d5dbdfda64fd90ce01e253c0878
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenclass ApplicationController < ActionController::Base
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen protect_from_forgery
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen ensure_security_headers
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen include Pagination
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen include PathHelpers
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # CanCan Authorization
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen rescue_from CanCan::AccessDenied do |exception|
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen redirect_to root_url, :alert => exception.message
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen if defined? PG
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # A foreign key constraint exception from the database
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen rescue_from PG::Error do |exception|
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen message = exception.message
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen if message.include?('foreign key constraint')
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen logger.warn(message)
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # shorten the message
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen message = message.match(/DETAIL: .+/).to_s
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen redirect_to :back,
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen :flash => {:error => "Whatever you tried to do - the server is unable to process your request because of a foreign key constraint. (#{message})" }
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen else
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # anything else
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen raise exception
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen protected
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen helper_method :admin?
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen def admin?
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen current_user.try(:admin?)
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen def authenticate_admin!
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen unless admin?
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen flash[:error] = "you need admin privileges for this action"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen redirect_to :root
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen helper_method :resource_chain
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen def resource_chain
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen return @resource_chain if @resource_chain
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen @resource_chain = [ Repository.find_by_path!( controller_name=='repositories' ? params[:id] : params[:repository_id] )]
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen if id = params[:commit_reference_id]
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen @resource_chain << CommitReference.new(id)
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen if id = (controller_name=='ontologies' ? params[:id] : params[:ontology_id])
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen @resource_chain << Ontology.find(id)
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen @resource_chain
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
b0be0bead3d6963149f7f2a9504b8ab5aced9af5Timo Sirainenend
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen