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.

fix(people): address review round 2 of the people directory

Guido Boyke62136d1afb3b
diff --git a/app/components/neo/buttons/icon_button_component.html.erb b/app/components/neo/buttons/icon_button_component.html.erb
index 42bb1ed..939bcdc 100644
--- a/app/components/neo/buttons/icon_button_component.html.erb
+++ b/app/components/neo/buttons/icon_button_component.html.erb
@@ -4,7 +4,7 @@
params: @form[:params],
class: component_classes,
form: { data: { turbo_confirm: @confirm }.compact.merge(@form[:data] || {}) },
- **component_attributes.except(:href, :type) do %>
+ **form_button_attributes do %>
<%= render_spinner(visible: @loading) %>
<%= render_icon %>
<% end %>
diff --git a/app/controllers/organisations/people_controller.rb b/app/controllers/organisations/people_controller.rb
index d8e3b0c..3f027d5 100644
--- a/app/controllers/organisations/people_controller.rb
+++ b/app/controllers/organisations/people_controller.rb
@@ -133,9 +133,20 @@ module Organisations
end
# A member row without a person row opens the form with the member chosen
- # (?user_id=); SavePerson checks the choice again.
+ # (?user_id=). Only a member the reader may link: an active member visible
+ # on the members page without a row yet. Anything else is no choice, so the
+ # select never names an account the reader may not see. SavePerson checks
+ # the choice again on save.
def new_person
- current_organisation.people.build(user_id: params[:user_id].presence)
+ current_organisation.people.build(user_id: linkable_user_id(params[:user_id]))
+ end
+
+ def linkable_user_id(value)
+ return unless value.is_a?(String) && value.present?
+
+ visible = policy_scope(Membership).where(organisation: current_organisation, active: true, user_id: value).exists?
+ taken = Person.where(organisation: current_organisation, user_id: value).exists?
+ value if visible && !taken
end
def new_person_requested?
diff --git a/app/helpers/people_helper.rb b/app/helpers/people_helper.rb
index abb2c12..cf57719 100644
--- a/app/helpers/people_helper.rb
+++ b/app/helpers/people_helper.rb
@@ -31,7 +31,7 @@ module PeopleHelper
index_page_selected_row_selector_value: '.selected-row[data-person-id]',
index_page_skip_if_content_visible_value: 'true',
index_url: organisation_people_path(current_organisation),
- lane_params: lane_params_json(selection: %w[person_id], form: %w[new])
+ lane_params: lane_params_json(selection: %w[person_id], form: %w[new user_id])
}.compact
end
@@ -44,16 +44,28 @@ module PeopleHelper
unresolved['person_id'] = person_id if person_id.is_a?(String) && person_id.present? && selected.nil?
new_state = params[:new]
unresolved['new'] = new_state if new_state.is_a?(String) && new_state.present? && !form_open
+ user_id = params[:user_id]
+ unresolved['user_id'] = user_id if user_id.is_a?(String) && user_id.present? && !form_open
unresolved
end
- # The name a row or the lane shows: a member through the viewer's name
- # display setting, a contact as written in the row.
+ # The name a row or the lane shows: a member (an ACTIVE member, passed as
+ # user) through the viewer's name display setting, a contact as written in
+ # the row. A row of a former member is a contact: its own name, never the
+ # account's current one (search and order read the row too).
def person_display_name(person: nil, user: nil)
- user ||= person&.user
user ? viewer_display_name(user) : person.name
end
+ # The linked account of a row while it is an active member of this
+ # organisation, else nil (the row is then shown as a contact).
+ def person_member_user(person)
+ return unless person.user_id
+
+ active = Membership.where(organisation: current_organisation, user_id: person.user_id, active: true).exists?
+ person.user if active
+ end
+
# Initials for a contact's avatar (no account, no picture).
def person_initials(name)
name.to_s.split.first(2).map { |part| part[0] }.join.upcase
@@ -80,7 +92,8 @@ module PeopleHelper
taken = Person.where(organisation: current_organisation).linked.where.not(id: person.id).select(:user_id)
member_ids = policy_scope(Membership).where(organisation: current_organisation, active: true).select(:user_id)
members = User.where(id: member_ids).where.not(id: taken).to_a
- members |= [person.user] if person.user
+ stored = User.find_by(id: person.user_id_in_database) if person.persisted? && person.user_id_in_database
+ members |= [stored] if stored
members.sort_by { |user| viewer_display_name(user).downcase }
end
end
diff --git a/app/services/actors/save_person.rb b/app/services/actors/save_person.rb
index 147b959..04cbe76 100644
--- a/app/services/actors/save_person.rb
+++ b/app/services/actors/save_person.rb
@@ -34,6 +34,7 @@ module Actors
def perform
raise ActorError.new(I18n.t('controllers.people.not_found'), code: :not_found) if @person.nil?
+ check_lock_version
@person.assign_attributes(@params.slice(*ATTRIBUTES))
assign_link if @params.key?(:user_id)
@person.creator ||= @current_user if @person.new_record?
@@ -45,6 +46,15 @@ module Actors
attr_reader :current_user
+ # A lock_version that is not a number (a hand-made request) cannot say what
+ # the writer saw; Rails would raise on a nil one. Treated as stale.
+ def check_lock_version
+ return unless @params.key?(:lock_version)
+ return if Integer(@params[:lock_version].to_s, exception: false)
+
+ raise ActorError.new(I18n.t('controllers.people.update.stale'), code: :stale_object)
+ end
+
# Two saves linking the same member at once: the validation passes for
# both, the unique index refuses the second. A failure, not a crash.
def save_link_safely
diff --git a/app/views/organisations/people/_detail.html.erb b/app/views/organisations/people/_detail.html.erb
index d739022..dad56b9 100644
--- a/app/views/organisations/people/_detail.html.erb
+++ b/app/views/organisations/people/_detail.html.erb
@@ -3,18 +3,21 @@
history, and rows that may be the same person (read only). Edit and
delete only for those who may write (PersonPolicy). %>
<% fields = %i[function department company email phone] %>
+<% member = person_member_user(person) %>
<div class="people-detail space-y-6">
<div class="flex flex-wrap items-start gap-3">
- <% if person.user %>
- <%= render Neo::Users::AvatarComponent.new(user: person.user, size: :medium) %>
+ <% if member %>
+ <%= render Neo::Users::AvatarComponent.new(user: member, size: :medium) %>
<% else %>
<%= render Neo::Users::AvatarComponent.new(user: nil, size: :medium, fallback_initials: person_initials(person.name)) %>
<% end %>
<div class="min-w-0 flex-1">
- <h2 class="text-[22px] leading-snug font-bold text-heading"><%= person_display_name(person: person) %></h2>
+ <h2 class="text-[22px] leading-snug font-bold text-heading"><%= person_display_name(person: person, user: member) %></h2>
<p class="text-sm text-muted">
- <% if person.user %>
- <%= t('views.organisations.people.show.linked_member', name: viewer_display_name(person.user)) %>
+ <% if member %>
+ <%= t('views.organisations.people.show.linked_member', name: viewer_display_name(member)) %>
+ <% elsif person.user_id %>
+ <%= t('views.organisations.people.show.former_member') %>
<% else %>
<%= t('views.organisations.people.show.no_account') %>
<% end %>
diff --git a/app/views/organisations/people/_form.html.erb b/app/views/organisations/people/_form.html.erb
index f150499..d0cc902 100644
--- a/app/views/organisations/people/_form.html.erb
+++ b/app/views/organisations/people/_form.html.erb
@@ -31,7 +31,9 @@
<div>
<%= f.label :name, class: 'block text-sm font-medium text-cream-700 mb-1' %>
- <%= f.text_field :name, class: input_class, maxlength: Person::MAX_FIELD_LENGTH %>
+ <%# A linked row shows the member's own name; the copy here only shows once
+ the link is gone, so it is not edited while linked. %>
+ <%= f.text_field :name, class: input_class, maxlength: Person::MAX_FIELD_LENGTH, readonly: person.user_id.present? %>
</div>
<% %i[function department company].each do |field| %>
diff --git a/app/views/organisations/people/_list_head.html.erb b/app/views/organisations/people/_list_head.html.erb
index 1433f4b..68a88df 100644
--- a/app/views/organisations/people/_list_head.html.erb
+++ b/app/views/organisations/people/_list_head.html.erb
@@ -19,7 +19,7 @@
lane_link_url_value: new_organisation_person_path(@current_organisation),
lane_link_frame_value: frame_id,
lane_link_set_value: { new: Organisations::PeopleController::NEW_STATE }.to_json,
- lane_link_remove_value: %w[person_id].to_json,
+ lane_link_remove_value: %w[person_id user_id].to_json,
lane_link_focus_value: '#person_name'
}
) %>
diff --git a/app/views/organisations/people/_row.html.erb b/app/views/organisations/people/_row.html.erb
index 0c7e4ff..813d885 100644
--- a/app/views/organisations/people/_row.html.erb
+++ b/app/views/organisations/people/_row.html.erb
@@ -39,7 +39,7 @@
action: 'click->lane-link#click',
lane_link_url_value: new_organisation_person_path(@current_organisation, user_id: entry.user.id),
lane_link_frame_value: Organisations::PeopleController::DETAIL_FRAME_ID,
- lane_link_set_value: { new: Organisations::PeopleController::NEW_STATE }.to_json,
+ lane_link_set_value: { new: Organisations::PeopleController::NEW_STATE, user_id: entry.user.id }.to_json,
lane_link_remove_value: %w[person_id].to_json
} %>
<% end %>
diff --git a/config/locales/views/personen.de.yml b/config/locales/views/personen.de.yml
index 1e41570..9c3b805 100644
--- a/config/locales/views/personen.de.yml
+++ b/config/locales/views/personen.de.yml
@@ -40,6 +40,7 @@ de:
delete_confirm: "Diese Person wirklich löschen? Die Angaben sind danach weg."
linked_member: "Mitglied: %{name}"
no_account: "Kein Konto"
+ former_member: "Früheres Mitglied"
contact_label: "Kontakt"
duplicates_title: "Gleicher Name"
created_by: "Angelegt von %{name} am %{date}"
diff --git a/config/locales/views/personen.en.yml b/config/locales/views/personen.en.yml
index f59d3e1..f7a15f1 100644
--- a/config/locales/views/personen.en.yml
+++ b/config/locales/views/personen.en.yml
@@ -40,6 +40,7 @@ en:
delete_confirm: "Really delete this person? The details will be gone."
linked_member: "Member: %{name}"
no_account: "No account"
+ former_member: "Former member"
contact_label: "Contact"
duplicates_title: "Same name"
created_by: "Added by %{name} on %{date}"
diff --git a/spec/components/neo/buttons/button_component_form_spec.rb b/spec/components/neo/buttons/button_component_form_spec.rb
index 613796a..789685f 100644
--- a/spec/components/neo/buttons/button_component_form_spec.rb
+++ b/spec/components/neo/buttons/button_component_form_spec.rb
@@ -130,7 +130,9 @@ RSpec.describe 'Neo::Buttons::ButtonComponent with form', type: :component do
# Button with all features
expect(page).to have_css("form button[type='submit']")
expect(page).to have_css("form button[data-controller='neo-button']")
- expect(page).to have_css("form button[data-neo-button-confirm-value*='wirklich löschen']")
+ # The form asks once; the button no longer asks a second time.
+ expect(page).to have_css("form[data-turbo-confirm*='wirklich löschen']")
+ expect(page).to have_no_css('form button[data-neo-button-confirm-value]')
expect(page).to have_css('form button i.bi.bi-trash')
# Icon only - no margins
diff --git a/spec/components/neo/buttons/icon_button_component_spec.rb b/spec/components/neo/buttons/icon_button_component_spec.rb
index e735f53..99d1383 100644
--- a/spec/components/neo/buttons/icon_button_component_spec.rb
+++ b/spec/components/neo/buttons/icon_button_component_spec.rb
@@ -11,6 +11,19 @@ RSpec.describe Neo::Buttons::IconButtonComponent, type: :component do
render_inline(component)
end
+ # Same rule as ButtonComponent: with form: the form asks, the button does not
+ # ask a second time (review round 2 of the people directory).
+ describe 'form button with confirmation' do
+ let(:options) { { icon: 'trash', confirm: 'Delete this?', form: { action: '/things/1', method: :delete } } }
+
+ it 'asks once, through the form' do
+ rendered_component
+
+ expect(page).to have_css("form[data-turbo-confirm='Delete this?'] button")
+ expect(page).to have_no_css('button[data-neo-button-confirm-value]')
+ end
+ end
+
describe 'basic rendering' do
it 'renders a button with icon' do
rendered_component
diff --git a/spec/requests/organisations/people_spec.rb b/spec/requests/organisations/people_spec.rb
index 6a29615..342a45a 100644
--- a/spec/requests/organisations/people_spec.rb
+++ b/spec/requests/organisations/people_spec.rb
@@ -390,6 +390,93 @@ RSpec.describe 'Organisations::People', :ci_guard, type: :request do
end
end
+ # Review round 2, finding 1: "Angaben ergänzen" must name the member in the
+ # PAGE address too: below the lane width and on a reload only that is used.
+ it 'puts the member into the page address of "Angaben ergänzen"' do
+ sign_in admin
+ member = member_with(role_with, first_name: 'Ohne')
+
+ get organisation_people_path(organisation)
+
+ row = response.body[%r{<li[^>]*people-row[\s\S]*?Ohne[\s\S]*?</li>}]
+ expect(row).to include(CGI.escapeHTML({ new: 'person', user_id: member.id }.to_json))
+ end
+
+ it 'opens the form with the member chosen on a full visit with ?new=person&user_id=' do
+ sign_in admin
+ member = member_with(role_with, first_name: 'Ohne')
+
+ get organisation_people_path(organisation, new: 'person', user_id: member.id)
+
+ expect(response.body).to match(/<option selected="selected" value="#{member.id}">/)
+ end
+
+ # Review round 2, finding 2: a raw user_id in the address named any account
+ # in the member select, also one the reader may not see or of another org.
+ describe 'a raw user_id in the address of the new form' do
+ let(:project) { create(:project, organisation: organisation) }
+ let(:stranger) { member_with(role_with, first_name: 'Verborgen') }
+ let(:outsider) { create(:user, first_name: 'Fremdorga', last_name: 'Nisation') }
+
+ before do
+ create(:project_permission, user: manager, project: project, tickets_access: :read_only)
+ create(:project_permission, user: stranger, project: create(:project, organisation: organisation),
+ tickets_access: :read_only)
+ create(:membership, user: outsider, organisation: other_organisation)
+ sign_in manager
+ end
+
+ it 'does not name a member the reader cannot see' do
+ get new_organisation_person_path(organisation, user_id: stranger.id), headers: frame_headers
+
+ expect(response.body).not_to include('Verborgen', stranger.id)
+ end
+
+ it 'does not name an account of another organisation' do
+ get organisation_people_path(organisation, new: 'person', user_id: outsider.id)
+
+ expect(response.body).not_to include('Fremdorga', outsider.id)
+ end
+ end
+
+ # Review round 2, finding 3: a row whose member was deactivated is a contact;
+ # list and lane show the row's own name, not the account's current one.
+ it 'shows the row name, not the account, for a row of a deactivated member' do
+ former = member_with(role_with, first_name: 'Alt')
+ create(:person, organisation: organisation, user: former, name: 'Alt Name')
+ Membership.find_by(user: former, organisation: organisation).update!(active: false)
+ former.update!(first_name: 'Neu', last_name: 'Anders')
+ row = Person.find_by!(user: former)
+ sign_in admin
+
+ get organisation_people_path(organisation, person_id: row.id)
+
+ expect(response.body).to include('Alt Name')
+ expect(response.body).not_to include('Neu Anders')
+ end
+
+ # Review round 2, finding 5: a hand-made request without a usable
+ # lock_version must not crash.
+ it 'answers a missing lock_version as a stale save, not with an error' do
+ sign_in admin
+
+ patch organisation_person_path(organisation, person), params: { person: { phone: '0', lock_version: '' } }
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(person.reload.phone).to eq('+49 30 1234567')
+ end
+
+ # Review round 2, finding 6: a linked row shows the member's name; its own
+ # name field is read only there.
+ it 'shows the name field read only for a linked row' do
+ linked = create(:person, organisation: organisation, user: viewer, name: 'Kopie')
+ sign_in admin
+
+ get edit_organisation_person_path(organisation, linked), headers: frame_headers
+
+ expect(response.body).to match(/<input[^>]*readonly="readonly"[^>]*id="person_name"|<input[^>]*id="person_name"[^>]*readonly="readonly"/)
+ end
+
it 'has no missing translation on the page' do
sign_in admin
get organisation_people_path(organisation, person_id: person.id)