Files
sure/app/views/admin/users/index.html.erb
T
bb835b9793 feat: Super Admins can Delete Users and Modify Families/Groups (#2868)
* Add admin family management features and tests

- Implement FamiliesController with destroy action to delete unused families.
- Add localization for success and error messages related to family deletion.
- Create FamiliesControllerTest to ensure proper functionality of family deletion.
- Update UserPolicyTest to include permissions for super admins to delete users.
- Enhance UsersControllerTest with tests for user family management, including moving users between families and creating new families.

* feat(users): enhance user management with family transfer validation and improved delete warnings

* Simplify user management actions column and combine family options

Move heavy user edit forms from table rows into a DS::Popover action
menu, add role badges to the user column, combine family migration and
creation inputs with a Stimulus controller, enable self-family
transfer for super admins, and add safety guards against demoting the
last super admin in the system.

* feat: add authentication type pills to admin user index to display SSO and local login status

* Add set password feature for local users in admin user management

- Add password field in action popover for users with local password login
- Enforce all registration password criteria (min 8 chars, mixed case, digit, special char)
- Block simultaneous family and password updates with clear error
- Show descriptive success notifications (role, password, both, family)
- Ignore password param for SSO-only users
- Add comprehensive tests for all password validation paths

* Resolve DS Drift Patrol findings and CI scan failures

- Wrap auth-type pills in DS::Tooltip instead of native title= attribute

- Add actions.manage_user key to locale and drop redundant default: fallbacks

- Fix RuboCop style offenses in Admin::UsersController

- Update Brakeman ignore entry fingerprint for Admin::UsersController#user_params

* fix: update badge query to target DS::Pill structure

* Fix DS::Tooltip misuse hiding SSO auth-type pill in admin users view

The SSO pill was passed as a block to DS::Tooltip, which caused it to
render inside the hidden div[role="tooltip"] instead of being visible.
The text: option ("SSO Provider: ...") was also silently ignored because
tooltip_content returns content (the block) over @text when a block is
given.

Fix: render the SSO/Local+SSO pill directly as visible content and pass
DS::Tooltip with no block so text: is used as the tooltip popup. An info
icon now appears next to the pill and shows the provider name on hover.

Fixes test: Admin::UsersControllerTest#test_index_renders_auth_type_pills_for_local_and_sso_users

* Remove redundant default: fallback from role pill i18n lookup

All admin.users.index.roles.{guest,member,admin,super_admin} keys are
defined in the locale file and used elsewhere in the same view without
a default:. The fallback was redundant for every valid role and would
silently mask a missing or renamed key instead of raising in
development.

Drop the default: user.role.humanize argument so that any future
missing key surfaces immediately as I18n::MissingTranslationData.

* Revert unrelated JS/schema/split churn; fix transfer_to_family! default role

- Revert 62 JS files (Biome formatter and unrelated controller changes)
- Revert db/schema.rb dump churn (no new migrations in this branch)
- Revert unrelated split transaction view changes (edit/new.html.erb)
- Fix User#transfer_to_family! role default: role: role evaluates to nil
  when omitted; use explicit self.role to read model attribute

Keeps the PR focused on user/family management (~18-20 files).

* Fix last login and session count in admin user management

Store last_login_at and sessions_count directly on the users table
so they remain accurate after a user logs out.

- Add migration to add last_login_at (datetime) and sessions_count
  (integer, default 0) columns to users, with backfill from sessions
- Add counter_cache: :sessions_count to Session#belongs_to :user so
  the count auto-increments/decrements on session create/destroy
- Add after_create callback on Session to stamp user.last_login_at
- Update Admin::UsersController to read both values from users table
  instead of aggregating Session rows (which disappear on logout)

* Fix user management PR pending CI items

* Keep test current session after sign in

* Address PR review comments for user transfers

* refactor: update user removal label to "Delete User" and standardize component attribute naming

* Address PR Review Feedback for User Management

* test: Fix families and users controller tests for user management PR

* Limit PR 2868 schema diff

* Fix PR 2868 user management CI failures

---------

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: sure-admin <sure-admin@splashblot.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-08-28 23:10:09 +02:00

389 lines
22 KiB
ERB

<%= content_for :page_title, t(".title") %>
<div class="bg-container rounded-xl shadow-border-xs p-4 space-y-6">
<div>
<p class="text-sm text-secondary"><%= t(".description") %></p>
</div>
<!-- Filters -->
<div>
<%= form_with url: admin_users_path, method: :get, class: "flex gap-4 items-end flex-wrap" do |f| %>
<div class="w-full md:w-auto">
<%= f.label :role, t(".filters.role"), class: "block text-sm font-medium text-primary mb-1" %>
<%= f.select :role,
options_for_select(
[[t(".filters.role_all"), ""], [t(".roles.guest"), "guest"], [t(".roles.member"), "member"], [t(".roles.admin"), "admin"], [t(".roles.super_admin"), "super_admin"]],
params[:role]
),
{},
class: "rounded-lg border border-primary px-3 py-2 text-sm bg-container-inset text-primary w-full" %>
</div>
<div class="w-full md:w-auto">
<%= f.label :trial_status, t(".filters.trial_status"), class: "block text-sm font-medium text-primary mb-1" %>
<%= f.select :trial_status,
options_for_select(
[[t(".filters.trial_all"), ""], [t(".filters.trial_expiring_soon"), "expiring_soon"], [t(".filters.trial_trialing"), "trialing"]],
params[:trial_status]
),
{},
class: "rounded-lg border border-primary px-3 py-2 text-sm bg-container-inset text-primary w-full" %>
</div>
<%= render DS::Button.new(variant: :primary, size: :md, type: "submit", text: t(".filters.submit"), class: "md:w-auto w-full justify-center") %>
<% end %>
</div>
<!-- Summary: trials expiring in next 7 days -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<div class="bg-container-inset rounded-lg p-4">
<div class="flex items-center gap-2 mb-2">
<%= icon "calendar-clock", class: "w-5 h-5 text-secondary" %>
<p class="text-xs font-medium text-secondary uppercase"><%= t(".summary.trials_expiring_7_days") %></p>
</div>
<p class="text-2xl font-semibold text-primary"><%= @trials_expiring_in_7_days %></p>
</div>
</div>
<!-- Families/Groups & Users -->
<div>
<h2 class="text-lg font-semibold text-primary mb-3"><%= t(".section_title") %></h2>
<% if @families_with_users.any? %>
<div class="space-y-4">
<% @families_with_users.each do |family, users| %>
<% pending_invitations = @invitations_by_family[family.id] || [] %>
<%= render DS::Disclosure.new(
variant: :bare,
summary_class: "list-none focus-ring flex items-center justify-between gap-4 px-4 py-3 cursor-pointer select-none hover:bg-surface-hover",
class: "bg-container-inset rounded-lg overflow-hidden",
data: {
controller: "admin-invitation-delete",
admin_invitation_delete_delete_all_label_value: t(".invitations.delete_all")
}
) do |disclosure| %>
<% disclosure.with_summary_content do %>
<div class="flex items-center gap-3">
<%= icon "users", class: "w-5 h-5 text-secondary shrink-0" %>
<div>
<p class="font-semibold text-primary"><%= family.name.presence || t(".unnamed_family") %></p>
<p class="text-xs text-secondary">
<%= t(".family_summary",
members: users.size,
accounts: number_with_delimiter(@accounts_count_by_family[family.id] || 0),
transactions: number_with_delimiter(@entries_count_by_family[family.id] || 0)) %>
</p>
</div>
</div>
<div class="flex items-center gap-4 shrink-0">
<% sub = family.subscription %>
<% if sub&.trialing? %>
<span class="text-xs text-secondary">
<%= t(".table.trial_ends_at") %>: <%= sub.trial_ends_at&.to_fs(:long) || t(".not_available") %>
</span>
<% elsif sub %>
<%= render DS::Pill.new(label: sub.status.humanize, tone: sub.active? ? :success : :neutral) %>
<% else %>
<span class="text-xs text-secondary"><%= t(".no_subscription") %></span>
<% end %>
<%= icon "chevron-down", class: "w-4 h-4 text-secondary transition-transform group-open:rotate-180" %>
</div>
<% end %>
<div class="border-t border-primary">
<table class="w-full">
<thead class="bg-surface border-b border-primary">
<tr>
<th class="px-4 py-2 text-left text-xs font-medium text-secondary uppercase"><%= t(".table.user") %></th>
<th class="px-4 py-2 text-left text-xs font-medium text-secondary uppercase"><%= t(".table.last_login") %></th>
<th class="px-4 py-2 text-right text-xs font-medium text-secondary uppercase"><%= t(".table.session_count") %></th>
<th class="px-4 py-2 text-right text-xs font-medium text-secondary uppercase"><%= t(".table.actions") %></th>
</tr>
</thead>
<tbody class="divide-y divide-alpha-black-200 theme-dark:divide-alpha-white-200">
<% users.each do |user| %>
<tr>
<td class="px-4 py-3">
<div class="flex items-center gap-3">
<div class="w-8 h-8 rounded-full bg-surface flex items-center justify-center shrink-0">
<span class="text-sm font-medium text-primary"><%= user.initials %></span>
</div>
<div>
<div class="flex items-center gap-2">
<p class="font-medium text-primary"><%= user.display_name %></p>
<% if user.id == Current.user.id %>
<span class="text-xs font-medium text-secondary"><%= t(".you") %></span>
<% end %>
<%= render DS::Pill.new(label: t(".roles.#{user.role}"), tone: user.super_admin? ? :success : :neutral) %>
<% if user.oidc_identities.any? %>
<% sso_providers = user.oidc_identities.map { |i| (i.provider_config&.dig(:label) || i.provider_config&.dig("label")).presence || i.provider.titleize }.uniq.join(", ") %>
<% provider_tooltip = t(".auth_types.sso_provider_tooltip", providers: sso_providers) %>
<% if user.sso_only? %>
<%= render DS::Pill.new(label: t(".auth_types.sso"), tone: :info) %>
<%= render DS::Tooltip.new(text: provider_tooltip, as: :span) %>
<% else %>
<%= render DS::Pill.new(label: t(".auth_types.local_and_sso"), tone: :info) %>
<%= render DS::Tooltip.new(text: provider_tooltip, as: :span) %>
<% end %>
<% else %>
<%= render DS::Pill.new(label: t(".auth_types.local"), tone: :neutral) %>
<% end %>
</div>
<p class="text-sm text-secondary"><%= user.email %></p>
</div>
</div>
</td>
<td class="px-4 py-3 text-sm text-primary whitespace-nowrap">
<%= @last_login_by_user[user.id]&.to_fs(:long) || t(".table.never") %>
</td>
<td class="px-4 py-3 text-sm text-primary text-right whitespace-nowrap">
<%= number_with_delimiter(@sessions_count_by_user[user.id] || 0) %>
</td>
<td class="px-4 py-3 text-right whitespace-nowrap">
<%= render DS::Popover.new(variant: :icon, icon: "ellipsis-vertical", placement: "bottom-end", aria_label: t(".actions.manage_user")) do |popover| %>
<% popover.with_custom_content do %>
<% is_last_super_admin = user.super_admin? && User.where(role: :super_admin).where.not(id: user.id).none? %>
<div class="p-4 w-80 space-y-4 text-left whitespace-normal">
<div class="border-b border-primary pb-2">
<p class="font-medium text-sm text-primary"><%= user.display_name %></p>
<p class="text-xs text-secondary"><%= user.email %></p>
</div>
<%= form_with model: [:admin, user], method: :patch, class: "space-y-3" do |form| %>
<div>
<%= form.label :role, t(".table.role"), class: "block text-xs font-medium text-secondary mb-1" %>
<% if is_last_super_admin %>
<%= form.select :role,
options_for_select([[t(".roles.super_admin"), "super_admin"]], "super_admin"),
{},
disabled: true,
class: "w-full text-sm rounded-lg border border-primary bg-container-inset text-secondary px-2 py-1.5 cursor-not-allowed" %>
<%= form.hidden_field :role, value: "super_admin" %>
<p class="text-xs text-warning mt-1 leading-normal"><%= t(".actions.last_super_admin_role_locked") %></p>
<% else %>
<%= form.select :role,
options_for_select([
[t(".roles.guest"), "guest"],
[t(".roles.member"), "member"],
[t(".roles.admin"), "admin"],
[t(".roles.super_admin"), "super_admin"]
], user.role),
{},
class: "w-full text-sm rounded-lg border border-primary bg-container text-primary px-2 py-1.5" %>
<% end %>
</div>
<div data-controller="admin-user-family-select" class="space-y-3">
<div>
<%= form.label :family_id, t(".table.family"), class: "block text-xs font-medium text-secondary mb-1" %>
<%= form.select :family_id,
options_for_select([
[t(".table.keep_current_family"), ""],
[t(".table.create_new_family_option"), "new"]
] + @families.map { |family| [family_label_for(family), family.id] }, user.family_id),
{},
class: "w-full text-sm rounded-lg border border-primary bg-container text-primary px-2 py-1.5",
data: {
admin_user_family_select_target: "select",
action: "change->admin-user-family-select#toggle"
} %>
</div>
<div data-admin-user-family-select-target="newFamilyFields" class="hidden border-t border-primary pt-3 space-y-3">
<div>
<%= form.label :new_family_name, t(".table.new_family_name"), class: "block text-xs font-medium text-secondary mb-1" %>
<%= form.text_field :new_family_name,
placeholder: t(".table.new_family_name_placeholder"),
class: "w-full text-sm rounded-lg border border-primary bg-container text-primary px-2 py-1.5",
data: { admin_user_family_select_target: "nameInput" } %>
</div>
<div>
<%= form.label :new_family_moniker, t(".table.new_family_moniker"), class: "block text-xs font-medium text-secondary mb-1" %>
<%= form.select :new_family_moniker,
options_for_select([[t(".family_monikers.family"), "Family"], [t(".family_monikers.group"), "Group"]], "Family"),
{},
class: "w-full text-sm rounded-lg border border-primary bg-container text-primary px-2 py-1.5" %>
</div>
</div>
</div>
<% if user.has_local_password? %>
<div>
<%= form.label :password, t(".table.set_password"), class: "block text-xs font-medium text-secondary mb-1" %>
<%= form.password_field :password,
placeholder: t(".table.password_placeholder"),
autocomplete: "new-password",
class: "w-full text-sm rounded-lg border border-primary bg-container text-primary px-2 py-1.5" %>
</div>
<% end %>
<% if user.id == Current.user.id %>
<div class="bg-warning/10 border border-warning/20 rounded-lg p-2.5 text-xs text-warning whitespace-normal break-words leading-normal">
<p class="font-medium"><%= t(".actions.self_edit_warning") %></p>
</div>
<% end %>
<div class="pt-1">
<%= render DS::Button.new(
variant: :primary,
size: :sm,
type: "submit",
confirm: (user.id == Current.user.id ? t(".actions.self_update_confirm") : nil),
text: t(".actions.update"),
class: "w-full justify-center"
) %>
</div>
<% end %>
<% unless user.id == Current.user.id || (user.super_admin? && user.active? && @active_super_admin_count <= 1) %>
<div class="border-t border-primary pt-3">
<%= render DS::Button.new(
text: t(".remove"),
href: deletion_admin_user_path(user),
method: :get,
frame: "modal",
variant: :outline_destructive,
size: :sm,
class: "w-full justify-center"
) %>
</div>
<% end %>
</div>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
<% if pending_invitations.any? %>
<tbody class="divide-y divide-alpha-black-200 theme-dark:divide-alpha-white-200 border-t border-dashed border-primary">
<% pending_invitations.each do |invitation| %>
<tr class="bg-destructive/5">
<td class="px-4 py-3">
<div class="flex items-center gap-3">
<%= icon "mail", class: "w-5 h-5 text-secondary shrink-0" %>
<div>
<p class="font-medium text-secondary italic"><%= invitation.email %></p>
<p class="text-xs text-secondary"><%= t(".invitations.pending_label") %></p>
</div>
</div>
</td>
<td class="px-4 py-3 text-sm text-secondary whitespace-nowrap">
<%= t(".invitations.expires", date: invitation.expires_at.to_fs(:long)) %>
</td>
<td class="px-4 py-3 text-sm text-secondary text-right whitespace-nowrap">
</td>
<td class="px-4 py-3 text-right">
<%= form_with url: admin_invitation_path(invitation), method: :delete, class: "inline" do |f| %>
<%= render DS::Button.new(
text: t(".invitations.delete"),
type: "submit",
variant: :destructive,
size: :sm,
data: {
admin_invitation_delete_target: "button",
action: "click->admin-invitation-delete#handleClick"
}
) %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
<% end %>
</table>
<% if pending_invitations.any? %>
<%= form_with url: invitations_admin_family_path(family), method: :delete,
data: { admin_invitation_delete_target: "destroyAllForm" },
class: "hidden" do |f| %>
<% end %>
<% end %>
</div>
<% end %>
<% end %>
</div>
<% else %>
<div class="bg-container-inset rounded-lg p-8 text-center">
<%= icon "users", class: "w-12 h-12 mx-auto text-secondary mb-3" %>
<p class="text-secondary"><%= t(".no_users") %></p>
</div>
<% end %>
</div>
<div>
<h2 class="text-lg font-semibold text-primary mb-3"><%= t(".unused_families.title") %></h2>
<% if @unused_families.any? %>
<div class="space-y-2">
<% @unused_families.each do |family| %>
<div class="bg-container-inset rounded-lg p-3 flex items-center justify-between gap-3">
<div>
<p class="font-medium text-primary"><%= family.name.presence || t(".unnamed_family") %></p>
<p class="text-xs text-secondary"><%= t(".unused_families.family_id", id: family.id.to_s.first(8)) %></p>
</div>
<%= render DS::Button.new(
variant: :outline_destructive,
size: :sm,
href: admin_family_path(family),
method: :delete,
confirm: t(".unused_families.delete_confirm"),
text: t(".unused_families.delete")
) %>
</div>
<% end %>
</div>
<% else %>
<div class="bg-container-inset rounded-lg p-4 text-sm text-secondary">
<%= t(".unused_families.empty") %>
</div>
<% end %>
</div>
<% if @sso_identity_blocks.any? %>
<%= settings_section title: t(".removed_sso_identities.title"), collapsible: true, open: false do %>
<p class="text-sm text-secondary mb-4"><%= t(".removed_sso_identities.description") %></p>
<div class="space-y-2">
<% @sso_identity_blocks.each do |block| %>
<div class="flex items-center justify-between gap-4 rounded-lg border border-primary p-3">
<div>
<p class="font-medium text-primary"><%= block.identity_label %></p>
<p class="text-sm text-secondary"><%= block.provider %> · <%= l(block.created_at, format: :long) %></p>
</div>
<%= form_with url: admin_sso_identity_block_path(block), method: :delete do %>
<%= render DS::Button.new(
text: t(".removed_sso_identities.allow_again"),
type: "submit",
variant: :outline_destructive,
size: :sm,
data: { turbo_confirm: t(".removed_sso_identities.confirm", email: block.identity_label) }
) %>
<% end %>
</div>
<% end %>
</div>
<% end %>
<% end %>
<%= settings_section title: t(".role_descriptions_title"), collapsible: true, open: true do %>
<div class="space-y-3 text-sm">
<div class="flex items-start gap-3">
<%= render DS::Pill.new(label: t(".roles.guest"), tone: :neutral) %>
<p class="text-secondary"><%= t(".role_descriptions.guest") %></p>
</div>
<div class="flex items-start gap-3">
<%= render DS::Pill.new(label: t(".roles.member"), tone: :neutral) %>
<p class="text-secondary"><%= t(".role_descriptions.member") %></p>
</div>
<div class="flex items-start gap-3">
<%= render DS::Pill.new(label: t(".roles.admin"), tone: :neutral) %>
<p class="text-secondary"><%= t(".role_descriptions.admin") %></p>
</div>
<div class="flex items-start gap-3">
<%= render DS::Pill.new(label: t(".roles.super_admin"), tone: :success) %>
<p class="text-secondary"><%= t(".role_descriptions.super_admin") %></p>
</div>
</div>
<% end %>
</div>