Dynamic interface modification

From ISPWiki

Jump to: navigation, search

Following is an example on how to modify the interface depending on particular conditions.

Task

  • We have the ISPmanager control panel
  • A user john wants to use Squremail, rather than Roundcube as a mail web-interface.
  • Roundcube is accessible at /roundcube

Solution

1) Create a file /usr/local/ispmgr/etc/ispmgr_mod_choosewebmail.xml with the following contents

<?xml version="1.0" encoding="UTF-8"?>
  <mgrdata>
    <handler name="choosewebmail.pl" type="xml">
      <event after="yes">desktop</event>
    </handler>
  </mgrdata>

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

#!/usr/bin/perl
use XML::LibXML;
my $d = XML::LibXML->new->parse_string(join "", <STDIN>);
 
if ($ENV{"REMOTE_USER"} == "john") {
  @webnodes = ($d->findnodes("//node[@name='webmail']"));
  if ($#webnodes != -1) {
    $webnodes[0]->setAttribute("function","/roundcude");
  }
}
 
print $d->toString;

3) Set privileges on the file handler

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

4) Restart ISPmanager

Comments

As in the previous example we run our script while some functions were performed in the control panel. But now we are using the xml data transfer type

<handler name="choosewebmail.pl" type="xml">

This method allows to alter the data formed by the control panel, i.e. we can add, edit or delete anything we need.

The handler receives to STDIN an XML document formed by the control panel containing interface description, text messages and data. We recommend using the library XML::LibXML for working with the XML document and convert the data from STDIN into an XML object.

use XML::LibXML;
my $d = XML::LibXML->new->parse_string(join "", <STDIN>);

Then check that the user making a request is named john

if ($ENV{"REMOTE_USER"} == "john") {

Real plug-ins will have a more complicated condition. It's better create "Favourite web mail" for a user and specify which web mail to display. We are not going to consider this example.

You need to find the element that describes a web mail link, this is a node tag, its "name" attribute is "webmail".

@webnodes = ($d->findnodes("//node[@name='webmail']"));

Check this item is the menu. If anyб change its "function" attribute that describes the link

 if ($#webnodes != -1) {
   $webnodes[0]->setAttribute("function","/roundcude");
 }

The handler should return the XML document that will replace that of the control panel.

print $d->toString;

Even if you hander does not change anything in a document, but has the xml type, return the input document.


See also the article Hiding a field for more detail.

Was this helpful? Yes | No
Views
Personal tools