Files
pricebuddy/app/Providers/AppServiceProvider.php
Jeremy Graham 56a9b482dc fix: treat stores as shared in the stores API
Stores are shared by all users, but the stores API only returned the caller's
own stores. Stores with no creator, such as Amazon AU from the seed data, were
missing from every user's results.

- GET /api/stores, filter[domain], filter[domains] and GET /api/stores/{id}
  return every store.
- The urls and products includes return only the caller's rows. The user
  include returns the creator only to the creator.
- A new StorePolicy lets any user create and edit a store. Only an admin can
  delete one, in the API (403) and in the Filament UI.
- user_id now records who created a store. The server sets it, and an edit
  does not change it. Store::creating() fills it from the logged-in user, else
  the oldest admin. Adding a URL in a job passes the user who added it.
- A migration sets existing NULL user_id values to the oldest admin.
2026-10-11 22:13:29 +11:00

125 lines
4.2 KiB
PHP

<?php
namespace App\Providers;
use App\Enums\NotificationMethods;
use App\Models\Product;
use App\Models\ProductSource;
use App\Models\Store;
use App\Models\User;
use App\Policies\ProductPolicy;
use App\Policies\ProductSourcePolicy;
use App\Policies\StorePolicy;
use App\Policies\UserPolicy;
use App\Services\Helpers\NotificationsHelper;
use App\Services\Helpers\QueueHelper;
use App\Services\Helpers\SettingsHelper;
use App\Services\Scrapers\Utf8WebScraperHttp;
use Filament\Facades\Filament;
use Filament\Navigation\MenuItem;
use Filament\Support\Facades\FilamentView;
use Filament\View\PanelsRenderHook;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Jez500\WebScraperForLaravel\WebScraperHttp;
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
// Convert scraped pages to UTF-8 before they are cached (#203).
$this->app->bind(WebScraperHttp::class, Utf8WebScraperHttp::class);
}
public function boot(): void
{
$this->registerPolicies();
$this->registerFilamentSettings();
$this->setConfigFromAppSettings();
$this->registerAbout();
$this->authorizePackages();
}
protected function registerPolicies(): void
{
Gate::policy(Product::class, ProductPolicy::class);
Gate::policy(ProductSource::class, ProductSourcePolicy::class);
Gate::policy(Store::class, StorePolicy::class);
Gate::policy(User::class, UserPolicy::class);
}
protected function registerFilamentSettings(): void
{
FilamentView::registerRenderHook(
PanelsRenderHook::HEAD_END,
fn (): string => Blade::render(
'@vite([\'resources/scss/app.scss\', \'resources/js/app.js\'])'.
'@include(\'body.js-settings\')'.
'<link rel="manifest" href="/manifest.json">'.
'<script src="/js/clipboard.js"></script>'
),
);
FilamentView::registerRenderHook(
PanelsRenderHook::SIDEBAR_FOOTER,
fn (): string => view('components.sidebar-footer', [
'content' => config('app.version', 'development'),
]),
);
Filament::registerUserMenuItems([
MenuItem::make()
->label(__('Account settings'))
->url(fn () => '/admin/users/'.auth()->id().'/edit')
->icon('heroicon-s-cog'),
]);
}
protected function setConfigFromAppSettings(): void
{
// Email.
$mail = NotificationMethods::Mail->value;
if (NotificationsHelper::isEnabled($mail)) {
config([
'mail.default' => 'smtp',
'mail.mailers.smtp.host' => NotificationsHelper::getSetting($mail, 'smtp_host'),
'mail.mailers.smtp.port' => NotificationsHelper::getSetting($mail, 'smtp_port'),
'mail.mailers.smtp.username' => NotificationsHelper::getSetting($mail, 'smtp_user'),
'mail.mailers.smtp.password' => NotificationsHelper::getSetting($mail, 'smtp_password'),
'mail.mailers.smtp.encryption' => NotificationsHelper::getSetting($mail, 'encryption') ?: null,
'mail.from.address' => NotificationsHelper::getSetting($mail, 'from_address', 'hello@example.com'),
]);
}
// Pushover.
$pushover = NotificationMethods::Pushover->value;
if (NotificationsHelper::isEnabled($pushover)) {
config([
'services.pushover.token' => NotificationsHelper::getSetting($pushover, 'token'),
]);
}
// Logging.
config([
'logging.channels.db.days' => SettingsHelper::getSetting('log_retention_days', 7),
]);
}
protected function registerAbout(): void
{
AboutCommand::add('PriceBuddy', fn () => [
'Version' => config('app.version', 'development'),
'Queue Worker' => QueueHelper::isRunning() ? 'Running' : 'Stopped',
]);
}
protected function authorizePackages(): void
{
Gate::define('viewApiDocs', function (?User $user) {
return ! is_null($user);
});
}
}