-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathPluginAccessController.php
More file actions
195 lines (165 loc) · 5.66 KB
/
PluginAccessController.php
File metadata and controls
195 lines (165 loc) · 5.66 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
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Plugin;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class PluginAccessController extends Controller
{
/**
* Validate user credentials and return accessible plugins.
*
* Expects HTTP Basic Auth with:
* - Username: user's email address
* - Password: user's plugin_license_key
*/
public function index(Request $request): JsonResponse
{
$email = $request->getUser();
$licenseKey = $request->getPassword();
if (! $email || ! $licenseKey) {
return response()->json([
'error' => 'Authentication required',
'message' => 'Please provide email and license key via HTTP Basic Auth',
], 401);
}
$user = User::where('email', $email)
->where('plugin_license_key', $licenseKey)
->first();
if (! $user) {
return response()->json([
'error' => 'Invalid credentials',
'message' => 'The provided email or license key is incorrect',
], 401);
}
$accessiblePlugins = $this->getAccessiblePlugins($user);
return response()->json([
'success' => true,
'user' => [
'email' => $user->email,
],
'plugins' => $accessiblePlugins,
]);
}
/**
* Check if user has access to a specific plugin.
*/
public function checkAccess(Request $request, string $vendor, string $package): JsonResponse
{
$email = $request->getUser();
$licenseKey = $request->getPassword();
if (! $email || ! $licenseKey) {
return response()->json([
'error' => 'Authentication required',
], 401);
}
$user = User::where('email', $email)
->where('plugin_license_key', $licenseKey)
->first();
if (! $user) {
return response()->json([
'error' => 'Invalid credentials',
], 401);
}
$packageName = "{$vendor}/{$package}";
$plugin = Plugin::where('name', $packageName)->first();
if (! $plugin) {
return response()->json([
'error' => 'Plugin not found',
], 404);
}
$hasAccess = $user->hasPluginAccess($plugin);
return response()->json([
'success' => true,
'package' => $packageName,
'has_access' => $hasAccess,
]);
}
/**
* Get all paid plugins the user has access to (submitted or purchased).
*
* @return array<int, array{name: string, access: string}>
*/
protected function getAccessiblePlugins(User $user): array
{
$plugins = [];
// Admins have access to ALL paid plugins (including pending) for review
if ($user->isAdmin()) {
$allPaidPlugins = Plugin::query()
->where('type', \App\Enums\PluginType::Paid)
->whereNotNull('name')
->get(['name', 'status']);
foreach ($allPaidPlugins as $plugin) {
$plugins[] = [
'name' => $plugin->name,
'access' => 'admin',
];
}
return $plugins;
}
// Paid plugins the user has submitted
$submittedPlugins = Plugin::query()
->where('user_id', $user->id)
->where('type', \App\Enums\PluginType::Paid)
->get(['name']);
foreach ($submittedPlugins as $plugin) {
$plugins[] = [
'name' => $plugin->name,
'access' => 'author',
];
}
// Paid plugins the user has purchased (has licenses for)
$licensedPlugins = $user->pluginLicenses()
->active()
->with('plugin:id,name')
->get()
->pluck('plugin')
->filter()
->unique('id');
foreach ($licensedPlugins as $plugin) {
// Avoid duplicates if user is also the author
if (! collect($plugins)->contains('name', $plugin->name)) {
$plugins[] = [
'name' => $plugin->name,
'access' => 'purchased',
];
}
}
// Team members get access to official plugins and owner's purchased plugins
if ($user->isUltraTeamMember()) {
$officialPlugins = Plugin::query()
->where('type', \App\Enums\PluginType::Paid)
->where('is_official', true)
->whereNotNull('name')
->get(['name']);
foreach ($officialPlugins as $plugin) {
if (! collect($plugins)->contains('name', $plugin->name)) {
$plugins[] = [
'name' => $plugin->name,
'access' => 'team',
];
}
}
}
$teamOwner = $user->getTeamOwner();
if ($teamOwner) {
$teamPlugins = $teamOwner->pluginLicenses()
->active()
->with('plugin:id,name')
->get()
->pluck('plugin')
->filter()
->unique('id');
foreach ($teamPlugins as $plugin) {
if (! collect($plugins)->contains('name', $plugin->name)) {
$plugins[] = [
'name' => $plugin->name,
'access' => 'team',
];
}
}
}
return $plugins;
}
}