feat(core): UserProfileViewService + admin/users detail drawer
Extracts the user profile read-view into a core service + shared partial so
modules no longer duplicate the assignment/scope logic, and migrates the
admin/users list as the drawer's second consumer.
Consolidation:
- `core/Service/User/UserProfileViewService` owns the single read-path that
resolves tenants/departments/roles for a user UUID, enforces scope-aware
department visibility, and returns a consistent `{status, user}` shape.
- `templates/partials/app-user-profile.phtml` is the shared render template;
takes `$profileKey` for storage namespace + lightbox grouping.
- `AddressBookService::buildViewContext()` shrinks to a one-line delegation
and drops its `UserAssignmentService` dependency. The old
`modules/addressbook/templates/address-book-profile.phtml` is removed.
admin/users migration:
- `pages/admin/users/view-fragment($id).php` + `(none).phtml` use the core
service and enforce `ABILITY_ADMIN_USERS_VIEW`.
- `admin-users-index.js` replaces the grid-native `linkColumn` on the first
name with a formatter that emits `data-drawer-trigger`; `linkColumn` is
disabled so the drawer click handler wins. `fullUrl` points at the edit
form — drawer → edit is one click.
- Drawer labels wired via page config.
The new `DetailDrawerFragmentContractTest` validated the users fragment
endpoint automatically — no manual test additions were needed.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -12,8 +12,8 @@ use MintyPHP\Service\Org\DepartmentService;
|
||||
use MintyPHP\Service\Tenant\TenantScopeService;
|
||||
use MintyPHP\Service\Tenant\TenantService;
|
||||
use MintyPHP\Service\User\UserAccountService;
|
||||
use MintyPHP\Service\User\UserAssignmentService;
|
||||
use MintyPHP\Service\User\UserAvatarService;
|
||||
use MintyPHP\Service\User\UserProfileViewService;
|
||||
|
||||
final class AddressBookContainerRegistrar implements ContainerRegistrar
|
||||
{
|
||||
@@ -25,13 +25,13 @@ final class AddressBookContainerRegistrar implements ContainerRegistrar
|
||||
|
||||
$container->set(AddressBookService::class, static fn (AppContainer $c): AddressBookService => new AddressBookService(
|
||||
$c->get(UserAccountService::class),
|
||||
$c->get(UserAssignmentService::class),
|
||||
$c->get(TenantService::class),
|
||||
$c->get(DepartmentService::class),
|
||||
$c->get(RoleService::class),
|
||||
$c->get(TenantScopeService::class),
|
||||
$c->get(UserCustomFieldValueService::class),
|
||||
$c->get(UserAvatarService::class)
|
||||
$c->get(UserAvatarService::class),
|
||||
$c->get(UserProfileViewService::class)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,20 +9,20 @@ use MintyPHP\Service\Org\DepartmentService;
|
||||
use MintyPHP\Service\Tenant\TenantScopeService;
|
||||
use MintyPHP\Service\Tenant\TenantService;
|
||||
use MintyPHP\Service\User\UserAccountService;
|
||||
use MintyPHP\Service\User\UserAssignmentService;
|
||||
use MintyPHP\Service\User\UserAvatarService;
|
||||
use MintyPHP\Service\User\UserProfileViewService;
|
||||
|
||||
class AddressBookService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly UserAccountService $userAccountService,
|
||||
private readonly UserAssignmentService $userAssignmentService,
|
||||
private readonly TenantService $tenantService,
|
||||
private readonly DepartmentService $departmentService,
|
||||
private readonly RoleService $roleService,
|
||||
private readonly TenantScopeService $scopeGateway,
|
||||
private readonly UserCustomFieldValueService $customFieldValueService,
|
||||
private readonly UserAvatarService $avatarService
|
||||
private readonly UserAvatarService $avatarService,
|
||||
private readonly UserProfileViewService $userProfileViewService
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -240,101 +240,7 @@ class AddressBookService
|
||||
|
||||
public function buildViewContext(int $currentUserId, string $uuid): array
|
||||
{
|
||||
$uuid = trim($uuid);
|
||||
if ($uuid === '') {
|
||||
return ['status' => 'invalid_uuid'];
|
||||
}
|
||||
|
||||
$user = $this->userAccountService->findByUuid($uuid);
|
||||
if (!$user || !isset($user['id'])) {
|
||||
return ['status' => 'not_found'];
|
||||
}
|
||||
|
||||
$targetUserId = (int) $user['id'];
|
||||
if ($targetUserId <= 0) {
|
||||
return ['status' => 'not_found'];
|
||||
}
|
||||
|
||||
// Users can always view their own profile; others require scope access.
|
||||
if (
|
||||
$currentUserId !== $targetUserId
|
||||
&& !$this->scopeGateway->canAccess('users', $targetUserId, $currentUserId)
|
||||
) {
|
||||
return ['status' => 'forbidden'];
|
||||
}
|
||||
|
||||
$viewerTenantIds = $this->normalizeIds($this->scopeGateway->getUserTenantIds($currentUserId));
|
||||
$assignments = $this->userAssignmentService->buildAssignmentsForUser($targetUserId);
|
||||
|
||||
$departmentMap = [];
|
||||
foreach (($assignments['departments'] ?? []) as $department) {
|
||||
if (empty($department['active'])) {
|
||||
continue;
|
||||
}
|
||||
$tenantId = (int) ($department['tenant_id'] ?? 0);
|
||||
$label = trim((string) ($department['description'] ?? ''));
|
||||
// Only show departments in tenants the viewer also belongs to — prevents leaking org structure.
|
||||
if ($tenantId <= 0 || $label === '' || !in_array($tenantId, $viewerTenantIds, true)) {
|
||||
continue;
|
||||
}
|
||||
$departmentMap[$tenantId] ??= [];
|
||||
$departmentMap[$tenantId][] = $label;
|
||||
}
|
||||
foreach ($departmentMap as $tenantId => $labels) {
|
||||
$labels = array_values(array_unique($labels));
|
||||
natcasesort($labels);
|
||||
$departmentMap[$tenantId] = array_values($labels);
|
||||
}
|
||||
|
||||
$primaryTenantId = (int) ($user['primary_tenant_id'] ?? 0);
|
||||
$tenantGroups = [];
|
||||
foreach (($assignments['tenants'] ?? []) as $tenant) {
|
||||
$tenantId = (int) ($tenant['id'] ?? 0);
|
||||
$tenantLabel = trim((string) ($tenant['description'] ?? ''));
|
||||
$status = strtolower(trim((string) ($tenant['status'] ?? '')));
|
||||
if (
|
||||
$tenantId <= 0
|
||||
|| $tenantLabel === ''
|
||||
|| $status !== 'active'
|
||||
|| !in_array($tenantId, $viewerTenantIds, true)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
$tenantGroups[] = [
|
||||
'label' => $tenantLabel,
|
||||
'is_primary' => $primaryTenantId > 0 && $tenantId === $primaryTenantId,
|
||||
'departments' => $departmentMap[$tenantId] ?? [],
|
||||
];
|
||||
}
|
||||
|
||||
usort($tenantGroups, static fn (array $a, array $b): int => strnatcasecmp(
|
||||
(string) $a['label'],
|
||||
(string) $b['label']
|
||||
));
|
||||
|
||||
$roleLabels = [];
|
||||
foreach (($assignments['roles'] ?? []) as $role) {
|
||||
if (empty($role['active'])) {
|
||||
continue;
|
||||
}
|
||||
$label = trim((string) ($role['description'] ?? ''));
|
||||
if ($label !== '') {
|
||||
$roleLabels[] = $label;
|
||||
}
|
||||
}
|
||||
$roleLabels = array_values(array_unique($roleLabels));
|
||||
natcasesort($roleLabels);
|
||||
$roleLabels = array_values($roleLabels);
|
||||
|
||||
$avatarUuid = (string) ($user['uuid'] ?? '');
|
||||
$user['has_avatar'] = $avatarUuid !== '' && $this->avatarService->hasAvatar($avatarUuid);
|
||||
$user['tenant_groups'] = $tenantGroups;
|
||||
$user['role_labels'] = $roleLabels;
|
||||
|
||||
return [
|
||||
'status' => 'ok',
|
||||
'user' => $user,
|
||||
];
|
||||
return $this->userProfileViewService->buildProfile($currentUserId, $uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
<?php
|
||||
require __DIR__ . '/../../templates/address-book-profile.phtml';
|
||||
$profileKey = 'address-book-profile';
|
||||
require templatePath('partials/app-user-profile.phtml');
|
||||
|
||||
@@ -2,4 +2,5 @@
|
||||
if (!isset($user) || !is_array($user)) {
|
||||
return;
|
||||
}
|
||||
require __DIR__ . '/../../templates/address-book-profile.phtml';
|
||||
$profileKey = 'address-book-profile';
|
||||
require templatePath('partials/app-user-profile.phtml');
|
||||
|
||||
@@ -1,239 +0,0 @@
|
||||
<?php
|
||||
|
||||
use MintyPHP\I18n;
|
||||
|
||||
$values = $user ?? [];
|
||||
$name = trim(($values['first_name'] ?? '') . ' ' . ($values['last_name'] ?? ''));
|
||||
$displayName = $name !== '' ? $name : ($values['email'] ?? t('Address book'));
|
||||
$email = trim((string) ($values['email'] ?? ''));
|
||||
$phone = trim((string) ($values['phone'] ?? ''));
|
||||
$mobile = trim((string) ($values['mobile'] ?? ''));
|
||||
$shortDial = trim((string) ($values['short_dial'] ?? ''));
|
||||
$address = trim((string) ($values['address'] ?? ''));
|
||||
$postalCode = trim((string) ($values['postal_code'] ?? ''));
|
||||
$city = trim((string) ($values['city'] ?? ''));
|
||||
$region = trim((string) ($values['region'] ?? ''));
|
||||
$country = trim((string) ($values['country'] ?? ''));
|
||||
$profileDescription = trim((string) ($values['profile_description'] ?? ''));
|
||||
$jobTitle = trim((string) ($values['job_title'] ?? ''));
|
||||
$hireDate = trim((string) ($values['hire_date'] ?? ''));
|
||||
$hireDateLabel = '';
|
||||
if ($hireDate !== '') {
|
||||
$format = (strpos((string) (I18n::$locale ?? ''), 'de') === 0) ? 'd.m.Y' : 'Y-m-d';
|
||||
try {
|
||||
$hireDateLabel = (new \DateTimeImmutable($hireDate))->format($format);
|
||||
} catch (\Exception $e) {
|
||||
$hireDateLabel = $hireDate;
|
||||
}
|
||||
}
|
||||
$avatarUuid = (string) ($values['uuid'] ?? '');
|
||||
$hasAvatar = !empty($values['has_avatar']) && $avatarUuid !== '';
|
||||
|
||||
$initials = '';
|
||||
if ($displayName !== '') {
|
||||
$parts = array_filter(array_map('trim', preg_split('/\s+/', $displayName) ?: []));
|
||||
$chars = '';
|
||||
foreach ($parts as $part) {
|
||||
if (function_exists('mb_substr')) {
|
||||
$chars .= mb_substr($part, 0, 1);
|
||||
} else {
|
||||
$chars .= substr($part, 0, 1);
|
||||
}
|
||||
}
|
||||
$initials = strtoupper($chars !== '' ? $chars : '?');
|
||||
}
|
||||
$tenantGroups = $values['tenant_groups'] ?? [];
|
||||
$hasContact = ($email !== '' || $phone !== '' || $mobile !== '' || $shortDial !== '');
|
||||
$hasAddress = ($address !== '' || $postalCode !== '' || $city !== '' || $region !== '' || $country !== '');
|
||||
$hasOrganization = (!empty($tenantGroups));
|
||||
$aboutSummary = '';
|
||||
if ($profileDescription !== '') {
|
||||
foreach (preg_split('/\\r\\n|\\r|\\n/', $profileDescription) as $line) {
|
||||
$line = trim((string) $line);
|
||||
if ($line !== '') {
|
||||
$aboutSummary = $line;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<div class="app-details-container">
|
||||
<section>
|
||||
<div class="app-profile-card-container">
|
||||
<div class="app-profile-card">
|
||||
<div class="app-profile-banner"></div>
|
||||
<div class="app-profile-header">
|
||||
<div class="user-avatar-block avatar-round app-profile-avatar">
|
||||
<?php if ($hasAvatar): ?>
|
||||
<a data-fslightbox="address-book-avatar"
|
||||
href="admin/users/avatar-file?uuid=<?php e($avatarUuid); ?>&size=256">
|
||||
<img class="user-avatar-image" src="admin/users/avatar-file?uuid=<?php e($avatarUuid); ?>&size=160"
|
||||
alt="">
|
||||
</a>
|
||||
<?php else: ?>
|
||||
<span class="user-avatar-placeholder"><?php e($initials ?: '?'); ?></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="app-profile-meta">
|
||||
<hgroup>
|
||||
<h2><?php e($displayName); ?></h2>
|
||||
<?php if ($jobTitle !== ''): ?>
|
||||
<p><small><?php e($jobTitle); ?></small></p>
|
||||
<?php else: ?>
|
||||
<p><a href="mailto:<?php e($email); ?>"><small><?php e($email); ?></small></a></p>
|
||||
<?php endif; ?>
|
||||
</hgroup>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="app-profile-body-container">
|
||||
<div class="app-profile-body">
|
||||
<div class="app-tabs" data-tabs data-app-component="tabs" data-tabs-param="tab" data-tabs-storage-key="address-book-profile">
|
||||
<div class="app-tabs-nav">
|
||||
<?php if ($hasContact): ?>
|
||||
<button type="button" data-tab="contact" data-tab-default><small><?php e(t('Contact')); ?></small></button>
|
||||
<?php endif; ?>
|
||||
<?php if ($hasAddress): ?>
|
||||
<button type="button" data-tab="address"><small><?php e(t('Address')); ?></small></button>
|
||||
<?php endif; ?>
|
||||
<?php if ($hasOrganization): ?>
|
||||
<button type="button" data-tab="organization"><small><?php e(t('Organization')); ?></small></button>
|
||||
<?php endif; ?>
|
||||
<button type="button" data-tab="about"><small><?php e(t('About')); ?></small></button>
|
||||
</div>
|
||||
<?php if ($hasContact): ?>
|
||||
<div data-tab-panel="contact">
|
||||
<div class="grid">
|
||||
<?php if ($email !== ''): ?>
|
||||
<div>
|
||||
<small><?php e(t('Email')); ?></small>
|
||||
<p><a href="mailto:<?php e($email); ?>"><?php e($email); ?></a></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if ($phone !== ''): ?>
|
||||
<div>
|
||||
<small><?php e(t('Phone')); ?></small>
|
||||
<p><a href="tel:<?php e($phone); ?>"><?php e($phone); ?></a></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="grid">
|
||||
<?php if ($mobile !== ''): ?>
|
||||
<div>
|
||||
<small><?php e(t('Mobile')); ?></small>
|
||||
<p><a href="tel:<?php e($mobile); ?>"><?php e($mobile); ?></a></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if ($shortDial !== ''): ?>
|
||||
<div>
|
||||
<small><?php e(t('Short dial')); ?></small>
|
||||
<p><?php e($shortDial); ?></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if ($hasAddress): ?>
|
||||
<div data-tab-panel="address">
|
||||
<div>
|
||||
<?php if ($address !== ''): ?>
|
||||
<p><?php e($address); ?></p>
|
||||
<?php endif; ?>
|
||||
<?php $line2 = trim($postalCode . ' ' . $city); ?>
|
||||
<?php if ($line2 !== ''): ?>
|
||||
<p><?php e($line2); ?></p>
|
||||
<?php endif; ?>
|
||||
<?php if ($region !== ''): ?>
|
||||
<p><?php e($region); ?></p>
|
||||
<?php endif; ?>
|
||||
<?php if ($country !== ''): ?>
|
||||
<p><?php e($country); ?></p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if ($hasOrganization): ?>
|
||||
<div data-tab-panel="organization">
|
||||
<?php if (!empty($tenantGroups)): ?>
|
||||
<table class="app-profile-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php e(t('Tenant')); ?></th>
|
||||
<th><?php e(t('Departments')); ?></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($tenantGroups as $group): ?>
|
||||
<?php
|
||||
$departments = $group['departments'] ?? [];
|
||||
$departments = is_array($departments) ? $departments : [];
|
||||
?>
|
||||
<tr>
|
||||
<td>
|
||||
<?php e($group['label'] ?? ''); ?>
|
||||
<?php if (!empty($group['is_primary'])): ?>
|
||||
<small>(<?php e(t('Primary tenant')); ?>)</small>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td>
|
||||
<?php if (!empty($departments)): ?>
|
||||
<?php e(implode(', ', $departments)); ?>
|
||||
<?php else: ?>
|
||||
-
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php else: ?>
|
||||
<p>-</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<div data-tab-panel="about">
|
||||
<?php if ($hireDateLabel !== ''): ?>
|
||||
<div>
|
||||
<small><?php e(t('Hire date')); ?></small>
|
||||
<p><?php e($hireDateLabel); ?></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if ($profileDescription !== ''): ?>
|
||||
<?php foreach (preg_split('/\\r\\n|\\r|\\n/', $profileDescription) as $line): ?>
|
||||
<?php if (trim($line) !== ''): ?>
|
||||
<p><?php e($line); ?></p>
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
<?php elseif ($hireDateLabel === ''): ?>
|
||||
<p>-</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<aside id="app-details-aside-section">
|
||||
<div class="app-details-aside-section">
|
||||
<hgroup>
|
||||
<h2><?php e(t('Contact')); ?></h2>
|
||||
<p><?php e(t('Quick actions')); ?></p>
|
||||
</hgroup>
|
||||
<hr>
|
||||
<?php if ($email !== ''): ?>
|
||||
<p><a href="mailto:<?php e($email); ?>"><?php e(t('Send email')); ?></a></p>
|
||||
<?php endif; ?>
|
||||
<?php if ($phone !== ''): ?>
|
||||
<p><a href="tel:<?php e($phone); ?>"><?php e(t('Call phone')); ?></a></p>
|
||||
<?php endif; ?>
|
||||
<?php if ($mobile !== ''): ?>
|
||||
<p><a href="tel:<?php e($mobile); ?>"><?php e(t('Call mobile')); ?></a></p>
|
||||
<?php endif; ?>
|
||||
<?php if (!$hasContact): ?>
|
||||
<p>-</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</aside>
|
||||
</div>
|
||||
@@ -9,8 +9,8 @@ use MintyPHP\Service\Org\DepartmentService;
|
||||
use MintyPHP\Service\Tenant\TenantScopeService;
|
||||
use MintyPHP\Service\Tenant\TenantService;
|
||||
use MintyPHP\Service\User\UserAccountService;
|
||||
use MintyPHP\Service\User\UserAssignmentService;
|
||||
use MintyPHP\Service\User\UserAvatarService;
|
||||
use MintyPHP\Service\User\UserProfileViewService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AddressBookServiceTest extends TestCase
|
||||
@@ -18,13 +18,13 @@ class AddressBookServiceTest extends TestCase
|
||||
public function testBuildIndexContextBuildsTenantDepartmentMapForGlobalScope(): void
|
||||
{
|
||||
$userAccountService = $this->createMock(UserAccountService::class);
|
||||
$userAssignmentService = $this->createMock(UserAssignmentService::class);
|
||||
$tenantService = $this->createMock(TenantService::class);
|
||||
$departmentService = $this->createMock(DepartmentService::class);
|
||||
$roleService = $this->createMock(RoleService::class);
|
||||
$scopeGateway = $this->createMock(TenantScopeService::class);
|
||||
$customFieldGateway = $this->createMock(UserCustomFieldValueService::class);
|
||||
$avatarService = $this->createMock(UserAvatarService::class);
|
||||
$userProfileViewService = $this->createMock(UserProfileViewService::class);
|
||||
|
||||
$scopeGateway
|
||||
->expects($this->once())
|
||||
@@ -67,13 +67,13 @@ class AddressBookServiceTest extends TestCase
|
||||
]);
|
||||
$service = new AddressBookService(
|
||||
$userAccountService,
|
||||
$userAssignmentService,
|
||||
$tenantService,
|
||||
$departmentService,
|
||||
$roleService,
|
||||
$scopeGateway,
|
||||
$customFieldGateway,
|
||||
$avatarService
|
||||
$avatarService,
|
||||
$userProfileViewService
|
||||
);
|
||||
|
||||
$context = $service->buildIndexContext(7, []);
|
||||
|
||||
Reference in New Issue
Block a user