Commit 768173d8 authored by Tobias Kallauke's avatar Tobias Kallauke
Browse files

Consumer Config Parameters

parent eac5f314
class Config
attr_accessor :parameter_id, :name, :type, :format, :default, :value
class << self
# @param [Hash] hash
def from_hash(hash)
c = Config.new
hash.each { |k, v| c.send("#{k}=", v) }
c
end
# @param [Integer] group
# @param [Integer] consumer
def list(group, consumer)
res = RestClient.get("#{ENV['API_URL']}groups/#{group}/consumers/#{consumer}/config")
json = JSON.parse(res.body)
json.map do |e|
Config.from_hash(e)
end
end
# @param [Integer] group
# @param [Integer] consumer
# @param [Integer] parameter
# @param [String, nil] value
# @return [Array[Config]]
def set(group, consumer, parameter, value)
if value.nil? || value.empty?
RestClient.delete("#{ENV['API_URL']}groups/#{group}/consumers/#{consumer}/config/#{parameter}")
else
RestClient.post("#{ENV['API_URL']}groups/#{group}/consumers/#{consumer}/config",
{ parameter: parameter, value: value }.to_json)
end
end
end
end
# frozen_string_literal: true
class ConsumerController < ApplicationController
def get
@group_id = Integer(params[:gid])
......@@ -6,13 +8,15 @@ class ConsumerController < ApplicationController
@consumers = Group.get_consumers(@group_id)
@test = session[:user_id]
@more_selected = @consumers.filter { |c| c.id == @consumer_id }.empty?
@members,@my_role = Group.get_members(@group_id, session[:user_id])
@members, @my_role = Group.get_members(@group_id, session[:user_id])
data = Group.get_capabilities(@group_id, @consumer_id)
@capabilities = data[:capabilities]
@users = data[:users]
@config = Config.list(@group_id, @consumer_id)
@info = I18n.t 'help.infobox.consumerPage'
end
......@@ -26,6 +30,27 @@ class ConsumerController < ApplicationController
to_remove = params['remove']
Group.remove_capability(params[:gid], to_remove) if !to_remove.nil? && to_remove.length
end
def update_config
group_id = Integer(params[:gid])
consumer_id = Integer(params[:id])
config = Config.list(group_id, consumer_id)
# @type [Hash<String, String>]
input = params.require :_json
# Turn input into parameter -> value hash
data = input.to_h { |x| [x['parameter_id'], (x['value'].nil? || x['value'].empty? ? nil : x['value'])] }
# Remove values that are some as default and/or current value
data.filter! do |k, v|
c = config.find { |e| e.parameter_id == k }
!(v == c.default || v == c.value)
end
data.each { |k, v| Config.set(group_id, consumer_id, k.to_i, v.nil? || v.empty? ? nil : v) }
head :no_content
end
end
......@@ -8,6 +8,18 @@ interface CapabilityPair {
const add: CapabilityPair[] = []
const remove: CapabilityPair[] = []
let loading = false
function setLoading() {
loading = true
const buttons = [ $("#config-save"), $("#permission-save") ]
for (const button of buttons) {
button.children(".spinner-border").removeClass("d-none")
button.prop('disabled', true)
}
}
$(() => {
$("input[type=checkbox]").on("change", function () {
const el = $(this)
......@@ -45,6 +57,9 @@ $(() => {
})
$("button#permission-save").on("click", function () {
if (loading) return
setLoading()
let temp = add.map(e => `user: ${e.id}, capability: ${e.capability}`).join('\n')
console.log(`Permission pairs to create: ${temp}`)
......@@ -61,4 +76,48 @@ $(() => {
location.reload()
})
})
$("#form-config").on("submit", function (e) {
e.preventDefault()
if (loading) return
setLoading()
type ConfigValue = {
parameter_id: number,
value: string
}
let data: ConfigValue[] = []
let inputs = Array.from((e.target as HTMLFormElement).elements)
for (let el of inputs) {
// FormElementCollection also contains buttons
if (el.tagName !== "INPUT")
continue
const input = <HTMLInputElement> el
const parameter_id = parseInt(input.name.substring("config-".length))
if (isNaN(parameter_id))
alert("Could not read parameter id for input" + input.innerHTML)
let value
if (input.type == "text")
value = input.value
else if (input.type == "checkbox")
value = input.value == "on" ? "1" : "0"
data.push({parameter_id, value})
}
$.ajax({
type: "POST",
url: location.href + "/config",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
}).then(() => {
location.reload()
})
})
});
<%= render partial: "shared/group-header", locals: { group: @group } %>
<%= render partial: "shared/group-nav", locals: {group_id: @group_id, consumers: @consumers, consumer_selected: @consumer_id, more_selected: @more_selected } %>
<br>
<%= t('consumer.'+ @consumer_id.to_s + '.description') %>
<br><br>
<table class="table">
<thead>
<tr>
<th>Login</th>
<% @capabilities.each do |c| %>
<th>
<span data-toggle="tooltip" data-placement="bottom" title="<%= t('consumer.' + @consumer_id.to_s + '.permissions.' + c.id.to_s, default: 'Leider noch keine Beschreibung hinterlegt.')%>">
<%= t('consumer.' + @consumer_id.to_s + '.permissions.' + 'name' + c.id.to_s ) %>
</span>
</th>
<%= render partial: "shared/group-header", locals: { group: @group } %>
<%= render partial: "shared/group-nav", locals: { group_id: @group_id, consumers: @consumers, consumer_selected: @consumer_id, more_selected: @more_selected } %>
<div>
<h1><%= t('consumer.' + @consumer_id.to_s + '.name') %></h1>
<p><%= t('consumer.' + @consumer_id.to_s + '.description') %></p>
</div>
<div>
<h2>Konfigurationsparameter</h2>
<form id="form-config">
<% @config.each do |c| %>
<div class="row">
<div class="col-2">
<label for="config-<%= c.parameter_id %>" class="form-label mb-0"><%= c.name %></label>
<% if c.default.nil? %>
<p class="text-secondary">Kein Standardwert</p>
<% else %>
<p class="text-secondary">Standardwert: <%= c.default %></p>
<% end %>
</div>
<div class="col-2 d-flex flex-column justify-content-center">
<% if c.type == "string" %>
<input type="text" id="config-<%= c.parameter_id %>" name="config-<%= c.parameter_id %>" value="<%= c.value || c.default %>" class="form-control">
<% elsif c.type == "bool" %>
<input type="checkbox" id="config-<%= c.parameter_id %>" name="config-<%= c.parameter_id %>" <%= "checked" if (c.value || c.default) == "1" %> class="form-check-input">
<% else %>
<p>Unimplemented config type: <%= c.type %></p>
<% end %>
</div>
</div>
<% end %>
</tr>
</thead>
<tbody>
<% if @my_role == Roles::ADMIN %>
<% @users.each do |m| %>
<tr>
<td><%= m.login %></td>
<% m.capabilities.each_with_index do |c, i| %>
<td>
<input class="form-check-input position-static ml-1" type="checkbox"
data-uid="<%= m.id %>" data-cid="<%= @capabilities[i].id %>"
autocomplete="off" <%= "data-initial checked" if c %>
>
</td>
<% end %>
</tr>
<button type="submit" class="btn btn-success" id="config-save">
<span class="spinner-border spinner-border-sm d-none" role="status" aria-hidden="true"></span>
Konfiguration speichern
</button>
</form>
</div>
<h2>Rechte</h2>
<table class="table">
<thead>
<tr>
<th>Login</th>
<% @capabilities.each do |c| %>
<th>
<span data-toggle="tooltip" data-placement="bottom" title="<%= t('consumer.' + @consumer_id.to_s + '.permissions.' + c.id.to_s, default: 'Leider noch keine Beschreibung hinterlegt.') %>">
<%= t('consumer.' + @consumer_id.to_s + '.permissions.' + 'name' + c.id.to_s) %>
</span>
</th>
<% end %>
</tr>
</thead>
<tbody>
<% if @my_role == Roles::ADMIN %>
<% @users.each do |m| %>
<tr>
<td><%= m.login %></td>
<% m.capabilities.each_with_index do |c, i| %>
<td>
<input class="form-check-input position-static ml-1" type="checkbox"
data-uid="<%= m.id %>" data-cid="<%= @capabilities[i].id %>"
autocomplete="off" <%= "data-initial checked" if c %>
>
</td>
<% end %>
</tr>
<% end %>
<% end %>
<% end %>
<% if @my_role == Roles::MEMBER %>
<% @users.each do |m| %>
<tr>
<td><%= m.login %></td>
<% m.capabilities.each_with_index do |c, i| %>
<td>
<input class="form-check-input position-static ml-1" type="checkbox"
data-uid="<%= m.id %>" data-cid="<%= @capabilities[i].id %>"
autocomplete="off" disabled <%= "data-initial checked" if c %>
>
</td>
<% end %>
</tr>
<% if @my_role == Roles::MEMBER %>
<% @users.each do |m| %>
<tr>
<td><%= m.login %></td>
<% m.capabilities.each_with_index do |c, i| %>
<td>
<input class="form-check-input position-static ml-1" type="checkbox"
data-uid="<%= m.id %>" data-cid="<%= @capabilities[i].id %>"
autocomplete="off" disabled <%= "data-initial checked" if c %>
>
</td>
<% end %>
</tr>
<% end %>
<% end %>
<% end %>
</tbody>
</table>
</tbody>
</table>
<% if @my_role == Roles::ADMIN %>
<button class="mr-3 btn btn-primary" id="permission-save"><%= t('consumer.saveButton') %></button>
<% end %>
<% if @my_role == Roles::ADMIN %>
<button class="mr-3 btn btn-primary" id="permission-save">
<span class="spinner-border spinner-border-sm d-none" role="status" aria-hidden="true"></span>
<%= t('consumer.saveButton') %>
</button>
<% end %>
......@@ -11,6 +11,7 @@ Rails.application.routes.draw do
get 'group/:gid/consumer/:id', to: 'consumer#get', constraints: { gid: /[0-9]+/, id: /[0-9]+/ }
post 'group/:gid/consumer/:id', to: 'consumer#update', constraints: { gid: /[0-9]+/, id: /[0-9]+/ }
post 'group/:gid/consumer/:id/config', to: 'consumer#update_config', constraints: { gid: /[0-9]+/, id: /[0-9]+/ }
post 'group/:gid/members', to: 'group#add_user', constraints: { gid: /[0-9]+/ }
delete 'group/:gid/members', to: 'group#remove_user', constraints: { gid: /[0-9]+/ }
......
......@@ -3,7 +3,7 @@
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es2016", "dom"],
"lib": ["es2016", "dom", "dom.iterable"],
"module": "es6",
"moduleResolution": "node",
"baseUrl": ".",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment