Authenticated successfully. Access Token:
I have successfully authenticated the access token, but fetching data have a problem
Failed to fetch employees. Response: {“result”:null,”targetUrl”:null,”success”:false,”error”:{“code”:0,”message”:”Required permissions are not granted. At least one of these permissions must be granted: H r 2 0 1 employees”,”details”:null,”validationErrors”:null},”unAuthorizedRequest”:true,”__abp”:true}
here is my EmployeeController.php (controller):
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class EmployeeController extends CI_Controller {
public function __construct() {
parent::__construct();
// Load the session library
$this->load->library('session');
}
// Function to authenticate the user and get the access token
public function authenticate() {
$url = 'http://172.16.0.177:5101/api/TokenAuth/Authenticate';
$data = json_encode(array(
'userNameOrEmailAddress' => 'myusernamehere',
'password' => 'mypasswordhere',
'twoFactorVerificationCode' => '',
'rememberClient' => true,
'twoFactorRememberClientToken' => '',
'singleSignIn' => true,
'returnUrl' => '',
'captchaResponse' => ''
));
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'cURL Error: ' . curl_error($curl);
curl_close($curl);
return;
}
curl_close($curl);
$result = json_decode($response, true);
if (isset($result['result']['accessToken'])) {
$this->session->set_userdata('access_token', $result['result']['accessToken']);
echo "Authenticated successfully. Access Token: " . $result['result']['accessToken'];
} else {
echo "Authentication failed. Response: " . print_r($result, true);
}
}
// Function to ensure valid token, authenticating if needed
private function ensureAuthenticated() {
if (!$this->session->userdata('access_token')) {
$this->authenticate();
}
}
// Function to fetch employees
public function fetchEmployees() {
$this->ensureAuthenticated(); // Ensure user is authenticated
$accessToken = $this->session->userdata('access_token');
$url = 'http://172.16.0.177:5101/api/services/app/HR201EmployeesV2/GetAll';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $accessToken,
));
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'cURL Error: ' . curl_error($curl);
curl_close($curl);
return;
}
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
// Decode the response
$data = json_decode($response, true);
// Check for successful response
if ($httpCode == 200 && isset($data['result'])) {
$this->load->view('employee_list', ['employees' => $data['result']]);
} else {
// If unauthorized, re-authenticate and retry
if ($httpCode == 401) {
$this->authenticate(); // Attempt to re-authenticate
return; // Prevent recursive calls
} else {
log_message('error', 'Failed to fetch employees. Response: ' . print_r($data, true));
echo 'Failed to fetch employees. Response: ' . json_encode($data);
}
}
}
// Function to check user permissions
public function checkUserPermissions() {
$this->ensureAuthenticated(); // Ensure user is authenticated
$accessToken = $this->session->userdata('access_token');
$url = 'http://172.16.0.177:5101/api/services/app/Permission/GetAllPermissions'; // Endpoint to get permissions
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $accessToken,
));
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'cURL Error: ' . curl_error($curl);
curl_close($curl);
return;
}
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
// Decode the response
$data = json_decode($response, true);
if ($httpCode == 200 && isset($data['result'])) {
echo "User Permissions: " . print_r($data['result'], true);
} else {
if ($httpCode == 401) {
$this->authenticate(); // Attempt to re-authenticate
return; // Prevent recursive calls
} else {
echo 'Failed to fetch user permissions. Response: ' . json_encode($data);
}
}
}
}
I want to show all the employees within my view employee_list.php