-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathCustomerPluginController.php
More file actions
290 lines (228 loc) · 9.84 KB
/
CustomerPluginController.php
File metadata and controls
290 lines (228 loc) · 9.84 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
namespace App\Http\Controllers;
use App\Enums\PluginStatus;
use App\Features\AllowPaidPlugins;
use App\Http\Requests\SubmitPluginRequest;
use App\Http\Requests\UpdatePluginDescriptionRequest;
use App\Http\Requests\UpdatePluginLogoRequest;
use App\Jobs\ReviewPluginRepository;
use App\Models\Plugin;
use App\Notifications\PluginSubmitted;
use App\Services\GitHubUserService;
use App\Services\PluginSyncService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Laravel\Pennant\Feature;
class CustomerPluginController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index(): View
{
$user = Auth::user();
$plugins = $user->plugins()->orderBy('created_at', 'desc')->get();
$developerAccount = $user->developerAccount;
return view('customer.plugins.index', compact('plugins', 'developerAccount'));
}
public function create(): View
{
$developerAccount = Auth::user()->developerAccount;
return view('customer.plugins.create', [
'hasAcceptedTerms' => $developerAccount?->hasAcceptedCurrentTerms() ?? false,
'hasCompletedOnboarding' => $developerAccount?->hasCompletedOnboarding() ?? false,
]);
}
public function store(SubmitPluginRequest $request, PluginSyncService $syncService): RedirectResponse
{
$user = Auth::user();
// Require developer onboarding and terms acceptance for all plugin submissions
$developerAccount = $user->developerAccount;
if (! $developerAccount || ! $developerAccount->hasAcceptedCurrentTerms()) {
return to_route('customer.developer.onboarding')
->with('message', 'You must accept the Plugin Developer Terms and Conditions before submitting a plugin.');
}
// Reject paid plugin submissions if the feature is disabled
if ($request->type === 'paid' && ! Feature::active(AllowPaidPlugins::class)) {
return to_route('customer.plugins.create')
->with('error', 'Paid plugin submissions are not currently available.');
}
// For paid plugins, link to the user's developer account if they have one
$developerAccountId = null;
if ($request->type === 'paid' && $user->developerAccount) {
$developerAccountId = $user->developerAccount->id;
}
// Build the full repository URL from vendor/repo format
$repository = trim($request->repository, '/');
$repositoryUrl = 'https://github.com/'.$repository;
[$owner, $repo] = explode('/', $repository);
$plugin = $user->plugins()->create([
'repository_url' => $repositoryUrl,
'type' => $request->type,
'status' => PluginStatus::Pending,
'developer_account_id' => $developerAccountId,
]);
$webhookSecret = $plugin->generateWebhookSecret();
// Attempt to automatically install the webhook if the user has a GitHub token
$webhookInstalled = false;
if ($user->hasGitHubToken()) {
$githubService = GitHubUserService::for($user);
$webhookResult = $githubService->createWebhook(
$owner,
$repo,
$plugin->getWebhookUrl(),
$webhookSecret
);
$webhookInstalled = $webhookResult['success'];
}
$plugin->update(['webhook_installed' => $webhookInstalled]);
$syncService->sync($plugin);
(new ReviewPluginRepository($plugin))->handle();
if (! $plugin->name) {
$plugin->delete();
return to_route('customer.plugins.create')
->with('error', 'Could not find a valid composer.json in the repository. Please ensure your repository contains a composer.json with a valid package name.');
}
// Check if the vendor namespace is available for this user
$namespace = $plugin->getVendorNamespace();
if ($namespace && ! Plugin::isNamespaceAvailableForUser($namespace, $user->id)) {
$plugin->delete();
$errorMessage = Plugin::isReservedNamespace($namespace)
? "The namespace '{$namespace}' is reserved and cannot be used for plugin submissions."
: "The namespace '{$namespace}' is already claimed by another user. You cannot submit plugins under this namespace.";
return to_route('customer.plugins.create')
->with('error', $errorMessage);
}
$user->notify(new PluginSubmitted($plugin));
$successMessage = 'Your plugin has been submitted for review!';
if (! $webhookInstalled) {
$successMessage .= ' Please set up the webhook manually to enable automatic syncing.';
}
return to_route('customer.plugins.show', $this->pluginRouteParams($plugin))
->with('success', $successMessage);
}
public function show(string $vendor, string $package): View
{
$plugin = Plugin::findByVendorPackageOrFail($vendor, $package);
$user = Auth::user();
if ($plugin->user_id !== $user->id) {
abort(403);
}
return view('customer.plugins.show', compact('plugin'));
}
public function update(UpdatePluginDescriptionRequest $request, string $vendor, string $package): RedirectResponse
{
$plugin = Plugin::findByVendorPackageOrFail($vendor, $package);
$user = Auth::user();
if ($plugin->user_id !== $user->id) {
abort(403);
}
$plugin->updateDescription($request->description, $user->id);
return to_route('customer.plugins.show', $this->pluginRouteParams($plugin))
->with('success', 'Plugin description updated successfully!');
}
public function resubmit(string $vendor, string $package): RedirectResponse
{
$plugin = Plugin::findByVendorPackageOrFail($vendor, $package);
$user = Auth::user();
// Ensure the plugin belongs to the current user
if ($plugin->user_id !== $user->id) {
abort(403);
}
// Only rejected plugins can be resubmitted
if (! $plugin->isRejected()) {
return to_route('customer.plugins.index')
->with('error', 'Only rejected plugins can be resubmitted.');
}
$plugin->resubmit();
return to_route('customer.plugins.index')
->with('success', 'Your plugin has been resubmitted for review!');
}
public function updateLogo(UpdatePluginLogoRequest $request, string $vendor, string $package): RedirectResponse
{
$plugin = Plugin::findByVendorPackageOrFail($vendor, $package);
$user = Auth::user();
if ($plugin->user_id !== $user->id) {
abort(403);
}
if ($plugin->logo_path) {
Storage::disk('public')->delete($plugin->logo_path);
}
$path = $request->file('logo')->store('plugin-logos', 'public');
// Clear gradient/icon when uploading a custom logo
$plugin->update([
'logo_path' => $path,
'icon_gradient' => null,
'icon_name' => null,
]);
return to_route('customer.plugins.show', $this->pluginRouteParams($plugin))
->with('success', 'Plugin logo updated successfully!');
}
public function updateIcon(Request $request, string $vendor, string $package): RedirectResponse
{
$plugin = Plugin::findByVendorPackageOrFail($vendor, $package);
$user = Auth::user();
if ($plugin->user_id !== $user->id) {
abort(403);
}
$validated = $request->validate([
'icon_gradient' => ['required', 'string', 'in:'.implode(',', array_keys(Plugin::gradientPresets()))],
'icon_name' => ['required', 'string', 'max:100', 'regex:/^[a-z0-9-]+$/'],
]);
// Clear custom logo when using gradient icon
if ($plugin->logo_path) {
Storage::disk('public')->delete($plugin->logo_path);
}
$plugin->update([
'logo_path' => null,
'icon_gradient' => $validated['icon_gradient'],
'icon_name' => $validated['icon_name'],
]);
return to_route('customer.plugins.show', $this->pluginRouteParams($plugin))
->with('success', 'Plugin icon updated successfully!');
}
public function deleteLogo(string $vendor, string $package): RedirectResponse
{
$plugin = Plugin::findByVendorPackageOrFail($vendor, $package);
$user = Auth::user();
if ($plugin->user_id !== $user->id) {
abort(403);
}
if ($plugin->logo_path) {
Storage::disk('public')->delete($plugin->logo_path);
}
$plugin->update([
'logo_path' => null,
'icon_gradient' => null,
'icon_name' => null,
]);
return to_route('customer.plugins.show', $this->pluginRouteParams($plugin))
->with('success', 'Plugin icon removed successfully!');
}
/**
* Get route parameters for a plugin's vendor/package URL.
*
* @return array{vendor: string, package: string}
*/
protected function pluginRouteParams(Plugin $plugin): array
{
[$vendor, $package] = explode('/', $plugin->name);
return ['vendor' => $vendor, 'package' => $package];
}
public function updateDisplayName(): RedirectResponse
{
$user = Auth::user();
$validated = request()->validate([
'display_name' => ['nullable', 'string', 'max:255'],
]);
$user->update([
'display_name' => $validated['display_name'] ?: null,
]);
return to_route('customer.plugins.index')
->with('success', 'Display name updated successfully!');
}
}