Celebrate WHMCS Price Hike With 10% Off Blesta Licenses Using WCHIMPCS At Checkout! View Offers

September 30th, 2024

Add SSO to DirectAdmin

If you’re using Blesta to manage hosting services with DirectAdmin, you might want to provide your clients with a seamless way to access their DirectAdmin control panel directly from the Blesta client area. In this tutorial, we’ll walk through the steps to add a “Login” tab and a login link to the service information page in Blesta’s DirectAdmin module.

Prerequisites

• Blesta installed and configured.
• DirectAdmin module activated in Blesta.
• Basic understanding of PHP and Blesta’s module structure.

Overview

We’ll be making changes to the following files:


• api/direct_admin_api.php
• language/en_us/direct_admin.php
• direct_admin.php
• views/default/tab_login.pdt
• views/default/client_service_info.pdt

Let’s get started!
 

Step 1: Update the API file

First, we’ll add two new functions to the DirectAdmin API Functions to handle SSO requests and to get the Single Sign-On (SSO) URL.

File: api/direct_admin_api.php

Add the following functions inside the class:

private function jsonRequest($url, $method, $data) 
{
    $curl = curl_init();

    curl_setopt_array($curl, [
        CURLOPT_URL => trim($this->apiUrl, '/') . '/' . $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => $method,
        CURLOPT_POSTFIELDS => json_encode($data),
        CURLOPT_USERPWD => $this->user . ':' . $this->pass,
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC
    ]);

    $response = curl_exec($curl);

    curl_close($curl);
    return json_decode($response);
}

public function getSSOUrl($username, $password)
{
    return $this->jsonRequest('api/login', 'POST', ['username' => $username, 'password' => $password]);
}

These functions will allow us to make requests to DirectAdmin and retrieve the SSO URL for client login.

 

Step 2: Add Language Definitions

Next, we’ll add a new language definition for our “Login” tab.

File: language/en_us/direct_admin.php

Add the following line:

$lang['DirectAdmin.tab_login'] = "Login";

This defines the label for the new tab we’ll be adding.

 

Step 3: Create the tabLogin Function

We’ll add a new function in the main module file to handle the content of the “Login” tab.

File: direct_admin.php

Add the following function:

/**
 * Client Login
 *
 * @param stdClass $package A stdClass object representing the current package
 * @param stdClass $service A stdClass object representing the current service
 * @param array $get Any GET parameters
 * @param array $post Any POST parameters
 * @param array $files Any FILES parameters
 * @return string The string representing the contents of this tab
 */
public function tabLogin($package, $service, array $get = null, array $post = null, array $files = null)
{
    $this->view = new View('tab_login', 'default');
    $this->view->base_uri = $this->base_uri;
    // Load the helpers required for this view
    Loader::loadHelpers($this, ['Form', 'Html']);

    $row = $this->getModuleRow();
    $api = $this->getApi(
        $row->meta->host_name,
        $row->meta->user_name,
        $row->meta->password,
        ($row->meta->use_ssl == 'true'),
        $row->meta->port
    );

    $service_fields = $this->serviceFieldsToObject($service->fields);

    $this->view->set('login_url', $api->getSSOUrl($service_fields->direct_admin_username, $service_fields->direct_admin_password)->loginURL ?? '');
    $this->view->set('service_fields', $service_fields);
    $this->view->set('service_id', $service->id);
    $this->view->set('vars', (isset($vars) ? $vars : new stdClass()));

    $this->view->setDefaultView('components' . DS . 'modules' . DS . 'direct_admin' . DS);
    return $this->view->fetch();
}



This function sets up the view for the “Login” tab and fetches the SSO URL from DirectAdmin.

 

Step 4: Update Client Tabs

We need to register our new tab so it appears in the client interface.

File: direct_admin.php

Find the getClientTabs function and modify it as follows:


/**
 * Returns all tabs to display to a client when managing a service whose
 * package uses this module
 *
 * @param stdClass $package A stdClass object representing the selected package
 * @return array An array of tabs in the format of method => title.
 *  Example: ['methodName' => 'Title', 'methodName2' => 'Title2']
 */
public function getClientTabs($package)
{
    return [
        'tabClientActions' => Language::_('DirectAdmin.tab_client_actions', true),
        'tabLogin' => Language::_('DirectAdmin.tab_login', true)
    ];
}


