aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorhellekin <hellekin@cepheide.org>2021-03-22 15:37:40 +0100
committerhellekin <hellekin@cepheide.org>2021-03-22 15:37:40 +0100
commitcd49369079dfd5fc7c2530a9d601422dab26718b (patch)
treed418d041e9c0eb307ca6f25fa2c7b138a8c5147c /app
parent8c015485ad2ea61d128a38e7439d8b0574adc169 (diff)
parent15096ed20f918d585f7b49610f89deefda0a20b3 (diff)
downloadincommon-map-cd49369079dfd5fc7c2530a9d601422dab26718b.tar.gz
Merge branch 'uuid-resolver' into main
Diffstat (limited to 'app')
-rw-r--r--app/controllers/uuid_resolver_controller.rb65
-rw-r--r--app/helpers/uuid_resolver_helper.rb2
-rw-r--r--app/lib/uuid_resolver.rb55
-rw-r--r--app/views/uuid_resolver/new.html.erb15
4 files changed, 137 insertions, 0 deletions
diff --git a/app/controllers/uuid_resolver_controller.rb b/app/controllers/uuid_resolver_controller.rb
new file mode 100644
index 0000000..095d26d
--- /dev/null
+++ b/app/controllers/uuid_resolver_controller.rb
@@ -0,0 +1,65 @@
+# == UUIDResolverController
+#
+# This controller enables applications to retrieve information about a given UUID.
+
+# It can be used to verify the availability of this UUID in the database, e.g.,
+# when a remote Agent assigns an UUID to a record, or when it looks up the
+# record for that UUID.
+#
+# Usually it would either find no matching record (and can thus safely assign
+# the UUID) or a single one (and be redirected to the matching resource.).
+#
+# It's also possible, but unlikely, that the UUID matches more than one record
+# (e.g., a Map and a Resource), given the construction of UUIDs. In that case,
+# the controller will return the list of matching records.
+#
+# === Verifying the availability of an UUID
+#
+# When the call is made to verify an UUID is not yet assigned, a 404 (Not Found)
+# response means the UUID is available.
+#
+# === Identifying an existing record
+#
+# When the call is made to verify the existence of a record matching this UUID,
+# a 302 (Found) response means a single record was found, and the Location
+# header gives its URL. If a GET request was made, then the User-Agent will be
+# redirected to that Location.
+class UUIDResolverController < ApplicationController
+ # GET /by-uuid/:uuid
+ def new
+ @resolver = UUIDResolver.new(params[:uuid])
+
+ case @resolver.count
+ when 0
+ render json: {
+ status: :not_found,
+ message: "UUID %<uuid>s is unknown to the system." % { uuid: @resolver.uuid },
+ uuid: @resolver.uuid
+ },
+ status: :not_found
+ when 1
+ respond_to do |format|
+ format.html { redirect_to @resolver.record }
+ format.json {
+ render json: {
+ status: :found,
+ message: "UUID %<uuid>s was found at %<url>s." % {
+ uuid: @resolver.uuid,
+ url: url_for(@resolver.record)
+ },
+ uuid: @resolver.uuid,
+ location: url_for(@resolver.record)
+ }
+ }
+ end
+ else
+ render json: {
+ status: :ok,
+ message: @resolver.records.to_json
+ },
+ status: :ok
+ end
+ rescue ArgumentError => e
+ render plain: e.message, status: :unprocessable_entity
+ end
+end
diff --git a/app/helpers/uuid_resolver_helper.rb b/app/helpers/uuid_resolver_helper.rb
new file mode 100644
index 0000000..59f0aef
--- /dev/null
+++ b/app/helpers/uuid_resolver_helper.rb
@@ -0,0 +1,2 @@
+module UUIDResolverHelper
+end
diff --git a/app/lib/uuid_resolver.rb b/app/lib/uuid_resolver.rb
new file mode 100644
index 0000000..acca494
--- /dev/null
+++ b/app/lib/uuid_resolver.rb
@@ -0,0 +1,55 @@
+class UUIDResolver
+ # Note the static '4' in the third group: that's the UUID version.
+ UUID_V4_REGEX = %r[\A[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}\z]
+
+ attr_reader :records, :count, :record, :uuid
+
+ def initialize(uuid)
+ @uuid = validate!(uuid)
+ @records, @count = resolve!
+
+ end
+
+ def record
+ case @count
+ when 0
+ nil
+ else
+ records.first
+ end
+ end
+
+ private
+
+ # List models that have UUIDs
+ def public_record_types
+ [
+ ::Agent,
+ ::Map,
+ ::Resource,
+ ::Taxonomy
+ ].freeze
+ end
+
+ # Find records with this UUID
+ def resolve!
+ records = []
+
+ public_record_types.each do |model|
+ records << model.find_by(uuid: @uuid)
+ end
+
+ [records.compact, records.compact.size]
+ end
+
+ # Ensure the passed UUID is correct
+ def validate!(uuid)
+ validate_uuid_v4(uuid) || raise(ArgumentError.new("You must pass a valid random UUID (https://tools.ietf.org/html/rfc4122)"))
+ end
+
+ # Validate a UUID version 4 (random)
+ def validate_uuid_v4(uuid)
+ uuid = uuid.to_s.downcase
+ uuid.match?(UUID_V4_REGEX) ? uuid : false
+ end
+end
diff --git a/app/views/uuid_resolver/new.html.erb b/app/views/uuid_resolver/new.html.erb
new file mode 100644
index 0000000..1cefbfc
--- /dev/null
+++ b/app/views/uuid_resolver/new.html.erb
@@ -0,0 +1,15 @@
+<h1>Resolving <%= @resolver.uuid %></h1>
+<p>Found <%= pluralize(@resolver.count, "result") %>.</p>
+
+<% if @resolver.count == 1 %>
+ <%= render @resolver.record %>
+<% elsif @resolver.count > 1 %>
+ <% @resolver.records.each do |rec| %>
+ <%= render rec %>
+ <% end %>
+<% end %>
+
+<% content_for :debug do %>
+ <%= h @resolver.records.inspect %>
+<% end %>
+