Performing custom operations

From ISPWiki

Jump to: navigation, search

Following is an example on how to perform a custom operation while the control panel performs its own actions.

Task

  • Create a Samba account while creating a user in ISPmanager.
  • Delete a Samba user account while deleting a user.

Solution

1) Create a description file /usr/local/ispmgr/etc/ispmgr_mod_samba.xml for the plug-in with the following contents

<?xml version="1.0" encoding="UTF-8"?>
  <mgrdata>
    <handler name="samba.pl" type="cgi">
      <event after="yes">user.edit</event>
      <event after="yes">user.delete</event>
    </handler>
  </mgrdata>

2) Create a file-handler /usr/local/ispmgr/addon/samba.pl with the following contents

#!/usr/bin/perl
use CGI;
my $Q = new CGI;
$func = $Q->param("func");
$elid = $Q->param("elid");
 
 
if ($func eq "user.edit") {
  if ($Q->param("sok") && $elid eq "") {
    # a user is created 
    $name = $Q->param("name");
    $password = $Q->param("passwd");
 
    system("(echo \"$password\"; echo \"$password\") | smbpasswd -a $name");
  }
} elsif ($func eq "user.delete") {
    # users are deleted 
    @all_users = split(", ", $elid);
    for (@all_users) {
      system("smbpasswd -x $_");
    }
}
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc/>\n";

3) Set privileges for the file-handler

chmod 750 /usr/local/ispmgr/addon/samba.pl
chown 0:0 /usr/local/ispmgr/addon/samba.pl

4) Restart ISPmanager

Comments

Description


In the above example we run a custom script while performing operations in the control panel. Take a detailed look at the block

   <handler name="samba.pl" type="cgi">
     <event after="yes">user.edit</event>
     <event after="yes">user.delete</event>
   </handler>

The handler tag describes our event handler (script); samba.pl is the name of the script. All the script should locate in the /usr/local/ispmgr/addon directory.

type="cgi" defines that to transfer the data to the script, we need a to use method that is normally used for CGI-scripts.

The event tags define which functions should be executed to start our script handler. The after="yes" attribute defines that our script will start after execution of the main function. You can view internal names of required functions in the API Documentation. In our example go to ISPmanager API

More information can be found in the article Events.


Handler


Let's now consider our handler in more detail. Since we use one handler for several functions, we need to know which function has started it. To do that, consider the func parameter

$func = $Q->param("func");

and depending on its value create different blocks.

user.edit - create a user. Actually, this function is called to get user data, save modified properties and create a user. Below is a description of how you can define the action that is currently being performed:

  • if the sok parameters is empty or not present, this means that user data are requested, username is provided in the elid parameter.
  • if the sok parameter is not empty (usually yes or ok, but the control panel does not check its value, it must not be empty) and the elid parameter is not empty, this means that the edit user function is called, name of the user being modified is provided in the elid parameter.
  • if the sok parameter is not empty, the elid parameter is empty, a user creation function is called. We may check this in
 if ($Q->param("sok") && $elid eq "") {

The example with sok and elid described above holds for a create/edit function for other items.

Then we will get parameters' values and an external command that will perform our operations. You can learn internal names of parameters in the API documentation for a required control panel.

user.delete - delete a user. This is an operation over a group of items that allows you to delete several users at a time, so the elid parameter can contain several names separated by the symbols ", " (colon and space). Form an array of users you want to delete

@all_users = split(", ", $elid);

and execute the command to delete a user from the Samba password base for each user.

Our handler should return an XML document containing results to STDOUT. If the result is not required (as in our example), an empty valid XML should be returned.

print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc/>\n";
Was this helpful? Yes | No
Views
Personal tools