• HWIKI

    Build. Break. Explain.

    Building Custom Plugins for FusionDirectory

    FusionDirectory is a versatile tool for managing users and resources via LDAP. Its flexible plugin system allows administrators and developers to extend functionality and tailor the interface to specific needs. In this guide, we’ll walk through the process of creating a custom plugin, from setting up the PHP files to integrating custom LDAP schemas.

    1. Getting Started

    Before diving in, ensure you have:

    • A working FusionDirectory installation: Your system must already run FusionDirectory.
    • Basic knowledge of LDAP and PHP: The guide assumes you’re comfortable with PHP and the fundamentals of LDAP.
    • Root access to your server: You’ll need administrative rights to install files, update configurations, and modify schemas.

    2. Understanding the Plugin Structure

    A FusionDirectory plugin is essentially a PHP script that must be saved as an .inc file. Typically, your plugin should be stored in the following directory:

    /usr/share/fusiondirectory/plugins/<plugin_type>/<plugin_name>/<plugin_name>.inc
    

    The <plugin_type> reflects the plugin’s role (e.g., admin, config, or personal), while <plugin_name> is the unique name for your plugin. This structure ensures FusionDirectory can locate and load your plugin correctly.

    3. Writing Your Plugin Code

    Let’s take a look at a sample plugin file, customplugin.inc. This example demonstrates how you can create a plugin that adds extra input fields for user information:

    php
    <?php
    class demoPlugin extends simplePlugin
    {
      // Determines whether the plugin is enabled by default (TRUE) or not (FALSE)
      var $displayHeader = FALSE;
    
      // Provides basic plugin information such as its name and description.
      static function plInfo (): array
      {
        return [
          'plShortName'       => _('Demo Plugin'),
          'plTitle'           => _('Demo Plugin Information'),
          'plDescription'     => _('Edit some useless personal information'),
          'plSelfModify'      => TRUE,
          'plObjectType'      => ['user'],
    
          // IMPORTANT: This name must match the LDAP object class in the schema.
          'plObjectClass'     => ['demoPlugin'],
    
          // simplePlugin can automatically generate the ACL list
          'plProvidedAcls'    => parent::generatePlProvidedAcls(self::getAttributesInfo())
        ];
      }
    
      // Here, you can define custom fields for your plugin.
      static function getAttributesInfo (): array
      {
        return [
            // Example of adding input fields under a specific tab:
            /*
            'tab' => [ // Change the tab name
                'name'  => _('Case'), // Name of the box
                'attrs' => [
                    new AttributeType (
                        _('Color'),                     // Field label
                        _('Color of the hair'),         // Field description
                        'hairColor',                    // LDAP attribute name
                        TRUE,                           // Whether this field is mandatory
                        'DEFAULT VALUE'
                    ),
                ]
            ],
            */
        ];
      }
    
      // This function handles saving the plugin’s data into LDAP.
      protected function ldap_save (): array
      {
        $errors = parent::ldap_save();
        if (!empty($errors)) {
          return $errors;
        }
        return $errors;
      }
    }
    

    4. Integrating LDAP Schemas

    In addition to your PHP file, you need to define an LDAP schema that complements your plugin. Here’s an example of a simple schema file (customplugin.schema):

    ldif
    # customplugin.schema
    attributetype ( 1.3.6.1.4.1.9999.1
        NAME 'customAttribute'
        DESC 'A custom attribute'
        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15
    )
    
    objectclass ( 1.3.6.1.4.1.9999.2
        NAME 'demoPlugin'
        DESC 'A custom LDAP object'
        SUP top STRUCTURAL
        MUST customAttribute
    )
    

    Place this schema file in the following directory:

    /etc/ldap/schemas/fusiondirectory/myplugin.schema
    

    Then, run the command below to insert your schema into FusionDirectory’s LDAP system:

    bash
    sudo fusiondirectory-schema-manager --insert-schema /path/to/myplugin.schema
    

    To verify that your schema has been successfully added, check the list of schemas:

    bash
    sudo fusiondirectory-schema-manager --list-schemas
    

    5. Applying Changes

    After creating your plugin and integrating the schema, it’s important to apply the changes so FusionDirectory recognizes them. Clear the FusionDirectory cache with:

    bash
    sudo fusiondirectory-configuration-manager --update-cache
    

    Finally, log out and log back in to FusionDirectory to see your custom plugin in action.

    Final Thoughts

    Custom plugins are a powerful way to extend FusionDirectory to better suit your organization’s needs. This guide covered the essentials—from file placement and PHP scripting to LDAP schema integration and cache updates. With these steps, you should be well on your way to building plugins that can add new features or streamline existing processes.

    For more detailed instructions and examples, refer to the complete documentation available in both English and French. Happy coding!

    Made by h0ag