Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
technical:api-pu-profile [2025/04/13 10:27] – created systemtechnical: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 ======
-  * To enable or disable a Permanent User we have to first determine the id of the Permanent User.+ 
 +<alert type="warning"> 
 +  * RADIUSdesk includes a component called 'ISP-Plumbing' which needs to be configured first. 
 +  * 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. 
 +</alert> 
 + 
 +  * To change the Profile of a Permanent User we first have to determine the id of the Permanent User.
   * 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 list of Permanent Users which was obtained. +      * 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     = 'http://127.0.0.1'; 
-    $api_url    = "$server/cake4/rd_cake/permanent-users/enable-disable.json"; 
-    $token      = 'b4c6ac81-8c7c-4802-b50a-0a6380555b50'; 
-    $cloud_id   = 23; // replace with the Cloud ID that you want to work on. 
-     
-    $username   = 'testuser@dev'; 
-    $action     = 'disable'; //Options 'enable' or 'disable' 
-     
-    // 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_array = [ 
-        [ 
-            "operator" => $operator, 
-            "value"    => $value, // this stays a boolean! 
-            "property" => $filter 
-        ] 
-    ]; 
  
-    $encoded_filter urlencode(json_encode($filter_array));+// Configuration 
 +$server     = 'http://127.0.0.1'; 
 +$token      'b4c6ac81-8c7c-4802-b50a-0a6380555b50'; 
 +$cloud_id   = 23;
  
-    $find_url "$server/cake4/rd_cake/permanent-users/index.json?page=1&start=0&limit=$limit&filter=$encoded_filter&token=$token&cloud_id=$cloud_id"; +$username   = 'testuser@dev'; 
-     +$profile    = 'MzanziFibre-12/1'; 
-  +$realm      = 'Dev';
-    $listOfIds = getIds();    +
-    if($listOfIds){ +
-    +
-        $listOfIds['cloud_id']  = $cloud_id+
-        $listOfIds['token'    $token; +
-        $listOfIds['rb']        = $action+
-        $json_payload           json_encode($listOfIds); +
-         +
-        $ch = curl_init($api_url); +
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +
-        curl_setopt($ch, CURLOPT_POST, true); +
-        curl_setopt($ch, CURLOPT_POSTFIELDS, $json_payload); +
-        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));+
  
-        $response curl_exec($ch); +$api_url    "$server/cake4/rd_cake/permanent-users/edit-basic-info.json"; 
-        curl_close($ch); + 
-        echo "Response: $response\n"; +// 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/permanent-users/index.json?page=1&start=0&limit=1000&filter=$encoded_filter&token=$token&cloud_id=$cloud_id"; 
 + 
 +// Get User IDs 
 +$listOfIds = getIds($find_url); 
 + 
 +if ($listOfIds) { 
 +    foreach ($listOfIds as $id) { 
 +        $payload = [ 
 +            'cloud_id'  => $cloud_id, 
 +            'token'     => $token, 
 +            'realm'     => $realm, 
 +            'profile'   => $profile, 
 +            'id'        => $id 
 +        ]; 
 + 
 +        $response = sendPostRequest($api_url, $payload); 
 +        echo "Updated ID $id - Response: $response\n";
     }     }
-     +}
-    function getIds(){+
  
-        global $find_url;         +function getIds($find_url) { 
-     // Create stream context with headers +    $context = stream_context_create([ 
-     $context = stream_context_create([ +        'http' => [ 
-     'http' => [ +            'header' => "Accept: application/json\r\nContent-Type: application/json\r\n" 
-         'header' => "Accept: application/json\r\n" . +        
-          "Content-Type: application/json\r\n" +    ]);
-     +
-     ]); +
-      +
-     print($find_url);+
  
-     // Get the response +    echo "Fetching URL: $find_url\n"; 
-     $response = file_get_contents($find_url, false, $context); +    $response = file_get_contents($find_url, false, $context);
-      +
-     if ($response === false) { +
-     echo "Error fetching data"; +
-     exit; +
-     }+
  
-     // Decode JSON +    if ($response === false) { 
-     $data = json_decode($response, true);     +        echo "Error fetching data\n"; 
-     if (!empty($data['items']) && is_array($data['items'])) {           +        return false;
-            $count      count($data['items']); +
-            $listOfIds  []; +
-            foreach($data['items'] as $permanentUser){ +
-                $id             = $permanentUser['id']; +
-                $listOfIds[$id] = $id;     +
-            } +
-            echo "There $count items\n"+
-            return $listOfIds+
-        } else { +
-            echo "No items."; +
-        } +
-     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 sendPostRequest($url, $data) { 
 +    $ch = curl_init($url); 
 +    curl_setopt_array($ch,
 +        CURLOPT_RETURNTRANSFER => true, 
 +        CURLOPT_POST           => true, 
 +        CURLOPT_POSTFIELDS     => json_encode($data), 
 +        CURLOPT_HTTPHEADER     => ['Content-Type: application/json'
 +    ]); 
 +    $response = curl_exec($ch); 
 +    if ($response === false) { 
 +        echo "cURL error: " . curl_error($ch) . "\n"; 
 +    } 
 +    curl_close($ch); 
 +    return $response; 
 +}
 ?> ?>
 </file> </file>
-  * We are not limited to only one user but we can adjust the filter for instance to disable all the users with a specific suffix e.g. @dev.+  * 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   = '@dev'; $username   = '@dev';
-$action     = 'disable'; //Options 'enable' or 'disable' +
-    +
 // Filter settings // Filter settings
 $filter     = 'username'; //field that filter should apply to $filter     = 'username'; //field that filter should apply to
Line 115: Line 120:
 $limit      = 1000; $limit      = 1000;
 </code> </code>
-  * By the same token, if we want to enable all disabled users we can adjust the filter as follows: 
-<code php> 
-$action     = 'enable'; //Options 'enable' or 'disable' 
-     
-// Filter settings 
-$filter     = 'active'; //field that filter should apply to 
-$operator   = '==';  //Options '==','like'  
-$value      = false; //can also be set to boolean e.g. true or false to filter for instance on the 'active' field 
-$limit      = 1000; 
-</code> 
- 
  
  • technical/api-pu-profile.1744532823.txt.gz
  • Last modified: 2025/04/13 10:27
  • by system