Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get username and session id of logged in user #328

Open
Neelam-zanvar opened this issue Feb 7, 2024 · 0 comments
Open

Get username and session id of logged in user #328

Neelam-zanvar opened this issue Feb 7, 2024 · 0 comments

Comments

@Neelam-zanvar
Copy link

Hi i have created a zabbix frontend module to generate a audit report. For some requirement i want to fetch the current logged in user name for that session i want to fetch it and use it in the php code of zabbix module. How can i get the logged in user name for that session? i tried user.get api but it's not working
`function getLoggedInUsername($apiUrl, $apiToken)
{
$data = [
'jsonrpc' => '2.0',
'method' => 'user.get',
'params' => [
'output' => ['userid', 'alias'],
'selectMediatypes' => 'extend',
],
'auth' => $apiToken,
'id' => 1,
];

$options = [
    'http' => [
        'header' => [
            "Content-type: application/json",
            "User-Agent: PHP"
        ],
        'method' => 'POST',
        'content' => json_encode($data),
    ],
];

$context = stream_context_create($options);
$response = file_get_contents($apiUrl, false, $context);

$result = json_decode($response, true);

if (isset($result['result'][0]['alias'])) {
    return $result['result'][0]['alias'];
}

return null;

}

// Zabbix API URL and token
$zabbixApiUrl = 'https://zabbix url/zabbix/api_jsonrpc.php';
$zabbixApiToken = 'zabbix token'

// Get the currently logged-in username
$loggedInUsername = getLoggedInUsername($zabbixApiUrl, $zabbixApiToken);
echo "Retrieved Username (Main Script): $loggedInUsername\n";

`
i am getting the following Retrieved Username (Main Script):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant