Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Store up to ten labelled details per person

Guido Boyke417a47459ad7
diff --git a/app/controllers/organisations/people_controller.rb b/app/controllers/organisations/people_controller.rb
index fb2e3e9..c40a39c 100644
--- a/app/controllers/organisations/people_controller.rb
+++ b/app/controllers/organisations/people_controller.rb
@@ -146,8 +146,12 @@ module Organisations
result.resource || new_person
end
+ # The additional details come as an indexed hash of any shape here;
+ # PersonDetail.normalize (through SavePerson) keeps only kind, label and
+ # value of the numbered rows. Anything but a hash drops the key, and the
+ # stored details stay.
def person_params
- params.require(:person).permit(*Actors::SavePerson::ATTRIBUTES, :user_id)
+ params.require(:person).permit(*Actors::SavePerson::ATTRIBUTES, :user_id, Actors::SavePerson::DETAILS => {})
end
def directory
diff --git a/app/helpers/people_helper.rb b/app/helpers/people_helper.rb
index 3198b11..1b75b81 100644
--- a/app/helpers/people_helper.rb
+++ b/app/helpers/people_helper.rb
@@ -6,6 +6,8 @@ module PeopleHelper
# The two things a row can open in the lane: a person row, or a member who
# has no row of their own yet. They exclude each other.
SELECTION_PARAMS = %w[person_id member_id].freeze
+ # The icon of an additional detail that is a link (PersonDetail#href).
+ PERSON_DETAIL_ICONS = { 'phone' => 'bi-telephone', 'email' => 'bi-envelope', 'url' => 'bi-link-45deg' }.freeze
# The page wrapper's data: the lane (layout-toggle, index-page) and the
# desktop/mobile switch, the same controllers as "Alle Meetings"
@@ -104,12 +106,54 @@ module PeopleHelper
# A member row with none of the details the directory is for yet.
def person_bare?(person)
- person.nil? || %i[salutation function department phone notes].all? { |field| person.public_send(field).blank? }
+ person.nil? ||
+ %i[salutation function department phone notes additional_details].all? { |field| person.public_send(field).blank? }
end
# The lines of the lane: label, value, and for email and phone the link. An
- # active member's email is the account's (the form locks it).
+ # active member's email is the account's (the form locks it). The
+ # additional details follow the phone, in their stored order.
def person_fields(person, member)
+ person_fixed_fields(person, member) + person_detail_fields(person)
+ end
+
+ # One line per additional detail (PersonDetail): the label, or the name of
+ # the kind without one. A phone, an email and an http(s) url are links (a
+ # url opens a new tab), a date is formatted for the reader, an address keeps
+ # its lines. On a phone a link is a tap target named by its label.
+ def person_detail_fields(person)
+ details = person&.additional_details
+ return [] unless details.is_a?(Array)
+
+ details.map do |entry|
+ detail = PersonDetail.new(entry)
+ label = detail.label.presence || person_detail_kind_name(detail.kind)
+ field = { label: label, value: person_detail_value(detail) }
+ href = detail.href
+ next field unless href
+
+ field.merge(href: href, icon: PERSON_DETAIL_ICONS[detail.kind], mono: detail.kind == 'phone',
+ action: label, external: detail.kind == 'url')
+ end
+ end
+
+ def person_detail_kind_name(kind)
+ return kind.to_s unless PersonDetail::KINDS.include?(kind)
+
+ t("views.organisations.people.details.kinds.#{kind}")
+ end
+
+ # The value of a detail as the lane shows it.
+ def person_detail_value(detail)
+ date = detail.date
+ return l(date, format: :long) if date
+ return safe_join(detail.lines, tag.br) if detail.kind == 'address'
+
+ detail.value
+ end
+
+ # The fixed fields of the lane, in the order of the form.
+ def person_fixed_fields(person, member)
email = member ? member.email : person&.email
phone = person&.phone
[
diff --git a/app/models/person.rb b/app/models/person.rb
index 9cf5915..82cbe55 100644
--- a/app/models/person.rb
+++ b/app/models/person.rb
@@ -11,6 +11,8 @@
# (PersonLinkRelease). Tracking keeps no personal data (PersonTrackable).
# salutation is free text ("Herr", "Frau Dr."): often only "Frau Müller" is
# known, without a first name. Blank means none given and is stored as nil.
+# additional_details: up to ten labelled details (fax, mobile, website,
+# birthday) as a JSONB array; their rule belongs to PersonDetail alone.
# lock_version: both sides edit the same row; a stale save raises
# ActiveRecord::StaleObjectError.
class Person < ApplicationRecord
@@ -31,6 +33,7 @@ class Person < ApplicationRecord
TEXT_FIELDS = %i[name salutation function department company email phone].freeze
before_validation :normalize_name_fields
+ before_validation :normalize_additional_details
validates :name, presence: true
validates :normalized_name, presence: true
@@ -38,6 +41,7 @@ class Person < ApplicationRecord
validates :notes, length: { maximum: MAX_NOTES_LENGTH }
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_blank: true
validates :user_id, uniqueness: { scope: :organisation_id }, allow_nil: true
+ validate :additional_details_valid
validate :user_must_be_active_member, if: -> { user_id.present? && will_save_change_to_user_id? }
scope :linked, -> { where.not(user_id: nil) }
@@ -65,6 +69,16 @@ class Person < ApplicationRecord
self.salutation = salutation.to_s.squish.presence
end
+ def normalize_additional_details
+ self.additional_details = PersonDetail.normalize(additional_details)
+ end
+
+ def additional_details_valid
+ PersonDetail.problems(additional_details).each do |problem, options|
+ errors.add(:additional_details, problem, **options)
+ end
+ end
+
# Same rule as Actors::SaveMeeting for participants: only an active member of
# this organisation can be linked. A deactivated or foreign user is refused.
def user_must_be_active_member
diff --git a/app/models/person_detail.rb b/app/models/person_detail.rb
new file mode 100644
index 0000000..34a2571
--- /dev/null
+++ b/app/models/person_detail.rb
@@ -0,0 +1,175 @@
+# frozen_string_literal: true
+
+# One additional detail of a person (people.additional_details, a JSONB array
+# of at most MAX entries): a kind, a free label and a value, say
+# { "kind" => "phone", "label" => "Fax", "value" => "+49 30 1234568" }.
+#
+# Not an ActiveRecord model: the one owner of the rule. Kinds, limits,
+# normalisation and validation live here and nowhere else; Person validates
+# through it, Actors::SavePerson normalizes the form through it, and the form
+# (kinds, input types, MAX), the lane (links, dates), the search and the
+# export ask it. The check constraint people_additional_details_shape repeats
+# only "an array of at most 10".
+#
+# The kind decides the behaviour (a tel: or mailto: link, a date in the
+# reader's language); the label only says what the value means. A fax is a
+# phone labelled "Fax". No reminders for dates, and the search reads the
+# values, never the labels ("Fax" would find everybody with a fax).
+class PersonDetail
+ KINDS = %w[phone email url date address text].freeze
+ KEYS = %w[kind label value].freeze
+ MAX = 10
+ MAX_LABEL_LENGTH = 60
+ MAX_VALUE_LENGTH = 255
+ MAX_ADDRESS_LENGTH = 500
+ URL_SCHEMES = %w[http https].freeze
+ # The input of the form per kind; "textarea" is the multi-line address.
+ INPUT_TYPES = { 'phone' => 'tel', 'email' => 'email', 'url' => 'url', 'date' => 'date',
+ 'address' => 'textarea', 'text' => 'text' }.freeze
+ # The form always sends this key next to the indexed rows
+ # (person[additional_details][sent]), so removing the last detail clears the
+ # list. A request without additional_details at all leaves the list alone.
+ FORM_MARKER = 'sent'
+ # The indexes of the form's rows; anything else in the hash is ignored.
+ INDEX_KEY = /\A\d{1,9}\z/
+ ISO_DATE = /\A\d{4}-\d{2}-\d{2}\z/
+ LINE_BREAK = /\r\n|\r|\n/
+
+ attr_reader :kind, :label, :value
+
+ # Raw input in, a list of clean entries out: an array of hashes, or the
+ # indexed hash of the form ({"0" => {...}, "3" => {...}}, ordered by index).
+ # Only the three keys, as strings; label and value squeezed (an address keeps
+ # its lines); a row without a value is dropped. An unknown kind is kept for
+ # the validation. Junk from a hand-made request is skipped, never raised on.
+ def self.normalize(raw)
+ rows(raw).filter_map { |row| clean(row) }
+ end
+
+ # What is wrong with a list, as [problem, options] pairs; empty when valid.
+ # The position (1-based) goes into the options of each entry's problem, for
+ # the messages under activerecord.errors.models.person.attributes.
+ def self.problems(list)
+ return [[:not_a_list, {}]] unless list.is_a?(Array)
+ return [[:too_many, { count: MAX }]] if list.size > MAX
+
+ list.each_with_index.flat_map do |entry, index|
+ new(entry).problems.map { |problem, options| [problem, { position: index + 1 }.merge(options)] }
+ end
+ end
+
+ # The values the directory search reads (People::Directory), never a label.
+ def self.search_values(list)
+ return [] unless list.is_a?(Array)
+
+ list.map { |entry| new(entry).value }.compact_blank
+ end
+
+ def self.max_value_length(kind)
+ kind == 'address' ? MAX_ADDRESS_LENGTH : MAX_VALUE_LENGTH
+ end
+
+ def self.rows(raw)
+ raw = raw.to_unsafe_h if raw.respond_to?(:to_unsafe_h)
+ case raw
+ when Array then raw
+ when Hash
+ raw.select { |key, _row| INDEX_KEY.match?(key.to_s) }.sort_by { |key, _row| key.to_s.to_i }.map(&:last)
+ else []
+ end
+ end
+ private_class_method :rows
+
+ def self.clean(row)
+ row = row.to_unsafe_h if row.respond_to?(:to_unsafe_h)
+ return unless row.is_a?(Hash)
+
+ kind = string_in(row, 'kind')&.squish
+ value = string_in(row, 'value').to_s
+ value = kind == 'address' ? address_lines(value).join("\n") : value.squish
+ return if value.empty?
+
+ { 'kind' => kind, 'label' => string_in(row, 'label').to_s.squish, 'value' => value }
+ end
+ private_class_method :clean
+
+ # A string read from a row (string or symbol key), nil for anything else.
+ def self.string_in(row, key)
+ found = row.key?(key) ? row[key] : row[key.to_sym]
+ case found
+ when String then found
+ when Symbol, Numeric then found.to_s
+ end
+ end
+
+ def self.address_lines(value)
+ value.split(LINE_BREAK).map(&:squish).compact_blank
+ end
+
+ # A stored entry, read without trusting it: written around the validation
+ # (a console, update_column) it still renders, and nothing here raises.
+ def initialize(entry)
+ entry = {} unless entry.is_a?(Hash)
+ @kind = self.class.string_in(entry, 'kind')
+ @label = self.class.string_in(entry, 'label').to_s
+ @value = self.class.string_in(entry, 'value').to_s
+ end
+
+ def known_kind?
+ KINDS.include?(kind)
+ end
+
+ def problems
+ found = []
+ found << [:unknown_kind, {}] unless known_kind?
+ return found << [:value_missing, {}] if value.blank?
+
+ found << [:label_too_long, { count: MAX_LABEL_LENGTH }] if label.length > MAX_LABEL_LENGTH
+ max = self.class.max_value_length(kind)
+ found << [:value_too_long, { count: max }] if value.length > max
+ found << [:"invalid_#{kind}", {}] if known_kind? && !well_formed?
+ found
+ end
+
+ # Where a click on the value goes, or nil: tel: through the digits, mailto:,
+ # an http(s) url as written. Only for a well-formed value, so a url stored
+ # around the validation (javascript:, data:) is never a link.
+ def href
+ return unless well_formed?
+
+ case kind
+ when 'phone' then "tel:#{value.gsub(/[^\d+]/, '')}"
+ when 'email' then "mailto:#{value}"
+ when 'url' then value
+ end
+ end
+
+ def date
+ Date.iso8601(value) if kind == 'date' && ISO_DATE.match?(value)
+ rescue Date::Error
+ nil
+ end
+
+ def lines
+ self.class.address_lines(value)
+ end
+
+ private
+
+ def well_formed?
+ case kind
+ when 'phone' then value.match?(/\d/)
+ when 'email' then value.match?(URI::MailTo::EMAIL_REGEXP)
+ when 'date' then !date.nil?
+ when 'url' then http_url?
+ else known_kind?
+ end
+ end
+
+ def http_url?
+ uri = URI.parse(value)
+ URL_SCHEMES.include?(uri.scheme.to_s.downcase) && uri.host.present?
+ rescue URI::Error
+ false
+ end
+end
diff --git a/app/services/actors/save_person.rb b/app/services/actors/save_person.rb
index ea12ce8..e218edd 100644
--- a/app/services/actors/save_person.rb
+++ b/app/services/actors/save_person.rb
@@ -15,6 +15,10 @@
# and a row created for a member takes no email of its own. A DEACTIVATED
# member's row keeps only its link (LINK_FIELDS): no other member, no
# contact, until the member is removed.
+# * the additional details (PersonDetail): the form sends them as an indexed
+# hash, turned into a list in the order of the index. The form always sends
+# PersonDetail::FORM_MARKER with them, so removing the last one clears the
+# list; a request without the key leaves the list alone (like user_id).
# * creator on create, updater on every save;
# * a stale save (lock_version, both sides edit the same row) raises
# StaleObjectError, which BaseActor maps to code :stale_object; the
@@ -24,6 +28,8 @@
module Actors
class SavePerson < BaseActor
ATTRIBUTES = %i[name salutation function department company email phone notes lock_version].freeze
+ # The list of PersonDetail entries, sent next to ATTRIBUTES.
+ DETAILS = :additional_details
# What the account of an active member owns on its row.
ACCOUNT_FIELDS = %i[name email user_id].freeze
# What stays while the member is only deactivated: the row's own name and
@@ -52,6 +58,7 @@ module Actors
@params = @params.except(:email) if @person.new_record? && @params[:user_id].present?
attributes = @params.slice(*ATTRIBUTES)
attributes.delete(:lock_version) if @person.new_record?
+ attributes[DETAILS] = PersonDetail.normalize(@params[DETAILS]) if @params.key?(DETAILS)
@person.assign_attributes(attributes)
assign_link if @params.key?(:user_id)
@person.creator ||= @current_user if @person.new_record?
diff --git a/app/services/people/directory.rb b/app/services/people/directory.rb
index c6e6969..22dc2d7 100644
--- a/app/services/people/directory.rb
+++ b/app/services/people/directory.rb
@@ -16,7 +16,8 @@ module People
# * Search: one normalized substring match (PersonName.normalize, so "Muller"
# finds nothing but "mueller" and "Müller" find each other) over name
# (with the salutation in front: "Frau Müller"), function, department,
- # company and email. In Ruby, because the member half
+ # company, email and the values of the additional details (never their
+ # labels, PersonDetail.search_values). In Ruby, because the member half
# reads its names from users; the list is sized for a few hundred rows per
# organisation. Pagination is a follow-up once an organisation passes that.
class Directory
@@ -215,7 +216,8 @@ module People
def matches?(entry)
email = entry.member? ? entry.user.email : entry.person&.email
addressed = [entry.person&.salutation, entry.plain_name].compact_blank.join(' ')
- haystack = [addressed, entry.person&.function, entry.person&.department, entry.person&.company, email]
+ haystack = [addressed, entry.person&.function, entry.person&.department, entry.person&.company, email,
+ *PersonDetail.search_values(entry.person&.additional_details)]
haystack.any? { |value| value.present? && PersonName.normalize(value).include?(@query) }
end
end
diff --git a/app/services/user_data_exports/registry.rb b/app/services/user_data_exports/registry.rb
index 9a435ad..8e0958c 100644
--- a/app/services/user_data_exports/registry.rb
+++ b/app/services/user_data_exports/registry.rb
@@ -221,7 +221,8 @@ module UserDataExports
# content (as analysis_prompts); unlinked rows are data about third
# parties, whose requests go through the organisation as controller.
source(key: :people, model: 'Person', tenant_scoped: true,
- attributes: %w[name salutation function department company email phone notes created_at updated_at],
+ attributes: %w[name salutation function department company email phone additional_details notes
+ created_at updated_at],
scope: ->(user) { Person.where(user_id: user.id) }),
source(key: :notification_subscriptions, model: 'NotificationSubscription', tenant_scoped: true,
attributes: %w[subscribable_type source created_at],
diff --git a/app/views/organisations/people/_fields.html.erb b/app/views/organisations/people/_fields.html.erb
index cc8667e..61d41ea 100644
--- a/app/views/organisations/people/_fields.html.erb
+++ b/app/views/organisations/people/_fields.html.erb
@@ -1,6 +1,7 @@
<%# locals: (fields:) %>
<%# Label and value per line; email and phone are links. On a phone they are
- large tap targets instead ("anrufen"). %>
+ large tap targets instead ("anrufen"). An additional detail that is a
+ web address (external) opens in a new tab, without the opener. %>
<% if @viewport_mode == 'mobile' %>
<% plain, links = fields.partition { |field| field[:href].nil? } %>
<% if plain.any? %>
@@ -11,7 +12,8 @@
</dl>
<% end %>
<% links.each do |field| %>
- <%= link_to field[:href], class: "people-tap #{'people-tap--mono' if field[:mono]}" do %>
+ <%= link_to field[:href], class: "people-tap #{'people-tap--mono' if field[:mono]}",
+ **(field[:external] ? { target: '_blank', rel: 'noopener noreferrer' } : {}) do %>
<i class="bi <%= field[:icon] %>" aria-hidden="true"></i><%= field[:value] %>
<% if field[:action] %><span class="people-tap__hint"><%= field[:action] %></span><% end %>
<% end %>
@@ -23,7 +25,8 @@
<dt><%= field[:label] %></dt>
<dd>
<% if field[:href] %>
- <%= link_to field[:href], class: "people-fields__link #{'people-fields__link--mono' if field[:mono]}" do %>
+ <%= link_to field[:href], class: "people-fields__link #{'people-fields__link--mono' if field[:mono]}",
+ **(field[:external] ? { target: '_blank', rel: 'noopener noreferrer' } : {}) do %>
<i class="bi <%= field[:icon] %>" aria-hidden="true"></i><%= field[:value] %>
<% end %>
<% else %>
diff --git a/config/locales/models.de.yml b/config/locales/models.de.yml
index 8cc1f4a..0706e40 100644
--- a/config/locales/models.de.yml
+++ b/config/locales/models.de.yml
@@ -11,6 +11,7 @@ de:
company: "Firma"
email: "E-Mail"
phone: "Telefon"
+ additional_details: "Weitere Angaben"
notes: "Notiz"
ticket_relation:
source_ticket: "Quell-Ticket"
@@ -79,6 +80,17 @@ de:
models:
person:
attributes:
+ additional_details:
+ not_a_list: "(keine Liste)"
+ too_many: "(höchstens %{count} erlaubt)"
+ unknown_kind: "(Nr. %{position}): unbekannte Art"
+ value_missing: "(Nr. %{position}): Wert fehlt"
+ label_too_long: "(Nr. %{position}): Bezeichnung zu lang (höchstens %{count} Zeichen)"
+ value_too_long: "(Nr. %{position}): Wert zu lang (höchstens %{count} Zeichen)"
+ invalid_phone: "(Nr. %{position}): Telefonnummer ohne Ziffern"
+ invalid_email: "(Nr. %{position}): keine gültige E-Mail-Adresse"
+ invalid_url: "(Nr. %{position}): keine Webadresse mit http:// oder https://"
+ invalid_date: "(Nr. %{position}): kein gültiges Datum"
user:
not_an_active_member: "muss ein aktives Mitglied dieser Organisation sein"
beuser:
diff --git a/config/locales/models.en.yml b/config/locales/models.en.yml
index 4ed5f4f..06703af 100644
--- a/config/locales/models.en.yml
+++ b/config/locales/models.en.yml
@@ -11,6 +11,7 @@ en:
company: "Company"
email: "Email"
phone: "Phone"
+ additional_details: "Additional details"
notes: "Note"
ticket_relation:
source_ticket: "Source ticket"
@@ -79,6 +80,17 @@ en:
models:
person:
attributes:
+ additional_details:
+ not_a_list: "(not a list)"
+ too_many: "(at most %{count} allowed)"
+ unknown_kind: "(no. %{position}): unknown kind"
+ value_missing: "(no. %{position}): value missing"
+ label_too_long: "(no. %{position}): label too long (at most %{count} characters)"
+ value_too_long: "(no. %{position}): value too long (at most %{count} characters)"
+ invalid_phone: "(no. %{position}): phone number without digits"
+ invalid_email: "(no. %{position}): not a valid email address"
+ invalid_url: "(no. %{position}): not a web address with http:// or https://"
+ invalid_date: "(no. %{position}): not a valid date"
user:
not_an_active_member: "must be an active member of this organisation"
beuser:
diff --git a/config/locales/views/personen.de.yml b/config/locales/views/personen.de.yml
index fb880ee..5dfab42 100644
--- a/config/locales/views/personen.de.yml
+++ b/config/locales/views/personen.de.yml
@@ -103,6 +103,14 @@ de:
delete_confirm: "%{name} wird aus der Liste gelöscht. Das lässt sich nicht rückgängig machen."
remove_details: "Ergänzte Angaben entfernen"
remove_details_confirm: "Funktion, Abteilung, Telefon und Notiz von %{name} werden entfernt, das Konto bleibt. Das lässt sich nicht rückgängig machen."
+ details:
+ kinds:
+ phone: "Telefon"
+ email: "E-Mail"
+ url: "Webadresse"
+ date: "Datum"
+ address: "Adresse"
+ text: "Text"
form:
new_title: "Person anlegen"
edit_title: "%{name} bearbeiten"
diff --git a/config/locales/views/personen.en.yml b/config/locales/views/personen.en.yml
index 945595a..6b2f04d 100644
--- a/config/locales/views/personen.en.yml
+++ b/config/locales/views/personen.en.yml
@@ -103,6 +103,14 @@ en:
delete_confirm: "%{name} will be deleted from the list. This cannot be undone."
remove_details: "Remove added details"
remove_details_confirm: "Function, department, phone and note of %{name} will be removed, the account stays. This cannot be undone."
+ details:
+ kinds:
+ phone: "Phone"
+ email: "Email"
+ url: "Web address"
+ date: "Date"
+ address: "Address"
+ text: "Text"
form:
new_title: "Add person"
edit_title: "Edit %{name}"
diff --git a/db/migrate/20261028120000_add_additional_details_to_people.rb b/db/migrate/20261028120000_add_additional_details_to_people.rb
new file mode 100644
index 0000000..c444440
--- /dev/null
+++ b/db/migrate/20261028120000_add_additional_details_to_people.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+# Up to ten labelled details of a person (fax, mobile, website, birthday), as
+# one JSONB array next to the fixed fields. The check constraint is the second
+# line behind the model validation (PersonDetail): a console write or a later
+# API cannot store an object or an eleventh entry.
+class AddAdditionalDetailsToPeople < ActiveRecord::Migration[8.1]
+ def change
+ add_column :people, :additional_details, :jsonb, null: false, default: []
+ add_check_constraint :people,
+ "jsonb_typeof(additional_details) = 'array' AND jsonb_array_length(additional_details) <= 10",
+ name: 'people_additional_details_shape'
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index cd4e872..44373bf 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[8.1].define(version: 2026_10_28_100000) do
+ActiveRecord::Schema[8.1].define(version: 2026_10_28_120000) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
enable_extension "pgcrypto"
@@ -1075,9 +1075,11 @@ ActiveRecord::Schema[8.1].define(version: 2026_10_28_100000) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "salutation"
+ t.jsonb "additional_details", default: [], null: false
t.index ["organisation_id", "normalized_name"], name: "index_people_on_organisation_id_and_normalized_name"
t.index ["organisation_id", "user_id"], name: "index_people_on_organisation_id_and_user_id", unique: true, where: "(user_id IS NOT NULL)"
t.index ["user_id"], name: "index_people_on_user_id"
+ t.check_constraint "jsonb_typeof(additional_details) = 'array'::text AND jsonb_array_length(additional_details) <= 10", name: "people_additional_details_shape"
end
create_table "permissions", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
diff --git a/spec/helpers/people_helper_spec.rb b/spec/helpers/people_helper_spec.rb
index 7934d34..6e51c4f 100644
--- a/spec/helpers/people_helper_spec.rb
+++ b/spec/helpers/people_helper_spec.rb
@@ -41,6 +41,78 @@ RSpec.describe PeopleHelper, type: :helper do
expect(helper.person_bare?(Person.new(name: 'Müller', salutation: 'Frau'))).to be(false)
end
end
+
+ it 'counts an additional detail as a detail' do
+ ActsAsTenant.with_tenant(create(:organisation)) do
+ person = Person.new(name: 'Müller', additional_details: [{ 'kind' => 'text', 'label' => '', 'value' => 'x' }])
+
+ expect(helper.person_bare?(person)).to be(false)
+ end
+ end
+ end
+
+ # The lane lines of the additional details, after the phone, in stored
+ # order: the kind decides the link and the format.
+ describe '#person_fields with additional details' do
+ before { ActsAsTenant.current_tenant = create(:organisation) }
+ after { ActsAsTenant.current_tenant = nil }
+
+ def fields_for(*details, phone: '+49 30 1234567')
+ person = Person.new(name: 'Müller', phone: phone, additional_details: details)
+ helper.person_fields(person, nil)
+ end
+
+ def detail(kind, value, label: '')
+ { 'kind' => kind, 'label' => label, 'value' => value }
+ end
+
+ it 'puts them after the phone, in their order, labelled' do
+ fields = fields_for(detail('phone', '+49 30 1234568', label: 'Fax'), detail('text', 'nur vormittags', label: 'Erreichbar'))
+
+ expect(fields.pluck(:label)).to eq([Person.human_attribute_name(:phone), 'Fax', 'Erreichbar'])
+ end
+
+ it 'links a phone through tel:, an email through mailto:, a url to a new tab' do
+ phone, email, url = fields_for(detail('phone', '+49 (170) 123-4567', label: 'Mobil'),
+ detail('email', '[email protected]'),
+ detail('url', 'https://example.com'), phone: nil)
+
+ expect(phone).to include(href: 'tel:+491701234567', mono: true, value: '+49 (170) 123-4567', action: 'Mobil')
+ expect(email).to include(href: 'mailto:[email protected]', value: '[email protected]')
+ expect(url).to include(href: 'https://example.com', external: true)
+ end
+
+ it 'names the kind when the label is empty' do
+ fields = I18n.with_locale(:de) { fields_for(detail('phone', '+49 30 1'), phone: nil) }
+
+ expect(fields.first[:label]).to eq('Telefon')
+ end
+
+ it 'formats a date in the reader\'s language' do
+ german = I18n.with_locale(:de) { fields_for(detail('date', '1980-02-29'), phone: nil) }.first
+ english = I18n.with_locale(:en) { fields_for(detail('date', '1980-02-29'), phone: nil) }.first
+
+ expect(german[:value]).to eq(I18n.l(Date.new(1980, 2, 29), format: :long, locale: :de))
+ expect(english[:value]).to eq(I18n.l(Date.new(1980, 2, 29), format: :long, locale: :en))
+ expect(german[:value]).to include('Februar')
+ end
+
+ it 'keeps the lines of an address and escapes them' do
+ value = fields_for(detail('address', "<b>Haupt</b>straße 5\n12345 Köln"), phone: nil).first[:value]
+
+ expect(value).to eq('&lt;b&gt;Haupt&lt;/b&gt;straße 5<br>12345 Köln')
+ end
+
+ it 'never links a url written around the validation' do
+ field = fields_for(detail('url', 'javascript:alert(1)'), phone: nil).first
+
+ expect(field[:href]).to be_nil
+ expect(field[:value]).to eq('javascript:alert(1)')
+ end
+
+ it 'shows no line for a person without details' do
+ expect(fields_for(phone: nil)).to eq([])
+ end
end
# Review round 1: the date of the last meeting names its year when it is
diff --git a/spec/models/concerns/person_trackable_spec.rb b/spec/models/concerns/person_trackable_spec.rb
index c82300d..441e6ef 100644
--- a/spec/models/concerns/person_trackable_spec.rb
+++ b/spec/models/concerns/person_trackable_spec.rb
@@ -54,6 +54,25 @@ RSpec.describe PersonTrackable, type: :model do
expect(serialized([activity])).not_to include('+49 40 1111111', 'Erika Neu')
end
+ it 'records changed additional details by name only, never their values' do
+ person = create_person
+ person.update!(additional_details: [{ 'kind' => 'phone', 'label' => 'Fax geheim', 'value' => '+49 30 5550000' }])
+
+ activity = Activity.find_by!(trackable: person, action: 'updated')
+ expect(activity.attribute_changes).to eq('additional_details' => nil)
+ expect(serialized([activity])).not_to include('Fax geheim', '+49 30 5550000')
+ end
+
+ it 'records nothing when the same details are saved again' do
+ person = create_person
+ details = [{ 'kind' => 'text', 'label' => 'A', 'value' => 'b' }]
+ person.update!(additional_details: details)
+
+ updates = -> { Activity.where(trackable: person, action: 'updated').count }
+
+ expect { person.update!(additional_details: details.map(&:dup)) }.not_to(change(&updates))
+ end
+
it 'does not record bookkeeping columns as changes' do
person = create_person
person.update!(phone: '1')
diff --git a/spec/models/person_detail_spec.rb b/spec/models/person_detail_spec.rb
new file mode 100644
index 0000000..aad533b
--- /dev/null
+++ b/spec/models/person_detail_spec.rb
@@ -0,0 +1,228 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# The one owner of the rule for the additional details of a person: kinds,
+# limits, normalisation and validation.
+RSpec.describe PersonDetail do
+ def detail(kind: 'text', label: 'Notiz', value: 'x')
+ { 'kind' => kind, 'label' => label, 'value' => value }
+ end
+
+ def problems(*entries)
+ described_class.problems(entries)
+ end
+
+ describe '.normalize' do
+ it 'keeps only kind, label and value, as strings' do
+ raw = [{ 'kind' => 'phone', 'label' => 'Fax', 'value' => '+49 30 1234568', 'evil' => 'x', 'id' => 1 }]
+
+ expect(described_class.normalize(raw)).to eq([detail(kind: 'phone', label: 'Fax', value: '+49 30 1234568')])
+ end
+
+ it 'takes symbol keys too' do
+ expect(described_class.normalize([{ kind: 'text', label: 'A', value: 'b' }])).to eq([detail(label: 'A', value: 'b')])
+ end
+
+ it 'squeezes label and value' do
+ raw = [detail(label: ' Büro Zentrale ', value: " +49 30\t 1234567 ")]
+
+ expect(described_class.normalize(raw).first).to include('label' => 'Büro Zentrale', 'value' => '+49 30 1234567')
+ end
+
+ it 'drops a row without a value (an empty row of the form is no detail)' do
+ raw = [detail(value: ' '), detail(value: nil), detail(kind: 'phone', label: 'Fax', value: '1')]
+
+ expect(described_class.normalize(raw)).to eq([detail(kind: 'phone', label: 'Fax', value: '1')])
+ end
+
+ it 'keeps an empty label as an empty string' do
+ expect(described_class.normalize([detail(label: nil)]).first['label']).to eq('')
+ end
+
+ it 'keeps the lines of an address, trimmed, without empty ones' do
+ raw = [detail(kind: 'address', value: " Musterstraße 1 \r\n\r\n 12345 Berlin \n ")]
+
+ expect(described_class.normalize(raw).first['value']).to eq("Musterstraße 1\n12345 Berlin")
+ end
+
+ it 'joins the lines of any other kind' do
+ expect(described_class.normalize([detail(value: "eins\nzwei")]).first['value']).to eq('eins zwei')
+ end
+
+ it 'keeps an unknown kind for the validation' do
+ expect(described_class.normalize([detail(kind: 'fax')]).first['kind']).to eq('fax')
+ end
+
+ it 'orders an indexed hash (the form) by its index, not by the order of the keys' do
+ raw = { '10' => detail(value: 'zehn'), '2' => detail(value: 'zwei'), '0' => detail(value: 'null') }
+
+ expect(described_class.normalize(raw).pluck('value')).to eq(%w[null zwei zehn])
+ end
+
+ it 'reads ActionController::Parameters' do
+ raw = ActionController::Parameters.new('0' => { 'kind' => 'url', 'label' => 'Web', 'value' => 'https://example.com' })
+
+ expect(described_class.normalize(raw)).to eq([detail(kind: 'url', label: 'Web', value: 'https://example.com')])
+ end
+
+ it 'is empty for nothing and for the marker of an empty list' do
+ expect(described_class.normalize(nil)).to eq([])
+ expect(described_class.normalize('')).to eq([])
+ expect(described_class.normalize({ described_class::FORM_MARKER => '1' })).to eq([])
+ end
+
+ it 'ignores junk from a hand-made request and never raises' do
+ raw = {
+ '0' => 'a string', '1' => %w[nested array], '2' => { 'kind' => %w[x], 'value' => { 'a' => 'b' } },
+ 'abc' => detail(value: 'not numeric'), '-1' => detail(value: 'negative'), '3' => detail(value: 'kept'),
+ '4' => { 'kind' => 'text', 'label' => { 'x' => 'y' }, 'value' => 'label dropped' }
+ }
+
+ expect(described_class.normalize(raw)).to eq([detail(value: 'kept'), detail(label: '', value: 'label dropped')])
+ expect(described_class.normalize([nil, 1, 'x', [detail]])).to eq([])
+ expect(described_class.normalize(42)).to eq([])
+ end
+
+ it 'is idempotent' do
+ once = described_class.normalize([detail(kind: 'address', label: ' A ', value: " x \n y ")])
+
+ expect(described_class.normalize(once)).to eq(once)
+ end
+ end
+
+ describe '.problems' do
+ it 'accepts a valid detail of every kind' do
+ valid = {
+ 'phone' => '+49 30 1234567', 'email' => '[email protected]', 'url' => 'https://example.com/team',
+ 'date' => '1980-02-29', 'address' => "Musterstraße 1\n12345 Berlin", 'text' => 'nur vormittags'
+ }
+ expect(valid.keys).to match_array(described_class::KINDS)
+
+ valid.each { |kind, value| expect(problems(detail(kind: kind, value: value))).to eq([]), kind }
+ end
+
+ it 'accepts http as well as https' do
+ expect(problems(detail(kind: 'url', value: 'http://example.com'))).to eq([])
+ expect(problems(detail(kind: 'url', value: 'HTTPS://EXAMPLE.COM'))).to eq([])
+ end
+
+ {
+ 'phone' => ['keine Nummer', :invalid_phone],
+ 'email' => ['erika at example', :invalid_email],
+ 'date' => ['28.10.2026', :invalid_date],
+ 'url' => ['example.com', :invalid_url]
+ }.each do |kind, (value, problem)|
+ it "refuses an invalid #{kind}" do
+ expect(problems(detail(kind: kind, value: value))).to eq([[problem, { position: 1 }]])
+ end
+ end
+
+ it 'refuses an impossible date and the other ISO forms (week, ordinal, compact)' do
+ %w[2026-02-30 2026-W44-3 2026-301 20261028 2026-10-28T10:00].each do |value|
+ expect(problems(detail(kind: 'date', value: value))).to eq([[:invalid_date, { position: 1 }]]), value
+ end
+ end
+
+ it 'refuses every scheme but http and https, and a url without a host' do
+ ['javascript:alert(1)', 'JavaScript:alert(1)', 'data:text/html,<b>x</b>', 'ftp://example.com',
+ 'mailto:[email protected]', 'https://', '//example.com', 'https://exa mple.com',
+ 'http://example.com/<script>'].each do |value|
+ expect(problems(detail(kind: 'url', value: value))).to eq([[:invalid_url, { position: 1 }]]), value
+ end
+ end
+
+ it 'refuses an unknown kind' do
+ expect(problems(detail(kind: 'fax'))).to eq([[:unknown_kind, { position: 1 }]])
+ expect(problems(detail(kind: nil))).to eq([[:unknown_kind, { position: 1 }]])
+ end
+
+ it 'refuses a detail without a value (written around the normalisation)' do
+ expect(problems(detail(value: ''))).to eq([[:value_missing, { position: 1 }]])
+ end
+
+ it 'refuses an entry that is no hash' do
+ expect(problems('x')).to eq([[:unknown_kind, { position: 1 }], [:value_missing, { position: 1 }]])
+ end
+
+ it 'checks the label length at n-1, n and n+1' do
+ max = described_class::MAX_LABEL_LENGTH
+
+ expect(problems(detail(label: 'a' * (max - 1)))).to eq([])
+ expect(problems(detail(label: 'a' * max))).to eq([])
+ expect(problems(detail(label: 'a' * (max + 1)))).to eq([[:label_too_long, { position: 1, count: max }]])
+ end
+
+ it 'checks the value length at n-1, n and n+1' do
+ max = described_class::MAX_VALUE_LENGTH
+
+ expect(problems(detail(value: 'a' * (max - 1)))).to eq([])
+ expect(problems(detail(value: 'a' * max))).to eq([])
+ expect(problems(detail(value: 'a' * (max + 1)))).to eq([[:value_too_long, { position: 1, count: max }]])
+ end
+
+ it 'gives an address more room' do
+ max = described_class::MAX_ADDRESS_LENGTH
+ expect(max).to be > described_class::MAX_VALUE_LENGTH
+
+ expect(problems(detail(kind: 'address', value: 'a' * max))).to eq([])
+ expect(problems(detail(kind: 'address', value: 'a' * (max + 1))))
+ .to eq([[:value_too_long, { position: 1, count: max }]])
+ end
+
+ it 'names the position of the entry' do
+ list = [detail, detail(kind: 'email', value: 'nope'), detail]
+
+ expect(described_class.problems(list)).to eq([[:invalid_email, { position: 2 }]])
+ end
+
+ it 'allows ten details and refuses eleven with one problem' do
+ expect(described_class.problems(Array.new(described_class::MAX) { detail })).to eq([])
+ expect(described_class.problems(Array.new(described_class::MAX + 1) { detail }))
+ .to eq([[:too_many, { count: described_class::MAX }]])
+ end
+
+ it 'refuses a stored value that is no list' do
+ expect(described_class.problems({ 'kind' => 'text' })).to eq([[:not_a_list, {}]])
+ end
+ end
+
+ describe 'a stored detail' do
+ it 'links only an http(s) url, also when it was written around the validation' do
+ expect(described_class.new(detail(kind: 'url', value: 'https://example.com')).href).to eq('https://example.com')
+ expect(described_class.new(detail(kind: 'url', value: 'javascript:alert(1)')).href).to be_nil
+ end
+
+ it 'links a phone through its digits and an email as mailto' do
+ expect(described_class.new(detail(kind: 'phone', value: '+49 (30) 123-4567')).href).to eq('tel:+49301234567')
+ expect(described_class.new(detail(kind: 'email', value: '[email protected]')).href).to eq('mailto:[email protected]')
+ end
+
+ it 'links neither a malformed email nor a phone without digits' do
+ expect(described_class.new(detail(kind: 'email', value: 'a b')).href).to be_nil
+ expect(described_class.new(detail(kind: 'phone', value: 'abc')).href).to be_nil
+ end
+
+ it 'reads a date, and nil for a malformed one' do
+ expect(described_class.new(detail(kind: 'date', value: '1980-02-29')).date).to eq(Date.new(1980, 2, 29))
+ expect(described_class.new(detail(kind: 'date', value: 'gestern')).date).to be_nil
+ end
+
+ it 'splits an address into its lines' do
+ expect(described_class.new(detail(kind: 'address', value: "A\nB")).lines).to eq(%w[A B])
+ end
+
+ it 'reads junk without raising' do
+ stored = described_class.new('x')
+
+ expect([stored.kind, stored.label, stored.value, stored.href]).to eq([nil, '', '', nil])
+ end
+
+ it 'gives the search its value only, not its label' do
+ list = [detail(label: 'Fax', value: '+49 30 1234568'), detail(kind: 'address', label: 'Büro', value: "A\nB")]
+
+ expect(described_class.search_values(list)).to eq(['+49 30 1234568', "A\nB"])
+ expect(described_class.search_values(nil)).to eq([])
+ end
+ end
+end
diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb
index 7e7112c..ae2243b 100644
--- a/spec/models/person_spec.rb
+++ b/spec/models/person_spec.rb
@@ -61,6 +61,67 @@ RSpec.describe Person, type: :model do
end
end
+ describe 'the additional details' do
+ def detail(kind: 'phone', label: 'Fax', value: '+49 30 1234568')
+ { 'kind' => kind, 'label' => label, 'value' => value }
+ end
+
+ it 'starts with none' do
+ expect(create(:person, organisation: organisation).additional_details).to eq([])
+ end
+
+ it 'normalizes them through PersonDetail before the validation' do
+ person = create(:person, organisation: organisation,
+ additional_details: { '1' => detail(value: ' +49 30 2 '), '0' => detail(label: ' Mobil ', value: '') })
+
+ expect(person.reload.additional_details).to eq([detail(value: '+49 30 2')])
+ end
+
+ it 'allows ten and refuses eleven' do
+ expect(build(:person, organisation: organisation, additional_details: Array.new(PersonDetail::MAX) { detail })).to be_valid
+
+ person = build(:person, organisation: organisation, additional_details: Array.new(PersonDetail::MAX + 1) { detail })
+ expect(person).not_to be_valid
+ expect(person.errors.details[:additional_details]).to eq([{ error: :too_many, count: PersonDetail::MAX }])
+ I18n.with_locale(:de) { expect(person.errors.full_messages).to eq(['Weitere Angaben (höchstens 10 erlaubt)']) }
+ end
+
+ it 'names the position of an invalid detail, in German and English' do
+ person = build(:person, organisation: organisation,
+ additional_details: [detail, detail(kind: 'email', label: 'Privat', value: 'nope')])
+
+ expect(person).not_to be_valid
+ expect(person.errors.details[:additional_details]).to eq([{ error: :invalid_email, position: 2 }])
+ I18n.with_locale(:de) { expect(person.errors.full_messages).to eq(['Weitere Angaben (Nr. 2): keine gültige E-Mail-Adresse']) }
+ I18n.with_locale(:en) { expect(person.errors.full_messages).to eq(['Additional details (no. 2): not a valid email address']) }
+ end
+
+ it 'has a message for every problem PersonDetail can name, in both languages' do
+ problems = %i[not_a_list too_many unknown_kind value_missing label_too_long value_too_long] +
+ PersonDetail::KINDS.map { |kind| :"invalid_#{kind}" } - %i[invalid_address invalid_text]
+ scope = 'activerecord.errors.models.person.attributes.additional_details'
+
+ %i[de en].each do |locale|
+ problems.each do |problem|
+ expect(I18n.exists?("#{scope}.#{problem}", locale)).to be(true), "#{locale}: #{problem}"
+ end
+ end
+ end
+
+ # Defense in depth: the check constraint holds where the validation is
+ # skipped (update_column, a console, a later API).
+ it 'refuses eleven details and a non-array in the database' do
+ person = create(:person, organisation: organisation)
+ # A savepoint per write: a refused statement aborts the test transaction.
+ write = ->(value) { Person.transaction(requires_new: true) { person.update_column(:additional_details, value) } }
+
+ expect { write.call(Array.new(PersonDetail::MAX + 1) { detail }) }
+ .to raise_error(ActiveRecord::StatementInvalid, /people_additional_details_shape/)
+ expect { write.call(detail) }.to raise_error(ActiveRecord::StatementInvalid, /people_additional_details_shape/)
+ expect { write.call(Array.new(PersonDetail::MAX) { detail }) }.not_to raise_error
+ end
+ end
+
describe 'the member link' do
it 'accepts an active member of the same organisation' do
expect(build(:person, organisation: organisation, user: member_of(organisation))).to be_valid
diff --git a/spec/requests/organisations/people_spec.rb b/spec/requests/organisations/people_spec.rb
index 6eb6b26..7ffb774 100644
--- a/spec/requests/organisations/people_spec.rb
+++ b/spec/requests/organisations/people_spec.rb
@@ -331,6 +331,163 @@ RSpec.describe 'Organisations::People', :ci_guard, type: :request do
end
end
+ # Up to ten labelled details (PersonDetail). The form sends them as an
+ # indexed hash next to a marker; the lane shows them after the phone.
+ describe 'the additional details' do
+ before { sign_in manager }
+
+ def row(kind, value, label: '')
+ { kind: kind, label: label, value: value }
+ end
+
+ def details_params(*rows)
+ rows.each_with_index.to_h { |entry, index| [index.to_s, entry] }.merge(PersonDetail::FORM_MARKER => '1')
+ end
+
+ def patch_details(target, details, **extra)
+ patch organisation_person_path(organisation, target),
+ params: { person: { additional_details: details, lock_version: target.reload.lock_version, **extra } }
+ end
+
+ def lane_html(target = person)
+ get organisation_person_path(organisation, target), headers: frame_headers
+ expect(response).to have_http_status(:ok)
+ Nokogiri::HTML(response.body)
+ end
+
+ it 'saves them from the form and shows each kind in the lane' do
+ patch_details(person, details_params(row('phone', '+49 30 1234568', label: 'Fax'),
+ row('email', '[email protected]', label: 'Privat'),
+ row('url', 'https://example.com', label: 'Website'),
+ row('date', '1980-02-29', label: 'Geburtstag'),
+ row('address', "Musterstraße 1\r\n12345 Berlin", label: 'Büro'),
+ row('text', 'nur vormittags', label: 'Erreichbar')))
+ expect(response).to redirect_to(organisation_people_path(organisation, person_id: person.id))
+ expect(person.reload.additional_details.pluck('kind')).to eq(%w[phone email url date address text])
+
+ lane = lane_html
+ expect(lane.at_css('a[href="tel:+49301234568"]').text).to include('+49 30 1234568')
+ expect(lane.at_css('a[href="mailto:[email protected]"]')).to be_present
+ web = lane.at_css('a[href="https://example.com"]')
+ expect([web['target'], web['rel']]).to eq(['_blank', 'noopener noreferrer'])
+ expect(lane.text).to include('Fax', 'Geburtstag', I18n.l(Date.new(1980, 2, 29), format: :long), 'nur vormittags')
+ expect(lane.to_html).to include('Musterstraße 1<br>12345 Berlin')
+ end
+
+ it 'formats the date in the reader\'s language' do
+ person.update!(additional_details: [row('date', '1980-02-29', label: 'Geburtstag')])
+ manager.update!(language_preference: 'en')
+
+ text = lane_html.text
+
+ expect(text).to include(I18n.l(Date.new(1980, 2, 29), format: :long, locale: :en))
+ end
+
+ it 'shows the details as tap targets on a phone, named by their label' do
+ person.update!(additional_details: [row('phone', '+49 170 1234567', label: 'Mobil')])
+
+ get organisation_people_path(organisation, person_id: person.id, viewport: 'mobile')
+
+ tap = Nokogiri::HTML(response.body).at_css('a.people-tap[href="tel:+491701234567"]')
+ expect(tap).to be_present
+ expect(tap.text).to include('+49 170 1234567', 'Mobil')
+ end
+
+ it 'shows no detail line for a person without details' do
+ fields = lane_html.css('.people-fields__row dt').map { |dt| dt.text.strip }
+
+ expect(fields).to eq(%i[function company email phone].map { |field| Person.human_attribute_name(field) })
+ end
+
+ it 'clears them when only the marker is sent (the last one removed)' do
+ person.update!(additional_details: [row('text', 'weg')])
+
+ patch_details(person, { PersonDetail::FORM_MARKER => '1' })
+
+ expect(response).to have_http_status(:see_other)
+ expect(person.reload.additional_details).to eq([])
+ end
+
+ it 'leaves them alone when a save does not name them' do
+ person.update!(additional_details: [row('text', 'bleibt')])
+
+ patch organisation_person_path(organisation, person),
+ params: { person: { function: 'IT', lock_version: person.lock_version } }
+
+ expect(person.reload.function).to eq('IT')
+ expect(person.additional_details).to eq([{ 'kind' => 'text', 'label' => '', 'value' => 'bleibt' }])
+ end
+
+ it 'refuses a javascript: url with the form and its reason, and saves nothing' do
+ patch_details(person, details_params(row('url', 'javascript:alert(1)', label: 'Web')), phone: '+49 30 0')
+
+ expect(response).to have_http_status(:unprocessable_content)
+ reason = I18n.t('activerecord.errors.models.person.attributes.additional_details.invalid_url', position: 1)
+ expect(response.body).to include('id="person_name"', CGI.escapeHTML(reason))
+ expect(person.reload.additional_details).to eq([])
+ expect(person.phone).to eq('+49 30 1234567')
+ end
+
+ it 'refuses an eleventh detail' do
+ patch_details(person, details_params(*Array.new(PersonDetail::MAX + 1) { row('text', 'x') }))
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(person.reload.additional_details).to eq([])
+ end
+
+ it 'escapes a script in a label or a value' do
+ person.update!(additional_details: [row('text', '<script>alert(1)</script>', label: '<script>alert(2)</script>'),
+ row('address', "<script>alert(3)</script>\nZeile")])
+
+ body = lane_html.to_html
+
+ expect(body).not_to include('<script>alert(')
+ expect(body).to include('&lt;script&gt;alert(1)&lt;/script&gt;', '&lt;script&gt;alert(2)&lt;/script&gt;',
+ '&lt;script&gt;alert(3)&lt;/script&gt;<br>Zeile')
+ end
+
+ it 'never answers a hand-made shape with a 500, and keeps the stored details' do
+ person.update!(additional_details: [row('text', 'bleibt')])
+ shapes = [
+ 'a string', %w[an array], { '0' => 'not a hash' }, { '0' => { 'kind' => { 'x' => 'y' }, 'value' => ['v'] } },
+ { 'abc' => row('text', 'not numeric') }, { '0' => { 'kind' => 'text', 'value' => 'ok', 'evil' => 'x' } }
+ ]
+
+ # One request per shape; the loop is not an N+1 of the page.
+ Prosopite.pause do
+ shapes.each do |shape|
+ patch organisation_person_path(organisation, person),
+ params: { person: { additional_details: shape, lock_version: person.reload.lock_version } }
+ expect(response.status).to be < 500, shape.inspect
+ end
+ end
+ expect(person.reload.additional_details).to eq([{ 'kind' => 'text', 'label' => '', 'value' => 'ok' }])
+ end
+
+ it 'writes "Weitere Angaben" into the history, without values' do
+ patch_details(person, details_params(row('phone', '+49 30 5550000', label: 'Fax geheim')))
+
+ I18n.with_locale(:de) do
+ history = lane_html.text
+
+ expect(history).to include('Weitere Angaben')
+ expect(history).not_to include('Fax geheim geändert')
+ end
+ expect(Activity.where(trackable: person).to_a.to_json).not_to include('5550000')
+ end
+
+ it 'answers 404 for the details of a person of another organisation and changes nothing' do
+ foreign = ActsAsTenant.with_tenant(other_organisation) { create(:person, organisation: other_organisation) }
+ sign_in admin
+
+ patch organisation_person_path(organisation, foreign),
+ params: { person: { additional_details: details_params(row('text', 'fremd')) } }
+
+ expect(response).to have_http_status(:not_found)
+ expect(Person.unscoped.find(foreign.id).additional_details).to eq([])
+ end
+ end
+
# Review round 1, finding 1: the form only offered ACTIVE members, so the row
# of a deactivated member showed "Kein Mitglied" selected, and any save (only
# the phone changed) dropped the link without anybody noticing.
diff --git a/spec/services/actors/save_person_spec.rb b/spec/services/actors/save_person_spec.rb
index 3d750a5..eb7df6e 100644
--- a/spec/services/actors/save_person_spec.rb
+++ b/spec/services/actors/save_person_spec.rb
@@ -204,4 +204,76 @@ RSpec.describe Actors::SavePerson do
expect(result).not_to be_success
expect(result.code).to eq(:validation_failed)
end
+
+ describe 'the additional details' do
+ def row(kind: 'phone', label: 'Fax', value: '+49 30 1234568')
+ { 'kind' => kind, 'label' => label, 'value' => value }
+ end
+
+ # The form sends the rows as an indexed hash next to the marker.
+ def form(*rows, marker: true)
+ list = rows.each_with_index.to_h { |entry, index| [index.to_s, entry] }
+ marker ? list.merge(PersonDetail::FORM_MARKER => '1') : list
+ end
+
+ let(:person) { save(name: 'Doris Dienstleister', additional_details: form(row, row(label: 'Mobil', value: '+49 170 1234567'))).resource }
+
+ it 'saves them in the order of the index' do
+ expect(person.additional_details).to eq([row, row(label: 'Mobil', value: '+49 170 1234567')])
+
+ result = save(additional_details: { '7' => row(label: 'Zweite'), '3' => row(label: 'Erste'), 'sent' => '1' },
+ lock_version: person.lock_version, id: person.id)
+
+ expect(result).to be_success
+ expect(person.reload.additional_details.pluck('label')).to eq(%w[Erste Zweite])
+ end
+
+ it 'clears them when the form sends only the marker (the last one removed)' do
+ save(additional_details: form, lock_version: person.lock_version, id: person.id)
+
+ expect(person.reload.additional_details).to eq([])
+ end
+
+ it 'leaves them alone when the request does not name them' do
+ save(phone: '+49 30 7654321', lock_version: person.lock_version, id: person.id)
+
+ expect(person.reload.additional_details.size).to eq(2)
+ expect(person.phone).to eq('+49 30 7654321')
+ end
+
+ it 'refuses an eleventh and keeps the stored ones' do
+ result = save(additional_details: form(*Array.new(PersonDetail::MAX + 1) { row }),
+ lock_version: person.lock_version, id: person.id)
+
+ expect(result.code).to eq(:validation_failed)
+ expect(person.reload.additional_details.size).to eq(2)
+ end
+
+ it 'refuses a javascript: url and stores nothing' do
+ result = save(additional_details: form(row(kind: 'url', label: 'Web', value: 'javascript:alert(1)')),
+ lock_version: person.lock_version, id: person.id)
+
+ expect(result.code).to eq(:validation_failed)
+ expect(result.errors.join).to include('(Nr. 1)').or include('(no. 1)')
+ expect(person.reload.additional_details.size).to eq(2)
+ end
+
+ it 'refuses a stale save of the details with the stale code' do
+ stale_version = person.lock_version
+ save(phone: '1', lock_version: stale_version, id: person.id)
+
+ result = save(additional_details: form, lock_version: stale_version, id: person.id)
+
+ expect(result.code).to eq(:stale_object)
+ expect(person.reload.additional_details.size).to eq(2)
+ end
+
+ it 'takes details for the row of an active member' do
+ user = member
+ result = save(name: 'egal', user_id: user.id, additional_details: form(row))
+
+ expect(result).to be_success
+ expect(result.resource.additional_details).to eq([row])
+ end
+ end
end
diff --git a/spec/services/people/directory_spec.rb b/spec/services/people/directory_spec.rb
index f9f3cae..5c0a873 100644
--- a/spec/services/people/directory_spec.rb
+++ b/spec/services/people/directory_spec.rb
@@ -129,6 +129,39 @@ RSpec.describe People::Directory, :ci_guard do
expect(found.candidates.map(&:person)).to eq([frau])
end
+ # D3: the values of the additional details are searched, never their labels
+ # ("Fax" would find everybody with a fax).
+ describe 'the additional details' do
+ let!(:with_details) do
+ create(:person, organisation: organisation, name: 'Gerd Gast', company: nil,
+ additional_details: [{ 'kind' => 'phone', 'label' => 'Fax', 'value' => '+49 30 9876543' },
+ { 'kind' => 'address', 'label' => 'Büro', 'value' => "Hauptstraße 5\n12345 Köln" }])
+ end
+
+ def found(query)
+ described_class.new(viewer: admin, organisation: organisation, query: query)
+ end
+
+ it 'finds a person by a value, also umlaut-normalized' do
+ expect(found('9876543').entries.map(&:person)).to eq([with_details])
+ expect(found('hauptstrasse').entries.map(&:person)).to eq([with_details])
+ expect(found('Koeln').candidates.map(&:person)).to eq([with_details])
+ end
+
+ it 'does not find a person by a label alone' do
+ expect(found('Fax').entries).to be_empty
+ expect(found('Büro').entries).to be_empty
+ expect(found('Gast').entries.map(&:person)).to eq([with_details])
+ end
+
+ it 'finds the details of a member row' do
+ create(:person, organisation: organisation, user: colleague, name: 'Carl Kollege',
+ additional_details: [{ 'kind' => 'url', 'label' => 'Web', 'value' => 'https://kollege.example.com' }])
+
+ expect(found('kollege.example').entries.map(&:user)).to eq([colleague])
+ end
+ end
+
# External review of 26.10.2026: a linked member is compared by the name
# the directory shows, not by the copy taken when the row was linked.
it 'finds duplicates of a member by the current account name' do
diff --git a/spec/services/user_data_exports/serializers_spec.rb b/spec/services/user_data_exports/serializers_spec.rb
index 932c5e5..a1ab1e1 100644
--- a/spec/services/user_data_exports/serializers_spec.rb
+++ b/spec/services/user_data_exports/serializers_spec.rb
@@ -131,4 +131,21 @@ RSpec.describe 'UserDataExports::Serializers' do
expect(row['legacy_role']).to eq('legacy-admin')
end
end
+
+ # The person row linked to the account: the additional details are part of
+ # it, as stored (kind, label, value).
+ describe 'the people source' do
+ it 'exports the additional details of the own row' do
+ details = [{ 'kind' => 'phone', 'label' => 'Mobil', 'value' => '+49 170 1234567' }]
+ ActsAsTenant.with_tenant(organisation) do
+ create(:membership, user: user, organisation: organisation, active: true)
+ create(:person, organisation: organisation, user: user, name: 'Erika Beispiel', additional_details: details)
+
+ source = UserDataExports::Registry::SOURCES.find { |entry| entry.key == :people }
+ row = UserDataExports::GenericSerializer.call(source, user).first
+
+ expect(row['additional_details']).to eq(details)
+ end
+ end
+ end
end