70 lines
2.6 KiB
PHP
70 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
class UserManagementController extends Controller
|
|
{
|
|
protected $apiBaseUrl = 'http://192.168.100.6:8081/api'; // Same as in AuthController
|
|
|
|
/**
|
|
* Display the user management page with user data
|
|
*/
|
|
public function index()
|
|
{
|
|
try {
|
|
// Fetch the access token from the session
|
|
$user = Session::get('user');
|
|
$accessToken = $user['access_token'] ?? null;
|
|
|
|
if (!$accessToken) {
|
|
Log::info('No access token found, redirecting to login from user-management');
|
|
return redirect()->route('login')->with('error', 'Please log in to view user management.');
|
|
}
|
|
|
|
// Make the API call to fetch admin users
|
|
$response = Http::withHeaders([
|
|
'Accept' => 'application/json',
|
|
'Authorization' => 'Bearer ' . $accessToken,
|
|
])->get("{$this->apiBaseUrl}/cms/admin");
|
|
|
|
$json = $response->json();
|
|
|
|
Log::info('User Management API Response: ', $json);
|
|
|
|
if ($response->successful() && isset($json['data']) && is_array($json['data'])) {
|
|
// Transform the API response into the format expected by the table component
|
|
$users = array_map(function ($admin) {
|
|
return [
|
|
|
|
'username' => $admin['username'],
|
|
'firstName' => $admin['firstname'],
|
|
'lastName' => $admin['lastname'],
|
|
'role' => 'Admin', // Adjust if the API provides role data
|
|
'email' => $admin['email'],
|
|
// 'status' => $admin['is_active'] ? 'Active' : 'Inactive',
|
|
];
|
|
}, $json['data']);
|
|
|
|
// Pass the transformed data to the view
|
|
return view('pages.user-management', [
|
|
'users' => $users,
|
|
]);
|
|
} else {
|
|
Log::warning('No user data found or invalid API response: ', $json);
|
|
return view('pages.user-management', [
|
|
'users' => [], // Pass an empty array if no data
|
|
]);
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching user data: ' . $e->getMessage());
|
|
return view('pages.user-management', [
|
|
'users' => [], // Pass an empty array on error
|
|
]);
|
|
}
|
|
}
|
|
} |