From 8c306669af7f6f7ff4e96d04d7fffe62e06998cd Mon Sep 17 00:00:00 2001 From: Karol Date: Tue, 27 Aug 2019 18:33:21 +0200 Subject: [PATCH] codeharbor_links editable through own profile --- app/assets/javascripts/codeharbor_link.js | 49 +++++++++++++++++++ .../codeharbor_links_controller.rb | 20 ++++---- app/policies/codeharbor_link_policy.rb | 24 ++++----- app/views/application/_navigation.html.slim | 2 +- app/views/codeharbor_links/_form.html.slim | 26 +++++++++- app/views/codeharbor_links/edit.html.slim | 2 +- app/views/codeharbor_links/index.html.slim | 18 ------- app/views/codeharbor_links/show.html.slim | 7 --- app/views/internal_users/show.html.slim | 9 ++-- config/locales/en.yml | 13 +++++ config/routes.rb | 2 +- 11 files changed, 116 insertions(+), 56 deletions(-) create mode 100644 app/assets/javascripts/codeharbor_link.js delete mode 100644 app/views/codeharbor_links/index.html.slim delete mode 100644 app/views/codeharbor_links/show.html.slim diff --git a/app/assets/javascripts/codeharbor_link.js b/app/assets/javascripts/codeharbor_link.js new file mode 100644 index 00000000..bf8c74e1 --- /dev/null +++ b/app/assets/javascripts/codeharbor_link.js @@ -0,0 +1,49 @@ +$(document).on('turbolinks:load', function() { + $('[data-toggle="tooltip"]').tooltip(); + if($.isController('codeharbor_links')) { + if ($('.edit_codeharbor_link, .new_codeharbor_link').isPresent()) { + + var replace = (function(string) { + var d = getDate(); + return string.replace(/[xy]/g, function (c) { + var r = (d + Math.random() * 16) % 16 | 0; + d = Math.floor(d / 16); + return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16); + }); + }); + + var getDate = (function () { + var d = new Date().getTime(); + if (typeof performance !== 'undefined' && typeof performance.now === 'function') { + d += performance.now(); //use high-precision timer if available + } + return d + }); + + var generateUUID = (function () { // Public Domain/MIT + // var d = getDate(); + return replace('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'); + }); + + var generateRandomHex32 = (function () { + // var d = getDate(); + return replace(Array(32).join('x')); + }); + + + + $('.generate-client-id').on('click', function () { + $('.client-id').val(generateUUID()); + }); + + $('.generate-client-secret').on('click', function () { + $('.client-secret').val(generateRandomHex32()); + }); + + $('.generate-oauth2-token').on('click', function () { + $('.oauth2-token').val(generateRandomHex32()) + }); + } + } +}); + diff --git a/app/controllers/codeharbor_links_controller.rb b/app/controllers/codeharbor_links_controller.rb index 1eee74df..ddff149f 100644 --- a/app/controllers/codeharbor_links_controller.rb +++ b/app/controllers/codeharbor_links_controller.rb @@ -7,14 +7,14 @@ class CodeharborLinksController < ApplicationController end private :authorize! - def index - @codeharbor_links = CodeharborLink.where(user_id: current_user.id).paginate(page: params[:page]) - authorize! - end + # def index + # @codeharbor_links = CodeharborLink.where(user_id: current_user.id).paginate(page: params[:page]) + # authorize! + # end - def show - authorize! - end + # def show + # authorize! + # end def new @codeharbor_link = CodeharborLink.new @@ -29,16 +29,16 @@ class CodeharborLinksController < ApplicationController @codeharbor_link = CodeharborLink.new(codeharbor_link_params) @codeharbor_link.user = current_user authorize! - create_and_respond(object: @codeharbor_link) + create_and_respond(object: @codeharbor_link, path: proc { @codeharbor_link.user }) end def update - update_and_respond(object: @codeharbor_link, params: codeharbor_link_params) + update_and_respond(object: @codeharbor_link, params: codeharbor_link_params, path: @codeharbor_link.user) authorize! end def destroy - destroy_and_respond(object: @codeharbor_link) + destroy_and_respond(object: @codeharbor_link, path: @codeharbor_link.user) end private diff --git a/app/policies/codeharbor_link_policy.rb b/app/policies/codeharbor_link_policy.rb index e28e3af7..e83f9b75 100644 --- a/app/policies/codeharbor_link_policy.rb +++ b/app/policies/codeharbor_link_policy.rb @@ -2,6 +2,14 @@ class CodeharborLinkPolicy < ApplicationPolicy def index? + false + end + + def show? + false + end + + def new? teacher? end @@ -9,23 +17,15 @@ class CodeharborLinkPolicy < ApplicationPolicy teacher? end - def show? - teacher? - end - def edit? teacher? end - def destroy? - teacher? - end - - def new? - teacher? - end - def update? teacher? end + + def destroy? + teacher? + end end diff --git a/app/views/application/_navigation.html.slim b/app/views/application/_navigation.html.slim index c4626795..738dfc9d 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, CodeharborLink], + = render('navigation_submenu', title: t('navigation.sections.integrations'), models: [Consumer], cached: true) diff --git a/app/views/codeharbor_links/_form.html.slim b/app/views/codeharbor_links/_form.html.slim index de351ec7..fac3c05c 100644 --- a/app/views/codeharbor_links/_form.html.slim +++ b/app/views/codeharbor_links/_form.html.slim @@ -1,6 +1,28 @@ = form_for(@codeharbor_link) do |f| = render('shared/form_errors', object: @codeharbor_link) + .form-group + = f.label(:push_url) + = f.text_field :push_url, data: {toggle: 'tooltip', placement: 'bottom'}, title: t('codeharbor_link.info.push_url'), class: 'form-control' .form-group = f.label(:oauth2token) - = f.text_field(:oauth2token, class: 'form-control', required: true) - .actions = render('shared/submit_button', f: f, object: @codeharbor_link) + .input-group + = f.text_field(:oauth2token, data: {toggle: 'tooltip', placement: 'bottom'}, title: t('codeharbor_link.info.token'), class: 'form-control oauth2-token') + .input-group-btn + = button_tag t('codeharbor_link.generate'), type: 'button', class: 'generate-oauth2-token btn btn-default' + .field-element.form-group + = f.label(:client_id) + .input-group + = f.text_field(:client_id, data: {toggle: 'tooltip', placement: 'bottom'}, title: t('codeharbor_link.info.client_id'), class: 'form-control client-id') + .input-group-btn + = button_tag t('codeharbor_link.generate'), type: 'button', class: 'generate-client-id btn btn-default' + .field-element.form-group + = f.label(:client_secret) + .input-group + = f.text_field(:client_secret, data: {toggle: 'tooltip', placement: 'bottom'}, title: t('codeharbor_link.info.client_secret'), class: 'form-control client-secret') + .input-group-btn + = button_tag t('codeharbor_link.generate'), type: 'button', class: 'generate-client-secret btn btn-default' + .actions + = render('shared/submit_button', f: f, object: @codeharbor_link) + - if @codeharbor_link.persisted? + = link_to(t('codeharbor_link.delete'), codeharbor_link_path(@codeharbor_link), data: {confirm: t('shared.confirm_destroy')}, method: :delete, class: 'btn btn-danger pull-right') + diff --git a/app/views/codeharbor_links/edit.html.slim b/app/views/codeharbor_links/edit.html.slim index 6cb4a21b..43a92c7e 100644 --- a/app/views/codeharbor_links/edit.html.slim +++ b/app/views/codeharbor_links/edit.html.slim @@ -1,3 +1,3 @@ -h1 = @codeharbor_link +h1 = 'Codeharbor link' = render('form') diff --git a/app/views/codeharbor_links/index.html.slim b/app/views/codeharbor_links/index.html.slim deleted file mode 100644 index be6833c9..00000000 --- a/app/views/codeharbor_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.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/show.html.slim b/app/views/codeharbor_links/show.html.slim deleted file mode 100644 index b17c86e7..00000000 --- a/app/views/codeharbor_links/show.html.slim +++ /dev/null @@ -1,7 +0,0 @@ -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 da5620ff..6e876ec9 100644 --- a/app/views/internal_users/show.html.slim +++ b/app/views/internal_users/show.html.slim @@ -8,8 +8,9 @@ h1 = 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.profile_label', value: @user.codeharbor_link.nil? ? link_to(t('codeharbor_link.new'), new_codeharbor_link_path, class: 'btn btn-secondary') : link_to(t('codeharbor_link.edit'), edit_codeharbor_link_path(@user.codeharbor_link), class: 'btn btn-secondary')) - = 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')) +/ - if @user.codeharbor_link.nil? +/ = row(label: 'codeharbor_link.profile_label', value: link_to(t('codeharbor_link.new'), codeharbor_link_new_path, class: 'btn btn-secondary')) +/ - else +/ = row(label: 'codeharbor_link.profile_label', value: link_to(t('codeharbor_link.edit'), edit_codeharbor_link_path(@user.codeharbor_link), class: 'btn btn-secondary')) diff --git a/config/locales/en.yml b/config/locales/en.yml index 90f1f69e..ff8d8bb1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -241,9 +241,22 @@ en: show: link: Consumer codeharbor_link: + generate: Generate + info: + push_url: | + The address on CodeHarbor side your exercise can be exported to. If you don't know what to write here, ask an admin. + name: | + Can be anything to Identify your account link. + token: | + Will be used to authenticate your export request. Has to be the same on both sides. + client_id: + Will be sent with your token. Can be automatically generated. + client_secret: + Will be sent with your token. Can be automatically generated. profile_label: Codeharbor Link new: Create link to Codeharbor edit: Edit existing link + delete: Remove Codeharbor link execution_environments: form: hints: diff --git a/config/routes.rb b/config/routes.rb index 0949dea9..9310e946 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 :codeharbor_links + resources :codeharbor_links, only: %i[new create edit update destroy] resources :request_for_comments do member do get :mark_as_solved, defaults: { format: :json }