-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff5bf68
commit 4acd211
Showing
16 changed files
with
485 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,4 @@ | |
}, | ||
"minimum-stability": "stable", | ||
"prefer-stable": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/press/src/Http/Controllers/WpUserController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
packages/press/src/Http/Middleware/PlatformTokenAuthMiddleware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
Oops, something went wrong.