diff --git a/app/controllers/codeharbor_links_controller.rb b/app/controllers/codeharbor_links_controller.rb index ddff149f..732d897e 100644 --- a/app/controllers/codeharbor_links_controller.rb +++ b/app/controllers/codeharbor_links_controller.rb @@ -1,20 +1,8 @@ +# frozen_string_literal: true + class CodeharborLinksController < ApplicationController include CommonBehavior - before_action :set_codeharbor_link, only: [:show, :edit, :update, :destroy] - - def authorize! - authorize(@codeharbor_link || @codeharbor_links) - end - private :authorize! - - # def index - # @codeharbor_links = CodeharborLink.where(user_id: current_user.id).paginate(page: params[:page]) - # authorize! - # end - - # def show - # authorize! - # end + before_action :set_codeharbor_link, only: %i[show edit update destroy] def new @codeharbor_link = CodeharborLink.new @@ -33,8 +21,8 @@ class CodeharborLinksController < ApplicationController end def update - update_and_respond(object: @codeharbor_link, params: codeharbor_link_params, path: @codeharbor_link.user) authorize! + update_and_respond(object: @codeharbor_link, params: codeharbor_link_params, path: @codeharbor_link.user) end def destroy @@ -43,6 +31,10 @@ class CodeharborLinksController < ApplicationController private + def authorize! + authorize @codeharbor_link + end + def set_codeharbor_link @codeharbor_link = CodeharborLink.find(params[:id]) @codeharbor_link.user = current_user diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 1421db5a..9e1c057d 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -108,29 +108,15 @@ class ExercisesController < ApplicationController end def push_proforma_xml - # codeharbor_link = current_user.codeharbor_link # CodeharborLink.find(params[:account_link]) - error = ExerciseService::PushExternal.call( zip: ProformaService::ExportTask.call(exercise: @exercise), codeharbor_link: current_user.codeharbor_link ) if error.nil? - redirect_to exercises_path, notice: 'klappt' # t('controllers.exercise.push_external_notice', account_link: account_link.readable) - # redirect_to @exercise, notice: 'klappt' # t('controllers.exercise.push_external_notice', account_link: account_link.readable) + redirect_to exercises_path, notice: t('exercises.export_codeharbor.success') else - # logger.debug(error) - redirect_to exercises_path, alert: 'klappt nicht' # t('controllers.account_links.not_working', account_link: account_link.readable) - # redirect_to @exercise, alert: 'klappt nicht' # t('controllers.account_links.not_working', account_link: account_link.readable) + redirect_to exercises_path, alert: t('exercises.export_codeharbor.fail') end - # oauth2_client = OAuth2::Client.new(codeharbor_link.client_id, codeharbor_link.client_secret, url: codeharbor_link.push_url, ssl: {verify: false}) - # oauth2token = codeharbor_link[:oauth2token] - # token = OAuth2::AccessToken.from_hash(oauth2_client, access_token: oauth2token) - - # # xml_generator = Proforma::XmlGenerator.new - # xml_document = xml_generator.generate_xml(@exercise) - # request = token.post(codeharbor_link.push_url, body: xml_document, headers: {'Content-Type' => 'text/xml'}) - # puts request - # redirect_to @exercise, notice: t('exercises.push_proforma_xml.notice', link: codeharbor_link.push_url) end def import_proforma_xml @@ -138,7 +124,10 @@ class ExercisesController < ApplicationController tempfile.write request.body.read.force_encoding('UTF-8') tempfile.rewind - exercise = ProformaService::Import.call(zip: tempfile, user: user_for_oauth2_request) + user = user_for_oauth2_request + return render json: {}, status: 401 if user.nil? + + exercise = ProformaService::Import.call(zip: tempfile, user: user) if exercise.save render json: {}, status: 201 else @@ -149,16 +138,8 @@ class ExercisesController < ApplicationController def user_for_oauth2_request authorization_header = request.headers['Authorization'] - raise(status: 401, message: 'No Authorization header') if authorization_header.nil? - - oauth2_token = authorization_header.split(' ')[1] - raise(status: 401, message: 'No token in Authorization header') if oauth2_token.nil? || oauth2_token.size.zero? - - user = user_by_codeharbor_token(oauth2_token) - - raise(status: 401, message: 'Unknown OAuth2 token') if user.nil? - - user + oauth2_token = authorization_header&.split(' ')&.second + user_by_codeharbor_token(oauth2_token) end private :user_for_oauth2_request diff --git a/app/models/codeharbor_link.rb b/app/models/codeharbor_link.rb index 5bb1fba1..18f2da02 100644 --- a/app/models/codeharbor_link.rb +++ b/app/models/codeharbor_link.rb @@ -2,7 +2,6 @@ class CodeharborLink < ApplicationRecord validates :oauth2token, presence: true - validates :user_id, presence: true belongs_to :user, foreign_key: :user_id, class_name: 'InternalUser' diff --git a/app/services/exercise_service/push_external.rb b/app/services/exercise_service/push_external.rb index 562ca57f..dfa54468 100644 --- a/app/services/exercise_service/push_external.rb +++ b/app/services/exercise_service/push_external.rb @@ -2,7 +2,7 @@ module ExerciseService class PushExternal < ServiceBase - CODEHARBOR_PUSH_LINK = 'http://localhost:3001/import_exercise' + CODEHARBOR_PUSH_LINK = Rails.env.production? ? 'https://codeharbor.openhpi.de/import_exercise' : 'http://localhost:3001/import_exercise' def initialize(zip:, codeharbor_link:) @zip = zip @codeharbor_link = codeharbor_link diff --git a/app/views/exercises/index.html.slim b/app/views/exercises/index.html.slim index bdec864c..2b854d22 100644 --- a/app/views/exercises/index.html.slim +++ b/app/views/exercises/index.html.slim @@ -46,7 +46,7 @@ h1 = Exercise.model_name.human(count: 2) li = link_to(t('activerecord.models.user_exercise_feedback.other'), feedback_exercise_path(exercise), class: 'dropdown-item') if policy(exercise).feedback? li = link_to(t('shared.destroy'), exercise, data: {confirm: t('shared.confirm_destroy')}, method: :delete, class: 'dropdown-item') if policy(exercise).destroy? li = link_to(t('.clone'), clone_exercise_path(exercise), data: {confirm: t('shared.confirm_destroy')}, method: :post, class: 'dropdown-item') if policy(exercise).clone? - li = link_to(t('exercises.export_codeharbor'), push_proforma_xml_exercise_path(exercise), data: {confirm: 'PUSHPUSH?'}, method: :post, class: 'dropdown-item') if policy(exercise).push_proforma_xml? + li = link_to(t('exercises.export_codeharbor.label'), push_proforma_xml_exercise_path(exercise), method: :post, class: 'dropdown-item') if policy(exercise).push_proforma_xml? = render('shared/pagination', collection: @exercises) p = render('shared/new_button', model: Exercise) diff --git a/config/locales/en.yml b/config/locales/en.yml index 790e8393..6b8b243f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -316,7 +316,10 @@ en: request_for_comments_sent: "Request for comments sent." editor_file_tree: file_root: Files - export_codeharbor: Export to Codeharbor + export_codeharbor: + label: Export to Codeharbor + success: Successfully pushed the exercise to CodeHarbor. + fail: Failed to push the exercise to CodeHarbor. file_form: hints: feedback_message: This message is used as a hint for failing tests.