Jump to content

OverrideModuleUsernameGeneration hook not triggered when expected to be


nuxwin

Recommended Posts

Good morning,

 

We created a WHMCS provisioning module for our control panel. Because in our control panel, the usernames match the domain names, we added a hook which overrides the username that is automatically generated by WHMCS when an order is being placed. The hook look as follow:

 

/**
* Replace generated username by domain name
*/
add_hook('OverrideModuleUsernameGeneration', 1, function ($vars) {
   if($vars['moduletype'] != 'imscp') {
       return null;
   }

   return $vars['domain'];
});

 

 

This works well for products that are setup automatically (the hook is run as expected and the username is set with the domain name as expected). However, when the order is placed and accepted manually, or when the product setup is not automatic, the hook is not triggered and thus, the username is not overriden.

 

We already reported the issue to WHMCS team (#AMI-418461 no answered yet) but it would be great if someone here could help us to find a temporary workaround. The hook should be triggered in every contexts (when our provisioning module is involved).

 

To resume, each time the username is being generated, that hook should be called.

 

Thank you for your help.

 

Involved WHMCS version: 6.3.1

Edited by nuxwin
Link to comment
Share on other sites

try http://docs.whmcs.com/Hooks:AfterModuleCreate you will need to override/save the generated username in database directly

 

@sentq

 

Thank for your answer.

 

Something like:

 

/**
* Replace generated username by domain name
*/
add_hook('AfterModuleCreate', 1, function ($vars) {
   $vars = $vars['params'];

   if ($vars['moduletype'] != 'imscp') {
       return NULL;
   }

   full_query(
       "
         UPDATE tblhosting SET username = '{$vars['domain']}', lastupdate = NOW()
         WHERE id = '{$vars['accountid']}'
       "
   );
});

 

Is there a way to prepare the SQL statement? I would like the values being automatically quoted.

 

Thank.

 

Edit: Ok, it seem that full_query() function is being deprecated. I'll update according: http://docs.whmcs.com/Interacting_With_The_Database

 

Apart this, does the logic above is ok for you?

Edited by nuxwin
Link to comment
Share on other sites

@sentq

 

I figured out. I prefer use the PreModuleCreate hook because I can stop the process on error. The final hooks.php file look like this:

 

use Illuminate\Database\Capsule\Manager as Capsule;

/**
* Replace generated username by domain name when a product is setup automatically
*/
add_hook('OverrideModuleUsernameGeneration', 99, function ($vars) {
   if ($vars['moduletype'] != 'imscp') {
       return NULL;
   }

   return $vars['domain'];
});

/**
* Replace generated username by domain name when a product is setup manually
*/
add_hook('PreModuleCreate', 99, function ($vars) {
   $vars = $vars['params'];

   if ($vars['moduletype'] != 'imscp' || $vars['username'] === $vars['domain']) {
       return NULL;
   }

   try {
       Capsule::table('tblhosting')
           ->where('id', $vars['accountid'])
           ->update(array('username' => $vars['domain']));
   } catch (Exception $e) {
       logActivity(sprintf('[ERROR] Could not override username: %s', $e->getMessage()));
       return array('abortcmd' => true);
   }

   return NULL; // Only to make my PHPstorm IDE happy
});

 

Again, thank you for your help.

Edited by nuxwin
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated