-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathPluginDirectoryController.php
More file actions
103 lines (83 loc) · 3.17 KB
/
PluginDirectoryController.php
File metadata and controls
103 lines (83 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace App\Http\Controllers;
use App\Models\Plugin;
use App\Models\PluginBundle;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
class PluginDirectoryController extends Controller
{
public function index(): View
{
$user = Auth::user();
$featuredPlugins = Plugin::query()
->approved()
->where('is_active', true)
->featured()
->latest()
->take(16)
->get()
->filter(fn (Plugin $plugin) => $plugin->isFree() || $plugin->hasAccessiblePriceFor($user))
->take(8);
$latestPlugins = Plugin::query()
->approved()
->where('is_active', true)
->where('featured', false)
->latest()
->take(16)
->get()
->filter(fn (Plugin $plugin) => $plugin->isFree() || $plugin->hasAccessiblePriceFor($user))
->take(8);
$bundles = PluginBundle::query()
->active()
->with('plugins')
->latest()
->get()
->filter(fn (PluginBundle $bundle) => $bundle->hasAccessiblePriceFor($user));
return view('plugins', [
'featuredPlugins' => $featuredPlugins,
'latestPlugins' => $latestPlugins,
'bundles' => $bundles,
]);
}
public function show(string $vendor, string $package): View
{
$plugin = Plugin::findByVendorPackageOrFail($vendor, $package);
$user = Auth::user();
$isAdmin = $user?->isAdmin() ?? false;
$isOwner = $user && $plugin->user_id === $user->id;
abort_unless(($plugin->isApproved() && $plugin->is_active) || $isAdmin || $isOwner, 404);
// For paid plugins, check if user has an accessible price (admins and owners bypass)
if (! $isAdmin && ! $isOwner && $plugin->isPaid() && ! $plugin->hasAccessiblePriceFor($user)) {
abort(404);
}
$bundles = $plugin->bundles()
->active()
->get()
->filter(fn (PluginBundle $bundle) => $bundle->hasAccessiblePriceFor($user));
$bestPrice = $plugin->getBestPriceForUser($user);
$regularPrice = $plugin->getRegularPrice();
return view('plugin-show', [
'plugin' => $plugin,
'bundles' => $bundles,
'bestPrice' => $bestPrice,
'regularPrice' => $regularPrice,
'hasDiscount' => $bestPrice && $regularPrice && $bestPrice->id !== $regularPrice->id,
'isAdminPreview' => (! $plugin->isApproved() || ! $plugin->is_active) && ($isAdmin || $isOwner),
]);
}
public function license(string $vendor, string $package): View
{
$plugin = Plugin::findByVendorPackageOrFail($vendor, $package);
abort_unless($plugin->isApproved(), 404);
abort_unless($plugin->isPaid(), 404);
abort_unless($plugin->license_html, 404);
$user = Auth::user();
// For paid plugins, check if user has an accessible price
if (! $plugin->hasAccessiblePriceFor($user)) {
abort(404);
}
return view('plugin-license', [
'plugin' => $plugin,
]);
}
}