self-review

This commit is contained in:
Karol
2019-09-03 15:26:28 +02:00
parent 3c65565b8c
commit 973cc43f4c
6 changed files with 22 additions and 47 deletions

View File

@ -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

View File

@ -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

View File

@ -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'

View File

@ -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

View File

@ -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)

View File

@ -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.