Replace the single tenant avatar with a pair of theme-scoped brand logos.
Render only the theme-matching <img> server-side and swap src on theme
toggle via a JS hook — no reload, no double request, no CSS tricks.
Tenant logos
- TenantLogoService (ImageUploadTrait) with theme whitelist and per-theme
storage storage/tenants/{uuid}/logo/{light|dark}/, SIZES 128/256/512
- Public serving endpoint auth/tenant-logo-file so login can show the
logo pre-auth; matching authenticated admin preview endpoint
- appTenantLogoUrl(?size, ?theme) with 4-step fallback cascade; PDF +
mail always request 'light'
- Admin tenant edit: avatar block replaced by "Tenant logos" details
block inside the Master-data tab, two side-by-side slots via Pico
.grid with the core app-file-upload partial
- Policy rename ABILITY_ADMIN_TENANTS_AVATAR_VIEW -> LOGO_VIEW, action
routes logo / logo-delete / logo-file with theme body/query param
- API endpoint path kept (backward compat), internals on new service
- CLI tenant:logo-migrate-avatars moves legacy avatar/ -> logo/light/
idempotently (--dry-run, --yes, --cleanup)
- i18n "Tenant image" removed, 12 new keys synced across de/en
File upload component
- Full-width preview + filename/actions below (3D stack layout)
- Fixed 16:9 aspect ratio with 1rem inner padding for consistent
preview size across any logo aspect
- Transparency checker pattern as background so black logos stay
visible on dark mode and white logos on light mode
- form="" + deleteFormId support so the partial works with barrier
forms inside another form
Buttons
- width:100% dropped from button[type="submit"]; scoped back via
.login-main for the auth-flow primary CTA
- .outline base rule now tints background via color-mix of --app-color
so secondary/primary/danger outlines all gain a subtle surface
- .outline.secondary restyled Stripe-style in both themes: solid white
chip with soft shadow in light, solid elevated dark chip with white
text in dark; neutral border replaces role-colored border
- .app-action-success/.app-action-danger outlines get color-mix bg +
theme-aware outline-text tokens for stronger contrast
- Filled .primary/.app-action-success/.app-action-danger get raised
box-shadow (inset highlight + drop) — opt-in via class so chrome
buttons stay flat
- Dropped the legacy .secondary utility that was clobbering the
custom-property cascade with a hardcoded muted color
Theme swap
- Logo img carries data-src-light + data-src-dark; theme-toggle JS
swaps src when data-theme changes, keeping the topbar/login logo in
sync without a page reload
Quality gates: PHPUnit (2045), PHPStan L5, CS-Fixer, docs link/drift,
codex skills sync — all green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
183 lines
5.6 KiB
PHP
183 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace MintyPHP\Service\Tenant;
|
|
|
|
use MintyPHP\Service\Image\ImageUploadTrait;
|
|
|
|
/**
|
|
* Tenant-scoped brand logo with per-theme variants (light + dark).
|
|
*
|
|
* Storage layout: storage/tenants/{uuid}/logo/{light|dark}/
|
|
* Each theme directory holds one original file plus three resized variants
|
|
* (128/256/512). Consumers that lack a theme context (PDF, e-mail) always ask
|
|
* for 'light' — this is the by-design invariant.
|
|
*
|
|
* The theme parameter is whitelisted on every public method; invalid values
|
|
* short-circuit to empty results so callers never need to repeat the check.
|
|
*
|
|
* @api
|
|
*/
|
|
class TenantLogoService
|
|
{
|
|
use ImageUploadTrait;
|
|
|
|
public const THEME_LIGHT = 'light';
|
|
public const THEME_DARK = 'dark';
|
|
|
|
/** @var list<string> */
|
|
public const THEMES = [self::THEME_LIGHT, self::THEME_DARK];
|
|
|
|
private const MAX_SIZE = 5242880; // 5 MB
|
|
private const SIZES = [128, 256, 512];
|
|
private const DEFAULT_SIZE = 256;
|
|
|
|
public function isValidUuid(string $uuid): bool
|
|
{
|
|
return self::imageIsValidUuid($uuid);
|
|
}
|
|
|
|
public function isValidTheme(string $theme): bool
|
|
{
|
|
return in_array($theme, self::THEMES, true);
|
|
}
|
|
|
|
public function storageBase(): string
|
|
{
|
|
return self::imageStorageBase();
|
|
}
|
|
|
|
public function tenantLogoDir(string $uuid, string $theme): string
|
|
{
|
|
return $this->storageBase() . '/tenants/' . $uuid . '/logo/' . $theme;
|
|
}
|
|
|
|
public function findLogoPath(string $uuid, string $theme, ?int $size = null): ?string
|
|
{
|
|
if (!$this->isValidUuid($uuid) || !$this->isValidTheme($theme)) {
|
|
return null;
|
|
}
|
|
$dir = $this->tenantLogoDir($uuid, $theme);
|
|
if (!is_dir($dir)) {
|
|
return null;
|
|
}
|
|
if ($size) {
|
|
$size = $this->normalizeSize($size);
|
|
$variant = $this->findVariantPath($dir, $size);
|
|
if ($variant) {
|
|
return $variant;
|
|
}
|
|
}
|
|
$defaultVariant = $this->findVariantPath($dir, self::DEFAULT_SIZE);
|
|
if ($defaultVariant) {
|
|
return $defaultVariant;
|
|
}
|
|
$original = self::imageFindOriginalPath($dir);
|
|
return $original ?: null;
|
|
}
|
|
|
|
public function hasLogo(string $uuid, string $theme): bool
|
|
{
|
|
$path = $this->findLogoPath($uuid, $theme);
|
|
return $path ? is_file($path) : false;
|
|
}
|
|
|
|
public function delete(string $uuid, string $theme): bool
|
|
{
|
|
if (!$this->isValidUuid($uuid) || !$this->isValidTheme($theme)) {
|
|
return false;
|
|
}
|
|
$dir = $this->tenantLogoDir($uuid, $theme);
|
|
if (!is_dir($dir)) {
|
|
return true;
|
|
}
|
|
$matches = array_merge(
|
|
glob($dir . '/logo-*.*') ?: [],
|
|
glob($dir . '/logo.*') ?: [],
|
|
glob($dir . '/original.*') ?: []
|
|
);
|
|
foreach ($matches as $file) {
|
|
if (is_file($file)) {
|
|
@unlink($file);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function saveUpload(string $uuid, string $theme, array $file): array
|
|
{
|
|
if (!$this->isValidUuid($uuid)) {
|
|
return ['ok' => false, 'error' => t('Tenant not found')];
|
|
}
|
|
if (!$this->isValidTheme($theme)) {
|
|
return ['ok' => false, 'error' => t('Invalid theme')];
|
|
}
|
|
if (empty($file) || !isset($file['tmp_name'])) {
|
|
return ['ok' => false, 'error' => t('No file uploaded')];
|
|
}
|
|
if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
|
|
return ['ok' => false, 'error' => t('Upload failed')];
|
|
}
|
|
if (($file['size'] ?? 0) > self::MAX_SIZE) {
|
|
return ['ok' => false, 'error' => t('File is too large')];
|
|
}
|
|
|
|
$tmpPath = $file['tmp_name'];
|
|
$mime = $this->detectMime($tmpPath);
|
|
$isSvg = self::imageIsSvgUpload($mime, $tmpPath);
|
|
$ext = self::imageExtensionForMime($mime, $isSvg);
|
|
if (!$ext) {
|
|
return ['ok' => false, 'error' => t('Invalid image file')];
|
|
}
|
|
if ($isSvg && !self::imageIsSafeSvg($tmpPath)) {
|
|
return ['ok' => false, 'error' => t('Invalid image file')];
|
|
}
|
|
|
|
$dir = $this->tenantLogoDir($uuid, $theme);
|
|
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
|
|
return ['ok' => false, 'error' => t('Upload failed')];
|
|
}
|
|
|
|
$this->delete($uuid, $theme);
|
|
$originalPath = $dir . '/original.' . $ext;
|
|
if (!move_uploaded_file($tmpPath, $originalPath)) {
|
|
return ['ok' => false, 'error' => t('Upload failed')];
|
|
}
|
|
@chmod($originalPath, 0644);
|
|
|
|
$variantExt = function_exists('imagewebp') ? 'webp' : 'jpg';
|
|
if (!$isSvg && self::imageCanResize()) {
|
|
foreach (self::SIZES as $size) {
|
|
$target = $dir . '/logo-' . $size . '.' . $variantExt;
|
|
self::imageResizeAndFit($originalPath, $target, $size, $size, $variantExt);
|
|
}
|
|
}
|
|
|
|
return ['ok' => true, 'path' => $originalPath, 'mime' => $mime];
|
|
}
|
|
|
|
public function detectMime(string $path): string
|
|
{
|
|
return self::imageDetectMime($path);
|
|
}
|
|
|
|
private function normalizeSize(int $size): int
|
|
{
|
|
if (in_array($size, self::SIZES, true)) {
|
|
return $size;
|
|
}
|
|
return self::DEFAULT_SIZE;
|
|
}
|
|
|
|
private function findVariantPath(string $dir, int $size): ?string
|
|
{
|
|
$matches = glob($dir . '/logo-' . $size . '.*');
|
|
if (!$matches) {
|
|
return null;
|
|
}
|
|
usort($matches, static function ($a, $b) {
|
|
return filemtime($b) <=> filemtime($a);
|
|
});
|
|
return $matches[0];
|
|
}
|
|
}
|