Jump to content

hook PreModuleCreate


tozaz2

Recommended Posts

Hi,

 

I need to create a random username when create module function is called.

I use this module server https://github.com/secondimpression/whmcs-freeradius

So I created a hooks.php file in modules/servers/freeradius with the as content below

 

require_once('/home/www/whmcs/init.php');
use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('PreModuleCreate', 1, function ($vars)
{

       $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
       $charactersLength = strlen($characters);
       $username .= $characters[rand(0, $charactersLength - 1)];

       for ($i = 0; $i < 10; $i++) {
               $username .= $characters[rand(0, $charactersLength - 1)];
       }

       $serviceid = $vars['serviceid'];

       Capsule::table('tblhosting')
       ->where('id', '$serviceid')
       ->update(
               [
               'username' => '$username',
               ]
       );
});

 

My issue is username is still the emailadress and not the random username i set in my hook.

The freeradius module is working well, my only issue is the hook.

 

 

Thanks to help.

Link to comment
Share on other sites

For starters, you shouldn't need the require line. Hook files in module directories should autmatically be included. Unless that's just addon modules, but I've never actually had to use that line. They just work. This may very well be causing the problem

 

while not familiar directly with the specific hook, that code looks solid enough to work. What I'd do at this point is hook in something like chromephp or even use logactivity to verify your hook is being called, and go from there

Link to comment
Share on other sites

there is already ActionHook point for this specific need, try the following code instead of yours

<?php

add_hook('OverrideModuleUsernameGeneration', 1, function($vars) 
{ 

       $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
       $charactersLength = strlen($characters); 
       $username = "";

       for ($i = 0; $i < 10; $i++) { 
               $username .= $characters[rand(0, $charactersLength - 1)]; 
       } 

       return $username;
});

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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • 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