Skip to content

Commit 8c8db0d

Browse files
committed
Feature/wpuserapi (#575)
1 parent d828afe commit 8c8db0d

File tree

10 files changed

+411
-184
lines changed

10 files changed

+411
-184
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
},
3535
"minimum-stability": "stable",
3636
"prefer-stable": true
37-
}
37+
}

config/press.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,16 @@
3333
'capabilities' => 'Subscriber',
3434
'mm_sua_attachment_id' => '',
3535
],
36+
37+
'use_api' => true,
38+
'entities' => [
39+
'wp_users' => [
40+
'api' => [
41+
'enabled' => true,
42+
'public' => true, // false for private, true for public
43+
'auth_type' => 'platform', // 'platform' for platform tokens or 'sanctum' for user-tied tokens
44+
'route_only' => ['index', 'show', 'create', 'destroy'],
45+
],
46+
],
47+
],
3648
];

routes/api.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Route;
4+
use Moox\Press\Http\Controllers\WpUserController;
5+
6+
if (config('press.use_api')) {
7+
$entitiesConfig = config('press.entities.wp_users');
8+
foreach ($entitiesConfig as $entity => $config) {
9+
if ($config['enabled']) {
10+
$middleware = [];
11+
if (! $config['public']) {
12+
if ($config['auth_type'] === 'platform') {
13+
$middleware[] = 'auth.platformtoken';
14+
} else {
15+
$middleware[] = 'auth:sanctum';
16+
}
17+
}
18+
Route::middleware($middleware)->group(function () use ($entity) {
19+
Route::apiResource("/$entity/wpuser", WpUserController::class)->only(config('press.entities.wp_users.api.route_only'));
20+
});
21+
}
22+
}
23+
}

src/Commands/InstallCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function publishConfiguration(): void
8484
if (! File::exists('config/press.php')) {
8585
info('Publishing Press Configuration...');
8686
$this->callSilent('vendor:publish', ['--tag' => 'press-config']);
87+
info('finished publishing...');
8788

8889
return;
8990
}
@@ -97,7 +98,7 @@ public function publishMigrations(): void
9798
if (Schema::hasTable('press')) {
9899
warning('The press table already exists. The migrations will not be published.');
99100
} else {
100-
info('Publishing Press Migrations...');
101+
info('Publishing Press and Sanctum Migrations...');
101102
$this->callSilent('vendor:publish', ['--tag' => 'press-migrations']);
102103
}
103104
}
@@ -177,14 +178,12 @@ public function getPanelProviderPath(): string|array
177178
options: [...$providerNames],
178179
default: [$providerNames[0]],
179180
);
180-
181181
}
182182
if (count($providers) == 1) {
183183
$providerPath .= '/'.$providers[0]->getBasename();
184184
}
185185

186186
return $providerPath;
187-
188187
}
189188

190189
public function finish(): void
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Moox\Press\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Routing\Controller;
7+
use Illuminate\Support\Facades\Validator;
8+
use Moox\Press\Http\Resources\WpUserResource;
9+
use Moox\Press\Models\WpUser;
10+
11+
class WpUserController extends Controller
12+
{
13+
/**
14+
* Display a listing of the resource.
15+
*
16+
* @param \Illuminate\Http\Request $request
17+
* @return \Illuminate\Http\Response
18+
*/
19+
public function index()
20+
{
21+
$users = WpUser::all();
22+
23+
return WpUserResource::collection($users);
24+
}
25+
26+
/**
27+
* Store a newly created resource in storage.
28+
*
29+
* @return \Illuminate\Http\Response
30+
*/
31+
public function store(Request $request)
32+
{
33+
$validator = Validator::make($request->all(), [
34+
'user_login' => 'required|string|max:255',
35+
'user_pass' => 'required|string|max:255',
36+
'user_nicename' => 'required|string|max:255',
37+
'user_email' => 'required|string|email|max:255',
38+
]);
39+
40+
if ($validator->fails()) {
41+
return response()->json(['errors' => $validator->errors()], 422);
42+
}
43+
44+
$wpUser = new WpUser;
45+
46+
$wpUser->fill($request->all());
47+
48+
$wpUser->save();
49+
50+
return new WpUserResource($wpUser);
51+
}
52+
53+
/**
54+
* Display the specified resource.
55+
*
56+
* @param \Moox\Press\Models\WpUser $wpUser
57+
* @return \Illuminate\Http\Response
58+
*/
59+
public function show($id)
60+
{
61+
return new WpUserResource(WpUser::findOrFail($id));
62+
}
63+
64+
/**
65+
* Update the specified resource in storage.
66+
*
67+
* @param \Moox\Press\Models\WpUser $wpUser
68+
* @return \Illuminate\Http\Response
69+
*/
70+
public function update(Request $request, $id)
71+
{
72+
$wpUser = new WpUser;
73+
$wpUserData = $request->only($wpUser->getFillable());
74+
$wpUserMeta = $request->except($wpUser->getFillable());
75+
76+
$validator = Validator::make($request->all(), [
77+
'user_login' => 'sometimes|string|max:255',
78+
'user_pass' => 'sometimes|string|max:255',
79+
'user_nicename' => 'sometimes|string|max:255',
80+
'user_email' => 'sometimes|string|email|max:255',
81+
]);
82+
83+
if ($validator->fails()) {
84+
return response()->json(['errors' => $validator->errors()], 422);
85+
}
86+
87+
$wpUser = WpUser::findOrFail($id);
88+
89+
$wpUser->fill($request->all());
90+
$wpUser->save();
91+
92+
return new WpUserResource($wpUser);
93+
}
94+
95+
/**
96+
* Remove the specified resource from storage.
97+
*
98+
* @param \Moox\Press\Models\WpUser $wpUser
99+
* @return \Illuminate\Http\Response
100+
*/
101+
public function destroy($wpUser)
102+
{
103+
$wpUser = WpUser::findOrFail($wpUser);
104+
$wpUser->delete();
105+
106+
return response()->json(['message' => 'User deleted successfully']);
107+
}
108+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Moox\Press\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
8+
class PlatformTokenAuthMiddleware
9+
{
10+
public function handle(Request $request, Closure $next)
11+
{
12+
$token = $request->header('Authorization');
13+
// if (!$token || !config('press.api.api.model')::where('api_token', $token)->exists()) {
14+
if (! $token) {
15+
return response()->json(['message' => 'Unauthorized'], 401);
16+
}
17+
18+
return $next($request);
19+
}
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Moox\Press\Http\Resources;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
8+
class WpUserResource extends JsonResource
9+
{
10+
/**
11+
* Transform the resource collection into an array.
12+
*
13+
* @return array<int|string, mixed>
14+
*/
15+
public function toArray(Request $request): array
16+
{
17+
return [
18+
'id' => $this->id,
19+
'user_login' => $this->name,
20+
'user_nickname' => $this->nickname,
21+
'user_email' => $this->email,
22+
'first_name' => $this->first_name,
23+
'last_name' => $this->last_name,
24+
'description' => $this->description,
25+
'created_at' => $this->created_at,
26+
'updated_at' => $this->updated_at,
27+
];
28+
}
29+
}

0 commit comments

Comments
 (0)