Differences

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

Link to this comparison view

Next revision
Previous revision
technical:api-pu-disable [2025/04/13 09:05] – created systemtechnical:api-pu-disable [2025/04/13 15:13] (current) system
Line 6: Line 6:
 ----- -----
  
-====== Permanent Users ====== +====== Enable and Disable Permanent Users ====== 
-  * A **Permanent User** has to belong to **Profile** and **Realm** in order to be useful to RADIUS+<alert type="warning"> 
-  * In the previous Wiki Pages we already covered adding Profile and a Realm+  * RADIUSdesk includes component called 'ISP-Plumbing' which needs to be configured first
-  * In this page we will show you how to add a Permanent User to a  Cloud in RADIUSdesk using an API call+  * When this component is configured, any active sessions for a user will be terminated upon the completion of the enable or disable action. 
-  * We will use PHP but the principles can be applied using any programming language. +  * This comes in handy to move user into or out of an isolation VLAN or network. 
- +</alert> 
------------------ +  * To enable or disable a Permanent User we have to first determine the id of the Permanent User
- +  * A CRM system typically do not know the id of the Permanent User in RADIUSdesk. 
-====== Add a Permanent User using the API ====== +  * With the sample script we use two APIs 
- +      * The one API is used get list of Permanent Users to collect their ids
-  * Below is a simple as possible script that can be used as reference when adding a Permanent User using the API+      The other API is used to either enable or disable the list of Permanent Users which was obtained
-  We also show the optional fields that can be included in the API call+<file php permanent_user_disable_enable.php>
-<file php add_permanent_users.php>+
 <?php <?php
    
     // Configuration     // Configuration
-    $api_url    = 'http://127.0.0.1/cake4/rd_cake/permanent-users/add.json';+    $server     = 'http://127.0.0.1'; 
 +    $api_url    = "$server/cake4/rd_cake/permanent-users/enable-disable.json";
     $token      = 'b4c6ac81-8c7c-4802-b50a-0a6380555b50';     $token      = 'b4c6ac81-8c7c-4802-b50a-0a6380555b50';
     $cloud_id   = 23; // replace with the Cloud ID that you want to work on.     $cloud_id   = 23; // replace with the Cloud ID that you want to work on.
-    $username   = 'testuser'; 
-    $password   = 'testing123'; 
-    $realm      = 'Dev'; 
-    $profile    = 'MzanziFibre-12/1'; 
-  
-    // Set API payload 
-    $payload = [ 
          
-        //Required Fields      +    $username   = 'testuser@dev'
-        'username'  => $username, +    $action     = 'disable'; //Options 'enableor 'disable
-        'password => $password, +     
-        'realm'     => $realm, +    // Filter settings 
-        'profile  => $profile, +    $filter     = 'username'; //field that filter should apply to 
-        'cloud_id => $cloud_id, +    $operator   = '==';  //Options '==','like'  
-        'token    => $token, +    $value      $username; //can also be set to boolean e.gtrue or false to filter for instance on the 'active' field 
-        'active'    => 'active', //active flag must be set for user to authenticate       +    $limit      1000; 
-                  +     
-       /*         +    $filter_array [ 
-       //Optional Fields   +        
-        'name'          => '', +            "operator" => $operator
-        'surname'       => '',      +            "value"    => $value, // this stays boolean! 
-        'phone'         ='', +            "property" => $filter 
-        'email'         ='', +        ]
-        'address'       => '', +
-        'from_date'     ='04/12/2025'+
-        'to_date'       => ''04/12/2026', +
-        'realm_id'     => 19, //Alternative to Realm's name +
-        'profile_id'    => 49, //Alternative to Profile's name +
-        'static_ip'     => '192.168.1.100+
-        'extra_name'    => '', +
-        'extra_value'   => '', +
-        'site'          => ''+
-        'ppsk'          => '', +
-        'auto_add'      => 'auto_add' //flag to automatically add the MAC Address the user connects with as Device (BYOD) belonging to him +
-        */+
     ];     ];
 +
 +    $encoded_filter = urlencode(json_encode($filter_array));
 +
 +    $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";
 +    
    
-    $json_payload = json_encode($payload); +    $listOfIds = getIds();    
-    $ch = curl_init($api_url); +    if($listOfIds){ 
-    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +    
-    curl_setopt($ch, CURLOPT_POST, true); +        $listOfIds['cloud_id' = $cloud_id; 
-    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_payload); +        $listOfIds['token'    = $token; 
-    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); +        $listOfIds['rb'       = $action; 
-  +        $json_payload           = json_encode($listOfIds); 
-    $response = curl_exec($ch); +         
-    curl_close($ch); +        $ch = curl_init($api_url); 
-    echo "Added $username: $response\n"; +        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
-  +        curl_setopt($ch, CURLOPT_POST, true); 
-?> +        curl_setopt($ch, CURLOPT_POSTFIELDS, $json_payload); 
-</file> +        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
-  * Now that we have created a Permanent User, we will cover some common actions that can be done on the Permanent User via the API.+
  
 +        $response = curl_exec($ch);
 +        curl_close($ch);
 +        echo "Response: $response\n";
 +    
 +    }
 +    
 +    function getIds(){
  
 +        global $find_url;        
 +     // Create stream context with headers
 +     $context = stream_context_create([
 +     'http' => [
 +         'header' => "Accept: application/json\r\n" .
 +          "Content-Type: application/json\r\n"
 +     ]
 +     ]);
 +     
 +     print($find_url);
 +
 +     // Get the response
 +     $response = file_get_contents($find_url, false, $context);
 +     
 +     if ($response === false) {
 +     echo "Error fetching data";
 +     exit;
 +     }
 +
 +     // Decode JSON
 +     $data = json_decode($response, true);    
 +     if (!empty($data['items']) && is_array($data['items'])) {          
 +            $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;
 +    }
 +     
 +?>
 +</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.
 +  * For this we have to make the following adjustments:
 +<code php>
 +$username   = '@dev';
 +$action     = 'disable'; //Options 'enable' or 'disable'
 +    
 +// 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;
 +</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-disable.1744527916.txt.gz
  • Last modified: 2025/04/13 09:05
  • by system