diff --git a/app/controllers/code_harbor_links_controller.rb b/app/controllers/code_harbor_links_controller.rb deleted file mode 100644 index a4736f26..00000000 --- a/app/controllers/code_harbor_links_controller.rb +++ /dev/null @@ -1,68 +0,0 @@ -class CodeHarborLinksController < ApplicationController - include CommonBehavior - before_action :set_code_harbor_link, only: [:show, :edit, :update, :destroy] - - def authorize! - authorize(@code_harbor_link || @code_harbor_links) - end - private :authorize! - - # GET /code_harbor_links - # GET /code_harbor_links.json - def index - @code_harbor_links = CodeHarborLink.where(user_id: current_user.id).paginate(page: params[:page]) - authorize! - end - - # GET /code_harbor_links/1 - # GET /code_harbor_links/1.json - def show - authorize! - end - - # GET /code_harbor_links/new - def new - @code_harbor_link = CodeHarborLink.new - authorize! - end - - # GET /code_harbor_links/1/edit - def edit - authorize! - end - - # POST /code_harbor_links - # POST /code_harbor_links.json - def create - @code_harbor_link = CodeHarborLink.new(code_harbor_link_params) - @code_harbor_link.user = current_user - authorize! - create_and_respond(object: @code_harbor_link) - end - - # PATCH/PUT /code_harbor_links/1 - # PATCH/PUT /code_harbor_links/1.json - def update - update_and_respond(object: @code_harbor_link, params: code_harbor_link_params) - authorize! - end - - # DELETE /code_harbor_links/1 - # DELETE /code_harbor_links/1.json - def destroy - destroy_and_respond(object: @code_harbor_link) - end - - private - # Use callbacks to share common setup or constraints between actions. - def set_code_harbor_link - @code_harbor_link = CodeHarborLink.find(params[:id]) - @code_harbor_link.user = current_user - authorize! - end - - # Never trust parameters from the scary internet, only allow the white list through. - def code_harbor_link_params - params.require(:code_harbor_link).permit(:oauth2token) - end -end diff --git a/app/controllers/codeharbor_links_controller.rb b/app/controllers/codeharbor_links_controller.rb new file mode 100644 index 00000000..1eee74df --- /dev/null +++ b/app/controllers/codeharbor_links_controller.rb @@ -0,0 +1,55 @@ +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 + + def new + @codeharbor_link = CodeharborLink.new + authorize! + end + + def edit + authorize! + end + + def create + @codeharbor_link = CodeharborLink.new(codeharbor_link_params) + @codeharbor_link.user = current_user + authorize! + create_and_respond(object: @codeharbor_link) + end + + def update + update_and_respond(object: @codeharbor_link, params: codeharbor_link_params) + authorize! + end + + def destroy + destroy_and_respond(object: @codeharbor_link) + end + + private + + def set_codeharbor_link + @codeharbor_link = CodeharborLink.find(params[:id]) + @codeharbor_link.user = current_user + authorize! + end + + def codeharbor_link_params + params.require(:codeharbor_link).permit(:push_url, :oauth2token, :client_id, :client_secret) + end +end diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 131b1315..33e06fa9 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -108,34 +108,17 @@ class ExercisesController < ApplicationController end def import_proforma_xml - # begin - # user = user_for_oauth2_request - # exercise = Exercise.new - # request_body = request.body.read # needs to be some kind of a zip file - tempfile = Tempfile.new('codeharbor_import.zip') tempfile.write request.body.read.force_encoding('UTF-8') tempfile.rewind exercise = ProformaService::Import.call(zip: tempfile, user: user_for_oauth2_request) - # exercise.from_proforma_xml(request_body) - # exercise.user = user - # saved = exercise.save if exercise.save - # render text: 'SUCCESS', status: 200 render json: {}, status: 201 else logger.info(exercise.errors.full_messages) render json: {}, status: 400 end - # rescue => error - # if error.class == Hash - # render :text => error.message, :status => error.status - # else - # raise error - # render :text => '', :status => 500 - # end - # end end def user_for_oauth2_request diff --git a/app/models/user.rb b/app/models/user.rb index a9c9dab2..0315e761 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,6 +13,7 @@ class User < ApplicationRecord has_many :user_proxy_exercise_exercises, as: :user has_many :user_exercise_interventions, as: :user has_many :interventions, through: :user_exercise_interventions + has_one :codeharbor_link accepts_nested_attributes_for :user_proxy_exercise_exercises diff --git a/app/policies/codeharbor_link_policy.rb b/app/policies/codeharbor_link_policy.rb index 13c3676c..e28e3af7 100644 --- a/app/policies/codeharbor_link_policy.rb +++ b/app/policies/codeharbor_link_policy.rb @@ -1,3 +1,31 @@ -class CodeharborLinkPolicy < AdminOnlyPolicy +# frozen_string_literal: true +class CodeharborLinkPolicy < ApplicationPolicy + def index? + teacher? + end + + def create? + teacher? + end + + def show? + teacher? + end + + def edit? + teacher? + end + + def destroy? + teacher? + end + + def new? + teacher? + end + + def update? + teacher? + end end diff --git a/app/views/application/_navigation.html.slim b/app/views/application/_navigation.html.slim index 738dfc9d..c4626795 100644 --- a/app/views/application/_navigation.html.slim +++ b/app/views/application/_navigation.html.slim @@ -19,5 +19,5 @@ models: [ErrorTemplate, ErrorTemplateAttribute], cached: true) = render('navigation_submenu', title: t('navigation.sections.files'), models: [FileType, FileTemplate], cached: true) - = render('navigation_submenu', title: t('navigation.sections.integrations'), models: [Consumer], + = render('navigation_submenu', title: t('navigation.sections.integrations'), models: [Consumer, CodeharborLink], cached: true) diff --git a/app/views/code_harbor_links/_form.html.slim b/app/views/code_harbor_links/_form.html.slim deleted file mode 100644 index f7b449ee..00000000 --- a/app/views/code_harbor_links/_form.html.slim +++ /dev/null @@ -1,6 +0,0 @@ -= form_for(@code_harbor_link) do |f| - = render('shared/form_errors', object: @code_harbor_link) - .form-group - = f.label(:oauth2token) - = f.text_field(:oauth2token, class: 'form-control', required: true) - .actions = render('shared/submit_button', f: f, object: @code_harbor_link) diff --git a/app/views/code_harbor_links/edit.html.slim b/app/views/code_harbor_links/edit.html.slim deleted file mode 100644 index d1c7ea8f..00000000 --- a/app/views/code_harbor_links/edit.html.slim +++ /dev/null @@ -1,3 +0,0 @@ -h1 = @code_harbor_link - -= render('form') diff --git a/app/views/code_harbor_links/index.html.slim b/app/views/code_harbor_links/index.html.slim deleted file mode 100644 index c07e43da..00000000 --- a/app/views/code_harbor_links/index.html.slim +++ /dev/null @@ -1,18 +0,0 @@ -h1 = CodeHarborLink.model_name.human(count: 2) - -.table-responsive - table.table - thead - tr - th = t('activerecord.attributes.code_harbor_link.oauth2token') - th colspan=3 = t('shared.actions') - tbody - - @code_harbor_links.each do |code_harbor_link| - tr - td = link_to_if(policy(code_harbor_link).show?, code_harbor_link.oauth2token, code_harbor_link) - td = link_to(t('shared.show'), code_harbor_link) if policy(code_harbor_link).show? - td = link_to(t('shared.edit'), edit_code_harbor_link_path(code_harbor_link)) if policy(code_harbor_link).edit? - td = link_to(t('shared.destroy'), code_harbor_link, data: {confirm: t('shared.confirm_destroy')}, method: :delete) if policy(code_harbor_link).destroy? - -= render('shared/pagination', collection: @code_harbor_links) -p = render('shared/new_button', model: CodeHarborLink) diff --git a/app/views/code_harbor_links/new.html.slim b/app/views/code_harbor_links/new.html.slim deleted file mode 100644 index ef19a3e6..00000000 --- a/app/views/code_harbor_links/new.html.slim +++ /dev/null @@ -1,3 +0,0 @@ -h1 = t('shared.new_model', model: CodeHarborLink.model_name.human) - -= render('form') diff --git a/app/views/code_harbor_links/show.html.slim b/app/views/code_harbor_links/show.html.slim deleted file mode 100644 index e4a29f75..00000000 --- a/app/views/code_harbor_links/show.html.slim +++ /dev/null @@ -1,7 +0,0 @@ -h1 - = @code_harbor_link - = render('shared/edit_button', object: @code_harbor_link) - -- %w[oauth2token].each do |attribute| - = row(label: "code_harbor_link.#{attribute}") do - = content_tag(:input, nil, class: 'form-control', readonly: true, value: @code_harbor_link.send(attribute)) diff --git a/app/views/codeharbor_links/_form.html.slim b/app/views/codeharbor_links/_form.html.slim new file mode 100644 index 00000000..de351ec7 --- /dev/null +++ b/app/views/codeharbor_links/_form.html.slim @@ -0,0 +1,6 @@ += form_for(@codeharbor_link) do |f| + = render('shared/form_errors', object: @codeharbor_link) + .form-group + = f.label(:oauth2token) + = f.text_field(:oauth2token, class: 'form-control', required: true) + .actions = render('shared/submit_button', f: f, object: @codeharbor_link) diff --git a/app/views/codeharbor_links/edit.html.slim b/app/views/codeharbor_links/edit.html.slim new file mode 100644 index 00000000..6cb4a21b --- /dev/null +++ b/app/views/codeharbor_links/edit.html.slim @@ -0,0 +1,3 @@ +h1 = @codeharbor_link + += render('form') diff --git a/app/views/codeharbor_links/index.html.slim b/app/views/codeharbor_links/index.html.slim new file mode 100644 index 00000000..be6833c9 --- /dev/null +++ b/app/views/codeharbor_links/index.html.slim @@ -0,0 +1,18 @@ +h1 = CodeharborLink.model_name.human(count: 2) + +.table-responsive + table.table + thead + tr + th = t('activerecord.attributes.codeharbor_link.oauth2token') + th colspan=3 = t('shared.actions') + tbody + - @codeharbor_links.each do |codeharbor_link| + tr + td = link_to_if(policy(codeharbor_link).show?, codeharbor_link.oauth2token, codeharbor_link) + td = link_to(t('shared.show'), codeharbor_link) if policy(codeharbor_link).show? + td = link_to(t('shared.edit'), edit_codeharbor_link_path(codeharbor_link)) if policy(codeharbor_link).edit? + td = link_to(t('shared.destroy'), codeharbor_link, data: {confirm: t('shared.confirm_destroy')}, method: :delete) if policy(codeharbor_link).destroy? + += render('shared/pagination', collection: @codeharbor_links) +p = render('shared/new_button', model: CodeharborLink) diff --git a/app/views/codeharbor_links/new.html.slim b/app/views/codeharbor_links/new.html.slim new file mode 100644 index 00000000..c3038281 --- /dev/null +++ b/app/views/codeharbor_links/new.html.slim @@ -0,0 +1,3 @@ +h1 = t('shared.new_model', model: CodeharborLink.model_name.human) + += render('form') diff --git a/app/views/codeharbor_links/show.html.slim b/app/views/codeharbor_links/show.html.slim new file mode 100644 index 00000000..b17c86e7 --- /dev/null +++ b/app/views/codeharbor_links/show.html.slim @@ -0,0 +1,7 @@ +h1 + = @codeharbor_link + = render('shared/edit_button', object: @codeharbor_link) + +- %w[oauth2token].each do |attribute| + = row(label: "codeharbor_link.#{attribute}") do + = content_tag(:input, nil, class: 'form-control', readonly: true, value: @codeharbor_link.send(attribute)) diff --git a/app/views/internal_users/show.html.slim b/app/views/internal_users/show.html.slim index 167443f6..da5620ff 100644 --- a/app/views/internal_users/show.html.slim +++ b/app/views/internal_users/show.html.slim @@ -7,3 +7,9 @@ h1 = row(label: 'internal_user.consumer', value: @user.consumer ? link_to_if(policy(@user.consumer).show?, @user.consumer, @user.consumer) : nil) = row(label: 'internal_user.role', value: t("users.roles.#{@user.role}")) = row(label: 'internal_user.activated', value: @user.activated?) + +- if @user.codeharbor_link.nil? + + = row(label: 'codeharbor_link.new', value: link_to(t('codeharbor_link.new'), codeharbor_link_new_path, class: 'btn btn-secondary')) +- else + = row(label: 'codeharbor_link.edit', value: link_to(t('codeharbor_link.edit'), edit_codeharbor_link_path(@user.codeharbor_link), class: 'btn btn-secondary')) diff --git a/config/locales/de.yml b/config/locales/de.yml index f36e5cbb..6ae6b37c 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -1,7 +1,7 @@ de: activerecord: attributes: - code_harbor_link: + codeharbor_link: oauth2token: OAuth2 Token consumer: name: Name diff --git a/config/locales/en.yml b/config/locales/en.yml index e9d30ec0..90f1f69e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,7 +1,7 @@ en: activerecord: attributes: - code_harbor_link: + codeharbor_link: oauth2token: OAuth2 Token consumer: name: Name @@ -240,6 +240,10 @@ en: consumers: show: link: Consumer + codeharbor_link: + profile_label: Codeharbor Link + new: Create link to Codeharbor + edit: Edit existing link execution_environments: form: hints: diff --git a/config/routes.rb b/config/routes.rb index 23ed0984..0949dea9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,7 +13,7 @@ Rails.application.routes.draw do get 'by_file_type/:file_type_id', as: :by_file_type, action: :by_file_type end end - # resources :code_harbor_links + resources :codeharbor_links resources :request_for_comments do member do get :mark_as_solved, defaults: { format: :json }