This is an old revision of the document!



Delete Permanent User

  • To terminate a user's active sessions we will work on a filtered list of active sessions for a user.
  • With the sample script we use two APIs
    • The one API is used get a list of active sessions for a specified Permanent User, if there are any.
    • The other API is used to terminate each of the sessions which was obtained with the first API call.
permanent_user_delete.php
<?php
 
// Configuration
$server     = 'http://127.0.0.1';
$token      = 'b4c6ac81-8c7c-4802-b50a-0a6380555b50';
$cloud_id   = 23;
 
$username   = 'testuser@dev';
$api_url    = "$server/cake4/rd_cake/radaccts/kick-active.json";
 
// Filter settings
$filter     = 'username'; //field that filter should apply to
$operator   = '==';  //Options '==','like' 
$value      = $username; //can also be set to boolean e.g. true or false to filter for instance on the 'active' field
$limit      = 1000;
 
// Filter
$filter_array = [[
    "operator" => $operator,
    "value"    => $value,
    "property" => $filter
]];
$encoded_filter = urlencode(json_encode($filter_array));
$find_url = "$server/cake4/rd_cake/radaccts/index.json?page=1&start=0&limit=1000&filter=$encoded_filter&token=$token&cloud_id=$cloud_id&only_connected=true";
 
// Get User IDs
$listOfIds = getIds($find_url);
 
if ($listOfIds) {
    foreach ($listOfIds as $id) {
        $payload = [
            'cloud_id'  => $cloud_id,
            'token'     => $token,
            $id         => $id
        ];
        print_r($payload);
        $response = sendDisconnectRequest($api_url, $payload);
        echo "Terminated Session ID $id - Response: $response\n";
    }
}
 
function getIds($find_url) {
    $context = stream_context_create([
        'http' => [
            'header' => "Accept: application/json\r\nContent-Type: application/json\r\n"
        ]
    ]);
 
    echo "Fetching URL: $find_url\n";
    $response = file_get_contents($find_url, false, $context);
 
    if ($response === false) {
        echo "Error fetching data\n";
        return false;
    }
 
    $data = json_decode($response, true);
 
    if (!empty($data['items']) && is_array($data['items'])) {
        echo "There are " . count($data['items']) . " items\n";
        return array_column($data['items'], 'id');
    }
 
    echo "No items found.\n";
    return false;
}
 
function sendDisconnectRequest($url, $data) {
 
    // Build the query string from $data
    $queryString = http_build_query($data);
 
    // Append it to the URL
    $urlWithParams = $url . '?' . $queryString;
 
    $ch = curl_init($urlWithParams);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => ['Accept: application/json'] // optional
    ]);
 
    $response = curl_exec($ch);
    if ($response === false) {
        echo "cURL error: " . curl_error($ch) . "\n";
    }
    curl_close($ch);
    return $response;
}
?>
  • We are not limited to only one user but we can adjust the filter for instance to terminate the sessions of all the users with a specific suffix e.g. @dev.
  • For this we have to make the following adjustments:
$username   = '@dev';
 
// Filter settings
$filter     = 'username'; //field that filter should apply to
$operator   = 'like';  //Options '==','like' 
$value      = $username; //can also be set to boolean e.g. true or false to filter for instance on the 'active' field
$limit      = 1000;
  • technical/api-pu-delete.1744548213.txt.gz
  • Last modified: 2025/04/13 14:43
  • by system