Skip to content

Commit

Permalink
I don't know what I'm doin
Browse files Browse the repository at this point in the history
Signed-off-by: Hanif Dwy Putra S <[email protected]>
  • Loading branch information
hansputera committed Apr 15, 2023
1 parent 270cc92 commit 8f2f689
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/Console/Commands/CreateAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function handle()
$user->update([
'password' => Hash::make($password),
'email' => $email,
'status' => 1,
'status' => 2,
]);
$this->info('Done overwrite');
$this->info('Password: ' . $password);
Expand All @@ -60,7 +60,7 @@ public function handle()

$user->email = $email;
$user->password = Hash::make($password);
$user->status = 1;
$user->status = 2;
$user->username = $this->argument('username');

$user->save();
Expand Down
34 changes: 34 additions & 0 deletions app/Http/Controllers/Api/Users/DeleteUserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace App\Http\Controllers\Api\Users;

use App\Http\Controllers\ApiController;
use App\Models\User;
use Illuminate\Http\Request;

class DeleteUserController extends ApiController
{
public function delete(Request $request, string $user_id)
{
$user = User::find($user_id);
if (!isset($user)) {
return response()->json([
'errors' => ['_' => 'user doesn\'t exist']
], 400);
} else if ($user->id === $request->user()->id) {
return response()->json([
'errors' => ['_' => 'you couldn\'t delete yourself'],
], 400);
}

if (User::destroy($user->id)) {
return response()->json([
'data' => $user,
], 200);
} else {
return response()->json([
'errors' => ['_' => 'couldn\'t delete this user'],
], 400);
}
}
}
?>
26 changes: 26 additions & 0 deletions app/Http/Controllers/Api/Users/ShowUserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace App\Http\Controllers\Api\Users;

use App\Http\Controllers\ApiController;
use App\Models\User;
use Illuminate\Http\Request;

class ShowUserController extends ApiController
{
public function show(Request $request, string $user_id)
{
$self = $request->user();

if (strval($self->id) === $user_id || $self->status === 2) {
$self = User::find($user_id);
return response()->json([
'data' => $self,
], isset($self) ? 200 : 404);
} else {
return response()->json([
'errors' => ['_' => 'user doesn\'t exist'],
], 404);
}
}
}
?>
28 changes: 28 additions & 0 deletions app/Http/Middleware/OnlyFAUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class OnlyFAUser
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$user = $request->user();

if (isset($user) && $user->status === 2) {
return $next($request);
} else {
return response()->json([
'errors' => ['_' => 'Unauthorized'],
], 401);
}
}
}
20 changes: 19 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
use App\Http\Controllers\Api\Auth\LoginController;
use App\Http\Controllers\Api\Auth\ProfileController;
use App\Http\Controllers\Api\Auth\RegisterController;
use App\Http\Controllers\Api\Users\DeleteUserController;
use App\Http\Controllers\Api\Users\ShowUserController;
use App\Http\Middleware\JwtLogged;
use App\Http\Middleware\OnlyActiveUser;
use App\Http\Middleware\OnlyFAUser;
use Illuminate\Support\Facades\Route;

/*
Expand All @@ -18,16 +21,31 @@
|
*/

// /api/auth
Route::group([
'middleware' => 'api',
'prefix' => 'auth'
], function() {
Route::post('register', [RegisterController::class, 'register'])->middleware([
JwtLogged::class,
OnlyActiveUser::class,
OnlyFAUser::class,
]);
Route::post('login', [LoginController::class, 'login'])->middleware('guest');
Route::get('profile', [ProfileController::class, 'self'])->middleware([
JwtLogged::class,
]);
});

// /api/users
Route::group([
'middleware' => 'api',
'prefix' => 'users',
], function() {
Route::delete('{id}', [DeleteUserController::class, 'delete'])->middleware([
JwtLogged::class,
OnlyFAUser::class,
]);
Route::get('{id}', [ShowUserController::class, 'show'])->middleware([
JwtLogged::class,
]);
});

0 comments on commit 8f2f689

Please sign in to comment.