-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hanif Dwy Putra S <[email protected]>
- Loading branch information
1 parent
270cc92
commit 8f2f689
Showing
5 changed files
with
109 additions
and
3 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
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); | ||
} | ||
} | ||
} | ||
?> |
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,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); | ||
} | ||
} | ||
} | ||
?> |
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,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); | ||
} | ||
} | ||
} |
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