Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| technical:api-pu-profile [2025/04/13 10:27] – created system | technical:api-pu-profile [2025/04/13 15:15] (current) – system | ||
|---|---|---|---|
| Line 7: | Line 7: | ||
| ====== Change a Permanent User's Profile ====== | ====== Change a Permanent User's Profile ====== | ||
| - | | + | |
| + | <alert type=" | ||
| + | * RADIUSdesk includes a component called ' | ||
| + | * When this component is configured, any active sessions for a user will be terminated after the profile of a Permanent User is changed | ||
| + | * This comes in handy to activate the new profile upon the user immediately. | ||
| + | </ | ||
| + | |||
| + | | ||
| * A CRM system typically do not know the id of the Permanent User in RADIUSdesk. | * A CRM system typically do not know the id of the Permanent User in RADIUSdesk. | ||
| * With the sample script we use two APIs | * With the sample script we use two APIs | ||
| * The one API is used get a list of Permanent Users to collect their ids. | * The one API is used get a list of Permanent Users to collect their ids. | ||
| - | * The other API is used to either enable or disable | + | * The other API is used to change the Profile of the list of Permanent Users which was obtained. |
| - | <file php permanent_user_disable_enable.php> | + | <file php permanent_user_change_profile.php> |
| <?php | <?php | ||
| - | |||
| - | // Configuration | ||
| - | $server | ||
| - | $api_url | ||
| - | $token | ||
| - | $cloud_id | ||
| - | | ||
| - | $username | ||
| - | $action | ||
| - | | ||
| - | // Filter settings | ||
| - | $filter | ||
| - | $operator | ||
| - | $value | ||
| - | $limit | ||
| - | | ||
| - | $filter_array = [ | ||
| - | [ | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | ] | ||
| - | ]; | ||
| - | $encoded_filter | + | // Configuration |
| + | $server | ||
| + | $token | ||
| + | $cloud_id | ||
| - | | + | $username |
| - | + | $profile | |
| - | + | $realm | |
| - | $listOfIds = getIds(); | + | |
| - | if($listOfIds){ | + | |
| - | + | ||
| - | $listOfIds['cloud_id'] = $cloud_id; | + | |
| - | $listOfIds[' | + | |
| - | $listOfIds['rb'] = $action; | + | |
| - | $json_payload | + | |
| - | + | ||
| - | $ch = curl_init($api_url); | + | |
| - | curl_setopt($ch, | + | |
| - | curl_setopt($ch, | + | |
| - | curl_setopt($ch, | + | |
| - | curl_setopt($ch, | + | |
| - | | + | $api_url |
| - | | + | |
| - | echo " | + | // Filter settings |
| - | | + | $filter |
| + | $operator | ||
| + | $value | ||
| + | $limit | ||
| + | |||
| + | // Filter | ||
| + | $filter_array = [[ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ]]; | ||
| + | $encoded_filter = urlencode(json_encode($filter_array)); | ||
| + | $find_url = " | ||
| + | |||
| + | // Get User IDs | ||
| + | $listOfIds = getIds($find_url); | ||
| + | |||
| + | if ($listOfIds) { | ||
| + | foreach ($listOfIds as $id) { | ||
| + | | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ]; | ||
| + | |||
| + | $response = sendPostRequest($api_url, $payload); | ||
| + | echo "Updated ID $id - Response: $response\n"; | ||
| } | } | ||
| - | + | } | |
| - | function getIds(){ | + | |
| - | global | + | function getIds($find_url) { |
| - | // Create stream context with headers | + | $context = stream_context_create([ |
| - | | + | ' |
| - | | + | ' |
| - | | + | ] |
| - | " | + | ]); |
| - | | + | |
| - | | + | |
| - | + | ||
| - | print($find_url); | + | |
| - | // Get the response | + | |
| - | $response = file_get_contents($find_url, | + | |
| - | + | ||
| - | if ($response === false) { | + | |
| - | echo "Error fetching data"; | + | |
| - | exit; | + | |
| - | } | + | |
| - | // Decode JSON | + | |
| - | | + | echo "Error fetching data\n"; |
| - | if (!empty($data[' | + | return false; |
| - | $count | + | |
| - | $listOfIds | + | |
| - | foreach($data[' | + | |
| - | | + | |
| - | $listOfIds[$id] = $id; | + | |
| - | } | + | |
| - | | + | |
| - | return $listOfIds; | + | |
| - | | + | |
| - | echo "No items."; | + | |
| - | } | + | |
| - | | + | |
| } | } | ||
| - | + | ||
| + | $data = json_decode($response, | ||
| + | |||
| + | if (!empty($data[' | ||
| + | echo "There are " . count($data[' | ||
| + | return array_column($data[' | ||
| + | } | ||
| + | |||
| + | echo "No items found.\n"; | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | function sendPostRequest($url, | ||
| + | $ch = curl_init($url); | ||
| + | curl_setopt_array($ch, | ||
| + | CURLOPT_RETURNTRANSFER => true, | ||
| + | CURLOPT_POST | ||
| + | CURLOPT_POSTFIELDS | ||
| + | CURLOPT_HTTPHEADER | ||
| + | ]); | ||
| + | $response = curl_exec($ch); | ||
| + | if ($response === false) { | ||
| + | echo "cURL error: " . curl_error($ch) . " | ||
| + | } | ||
| + | curl_close($ch); | ||
| + | return $response; | ||
| + | } | ||
| ?> | ?> | ||
| </ | </ | ||
| - | * We are not limited to only one user but we can adjust the filter for instance to disable | + | * We are not limited to only one user but we can adjust the filter for instance to change the Profiles of all the users with a specific suffix e.g. @dev. |
| * For this we have to make the following adjustments: | * For this we have to make the following adjustments: | ||
| <code php> | <code php> | ||
| $username | $username | ||
| - | $action | + | |
| - | | + | |
| // Filter settings | // Filter settings | ||
| $filter | $filter | ||
| Line 115: | Line 120: | ||
| $limit | $limit | ||
| </ | </ | ||
| - | * By the same token, if we want to enable all disabled users we can adjust the filter as follows: | ||
| - | <code php> | ||
| - | $action | ||
| - | | ||
| - | // Filter settings | ||
| - | $filter | ||
| - | $operator | ||
| - | $value | ||
| - | $limit | ||
| - | </ | ||
| - | |||