Skip to content

Commit

Permalink
added api
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim-the-Diamond committed Jul 29, 2024
1 parent ff5bf68 commit 4acd211
Show file tree
Hide file tree
Showing 16 changed files with 485 additions and 205 deletions.
1 change: 1 addition & 0 deletions app/Providers/Filament/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function panel(Panel $panel): Panel
\Moox\Notification\NotificationPlugin::make(),

SecurityPlugin::make(),

ResetPasswordPlugin::make(),

]);
Expand Down
7 changes: 7 additions & 0 deletions app/Providers/Filament/PressPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public function panel(Panel $panel): Panel
\Moox\Training\TrainingDatePlugin::make(),
\Moox\Training\TrainingTypePlugin::make(),


\Moox\Security\SecurityPlugin::make(),
\Moox\Security\ResetPasswordPlugin::make(),

\Moox\Sync\PlatformPlugin::make(),
\Moox\Sync\SyncPlugin::make()

]);
}
}
12 changes: 12 additions & 0 deletions config/press.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@
'capabilities' => 'Subscriber',
'mm_sua_attachment_id' => '',
],

'use_api' => true,
'entities' => [
'wp_users' => [
'api' => [
'enabled' => true,
'public' => false, // false for private, true for public
'auth_type' => 'platform', // 'platform' for platform tokens or 'sanctum' for user-tied tokens
'route_only' => ['index', 'show', 'store', 'destroy', 'update'],
],
],
],
];
2 changes: 1 addition & 1 deletion packages/press/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
},
"minimum-stability": "stable",
"prefer-stable": true
}
}
12 changes: 12 additions & 0 deletions packages/press/config/press.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@
'capabilities' => 'Subscriber',
'mm_sua_attachment_id' => '',
],

'use_api' => true,
'entities' => [
'wp_users' => [
'api' => [
'enabled' => true,
'public' => true, // false for private, true for public
'auth_type' => 'platform', // 'platform' for platform tokens or 'sanctum' for user-tied tokens
'route_only' => ['index', 'show', 'create', 'destroy'],
],
],
],
];
24 changes: 24 additions & 0 deletions packages/press/routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Support\Facades\Route;
use Moox\Press\Http\Controllers\WpUserController;


if (config('press.use_api')) {
$entitiesConfig = config('press.entities.wp_users');
foreach ($entitiesConfig as $entity => $config) {
if ($config['enabled']) {
$middleware = [];
if (!$config['public']) {
if ($config['auth_type'] === 'platform') {
$middleware[] = 'auth.platformtoken';
} else {
$middleware[] = 'auth:sanctum';
}
}
Route::middleware($middleware)->group(function () use ($entity, $config) {
Route::apiResource("/$entity/wpuser", WpUserController::class)->only(config('press.entities.wp_users.api.route_only'));
});
}
}
}
20 changes: 9 additions & 11 deletions packages/press/src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function handle()
$panelsToregister = $this->getPanelProviderPath();
if (count($panelsToregister) > 0 && $panelsToregister != null) {
foreach ($panelsToregister as $panelprovider) {
$this->registerPlugins($providerPath.'/'.$panelprovider);
$this->registerPlugins($providerPath . '/' . $panelprovider);
}
} else {
$this->registerPlugins($panelsToregister[0]);
Expand Down Expand Up @@ -81,10 +81,10 @@ public function welcome(): void
public function publishConfiguration(): void
{
if (confirm('Do you wish to publish the configuration?', true)) {
if (! File::exists('config/press.php')) {
if (!File::exists('config/press.php')) {
info('Publishing Press Configuration...');
$this->callSilent('vendor:publish', ['--tag' => 'press-config']);

info('finished publishing...');
return;
}
warning('The Press config already exist. The config will not be published.');
Expand All @@ -97,7 +97,7 @@ public function publishMigrations(): void
if (Schema::hasTable('press')) {
warning('The press table already exists. The migrations will not be published.');
} else {
info('Publishing Press Migrations...');
info('Publishing Press and Sanctum Migrations...');
$this->callSilent('vendor:publish', ['--tag' => 'press-migrations']);
}
}
Expand Down Expand Up @@ -133,11 +133,11 @@ public function registerPlugins(string $providerPath): void
$newPlugins = '';

foreach ($pluginsToAdd as $plugin) {
$searchPlugin = '/'.$plugin.'/';
$searchPlugin = '/' . $plugin . '/';
if (preg_match($searchPlugin, $content)) {
warning("$plugin already registered.");
} else {
$newPlugins .= $intend.$namespace.'\\'.$plugin.$function."\n";
$newPlugins .= $intend . $namespace . '\\' . $plugin . $function . "\n";
}
}

Expand All @@ -152,14 +152,14 @@ public function registerPlugins(string $providerPath): void

$pluginsSection = " ->plugins([\n$newPlugins\n ]);";
$placeholderPattern = '/(\->authMiddleware\(\[.*?\]\))\s*\;/s';
$replacement = "$1\n".$pluginsSection;
$replacement = "$1\n" . $pluginsSection;
$newContent = preg_replace($placeholderPattern, $replacement, $content, 1);
}

File::put($providerPath, $newContent);
}
} else {
alert($providerPath.' not found. You need to add the plugins manually.');
alert($providerPath . ' not found. You need to add the plugins manually.');
}
}