By adding 'tabLogin' => Language::_('DirectAdmin.tab_login', true), we’re telling Blesta to include our new tab.

Step 5: Create the View for the “Login” Tab

Now, we’ll create the view file that contains the HTML for the “Login” tab.

File: views/default/tab_login.pdt

Create a new file with the following content:

<div id="login">
    <h4><?php $this->_('DirectAdmin.tab_login');?></h4>

    <a class="btn btn-light float-right" href="<?= $login_url ?>" target="_blank">
        <i class="fas fa-user"></i> <?php $this->_('DirectAdmin.tab_login');?>
    </a>
</div>


This view displays a button that links to the DirectAdmin control panel using the SSO URL.

 

Step 6: Enhance the Service Information Page

We also want to add a login link directly on the service information page.

File: direct_admin.php

Modify the getClientServiceInfo function to include the login URL:

/**
 * Fetches the HTML content to display when viewing the service info in the
 * client interface.
 *
 * @param stdClass $service A stdClass object representing the service
 * @param stdClass $package A stdClass object representing the service's package
 * @return string HTML content containing information to display when viewing the service info
 */
public function getClientServiceInfo($service, $package)
{
    $row = $this->getModuleRow();

    // Load the view into this object, so helpers can be automatically added to the view
    $this->view = new View('client_service_info', 'default');
    $this->view->base_uri = $this->base_uri;
    $this->view->setDefaultView('components' . DS . 'modules' . DS . 'direct_admin' . DS);

    // Load the helpers required for this view
    Loader::loadHelpers($this, ['Form', 'Html']);

    $row = $this->getModuleRow();
    $api = $this->getApi(
        $row->meta->host_name,
        $row->meta->user_name,
        $row->meta->password,
        ($row->meta->use_ssl == 'true'),
        $row->meta->port
    );

    $service_fields = $this->serviceFieldsToObject($service->fields);

    $this->view->set('login_url', $api->getSSOUrl($service_fields->direct_admin_username, $service_fields->direct_admin_password)->loginURL ?? '');
    $this->view->set('module_row', $row);
    $this->view->set('package', $package);
    $this->view->set('service', $service);
    $this->view->set('service_fields', $service_fields);

    return $this->view->fetch();
}

This adds the login_url variable to the view.

 

Step 7: Update the Service Information View

Finally, we’ll modify the service information view to include the login link.

File: views/default/client_service_info.pdt

Locate the last <td> element and modify it as follows:

<td><a href="<?= $login_url ?>" target="_blank"><?php $this->_('DirectAdmin.service_info.option_login');?></a></td>

The updated file should look like this:

<div class="table-responsive">
    <table class="table table-curved table-striped">
        <thead>
            <tr>
                <th><i class="fas fa-share fa-flip-vertical"></i></th>
                <th><?php $this->_('DirectAdmin.service_info.username');?></th>
                <th><?php $this->_('DirectAdmin.service_info.password');?></th>
                <th><?php $this->_('DirectAdmin.service_info.server');?></th>
                <th><?php $this->_('DirectAdmin.service_info.options');?></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td></td>
                <td><?php echo (isset($service_fields->direct_admin_username) ? $this->Html->safe($service_fields->direct_admin_username) : null);?></td>
                <td><?php echo (isset($service_fields->direct_admin_password) ? $this->Html->safe($service_fields->direct_admin_password) : null);?></td>
                <td><?php echo (isset($module_row->meta->host_name) ? $this->Html->safe($module_row->meta->host_name) : null);?></td>
                <td><a href="<?= $login_url ?>" target="_blank"><?php $this->_('DirectAdmin.service_info.option_login');?></a></td>
            </tr>
        </tbody>
    </table>
</div>

This adds a login link in the options column, allowing clients to access DirectAdmin directly from their service information page.

 

Conclusion

By following these steps, you’ve successfully added a “Login” tab and a login link to the service information page in Blesta’s DirectAdmin module. This enhancement provides a more seamless experience for your clients, allowing them to access their DirectAdmin control panel with a single click.

Feel free to customize the views and styles to match your branding and improve the user experience further.

Note: Always make sure to backup your files before making any changes, and test the modifications in a development environment if possible.

If you need further assistance, We are available to hire to make these changes for you at a reasonable price!

...
Chris
Author

Part owner of Blesta Club Ltd, Owner of TekLan Hosting and a few other companies.