codeharbor_links editable through own profile

This commit is contained in:
Karol
2019-08-27 18:33:21 +02:00
parent ec48d1f447
commit 8c306669af
11 changed files with 116 additions and 56 deletions

View File

@ -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())
});
}
}
});

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,3 @@
h1 = @codeharbor_link
h1 = 'Codeharbor link'
= render('form')

View File

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

View File

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

View File

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

View File

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

View File

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