Expand All @@ -177,14 +177,12 @@ public function getPanelProviderPath(): string|array
options: [...$providerNames],
default: [$providerNames[0]],
);

}
if (count($providers) == 1) {
$providerPath .= '/'.$providers[0]->getBasename();
$providerPath .= '/' . $providers[0]->getBasename();
}

return $providerPath;

}

public function finish(): void
Expand Down
109 changes: 109 additions & 0 deletions packages/press/src/Http/Controllers/WpUserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Moox\Press\Http\Controllers;

use Illuminate\Http\Request;
use Moox\Press\Models\WpUser;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Validator;
use Moox\Press\Http\Resources\WpUserResource;

class WpUserController extends Controller
{
/**
* Display a listing of the resource.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = WpUser::all();
return WpUserResource::collection($users);
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'user_login' => 'required|string|max:255',
'user_pass' => 'required|string|max:255',
'user_nicename' => 'required|string|max:255',
'user_email' => 'required|string|email|max:255',
]);

if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], 422);
}

$wpUser = new WpUser();

$wpUser->fill($request->all());

$wpUser->save();

return new WpUserResource($wpUser);
}

/**
* Display the specified resource.
*
* @param \Moox\Press\Models\WpUser $wpUser
* @return \Illuminate\Http\Response
*/
public function show($id)
{
return new WpUserResource(WpUser::findOrFail($id));
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \Moox\Press\Models\WpUser $wpUser
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$wpUser = new WpUser();
$wpUserData = $request->only($wpUser->getFillable());
$wpUserMeta = $request->except($wpUser->getFillable());


$validator = Validator::make($request->all(), [
'user_login' => 'sometimes|string|max:255',
'user_pass' => 'sometimes|string|max:255',
'user_nicename' => 'sometimes|string|max:255',
'user_email' => 'sometimes|string|email|max:255',
]);

if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], 422);
}

$wpUser = WpUser::findOrFail($id);

$wpUser->update($request->all());

return new WpUserResource($wpUser);
}

/**
* Remove the specified resource from storage.
*
* @param \Moox\Press\Models\WpUser $wpUser
* @return \Illuminate\Http\Response
*/
public function destroy($wpUser)
{
$wpUser = WpUser::findOrFail($wpUser);
$wpUser->delete();

return response()->json(['message' => 'User deleted successfully']);
}
}
19 changes: 19 additions & 0 deletions packages/press/src/Http/Middleware/PlatformTokenAuthMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Moox\Press\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class PlatformTokenAuthMiddleware
{
public function handle(Request $request, Closure $next)
{
$token = $request->header('Authorization');
// if (!$token || !config('press.api.api.model')::where('api_token', $token)->exists()) {
if (!$token) {
return response()->json(['message' => 'Unauthorized'], 401);
}
return $next($request);
}
}
30 changes: 30 additions & 0 deletions packages/press/src/Http/Resources/WpUserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Moox\Press\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;


class WpUserResource extends JsonResource
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'user_login' => $this->name,
'user_nickname' => $this->nickname,
'user_email' => $this->email,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'description' => $this->description,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
Loading

0 comments on commit 4acd211

Please sign in to comment.