Jump to content

Quick Password Generator


ike

Recommended Posts

I thought it would be useful to have a quick "generate random password" button on a few admin pages. I couldn't find editable template files for the admin pages (other than a few of the basic .tpl files), so I decided to implement it with Javascript. Basically, the JS looks for password input boxes, and then adds a "Generate" button after them.

 

This should be considered experimental as I just wrote it tonight, but it seems to be working very well in my tests. Here's what it looks like:

7NRZJ.png

4XC5k.png

QaX0W.png

 

Here's what you do:

Create a new file, /whmcs_path/admin/autoPassword.js

Content:

// AddAutoPasswordButtons: Look for password boxes and add a generate password button next to tehm
function AddAutoPasswordButtons() {
// Don't add the Generate button to certain pages where it wouldn't make sense
var NoGenerate = [
	'WHMCS - Servers',
	'WHMCS - Support Ticket Departments'
];
for (i=0; i<NoGenerate.length; i++) if (document.title == NoGenerate[i]) return;

// Get the Password text box, if there is one
var e = document.getElementsByName("password");
if (e.length>0)
{
	var b = document.createElement("input");
	b.type    = "button";
	b.value   = "Generate";
	b.onclick = GeneratePassword;
	e[0].parentNode.appendChild(b);
}
}

// GeneratePassword: Generate a password and place it in a textbox
function GeneratePassword() {
var p = GetRandomPassword();
var e = document.getElementsByName("password");
if (e.length>0) {
	e[0].value = p;
	if (e[0].type == "password") e[0].type = "text";
}
// For a "confirm password" field
e = document.getElementsByName("password2");
if (e.length>0) {
	e[0].value = p;
	if (e[0].type == "password") e[0].type = "text";
}
}

// GetRandomNumber: Get a random number between two specified numbers
function GetRandomNumber(lbound,ubound) {
return (Math.floor(Math.random() * (ubound-lbound)) + lbound);
}

// GetRandomCharacter: Get a random character
function GetRandomCharacter() {
var chars = ""; // Choose your character sets below
chars += "0123456789";
chars += "abcdefghijklmnopqrstuvwxyz";
chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

return chars.charAt(GetRandomNumber(0,chars.length));
}

// GetRandomPassword: Get a random password
function GetRandomPassword() {
var p = "";
for (i=0; i<8; i++) p += GetRandomCharacter();
return p;
}

//------------------------------------------------------------------------------

// Adding events to onLoad
// Source: http://onlinetools.org/articles/unobtrusivejavascript/chapter4.html
function addEvent(obj, evType, fn){ 
if (obj.addEventListener){ 
  obj.addEventListener(evType, fn, false); 
  return true; 
} else if (obj.attachEvent){ 
  var r = obj.attachEvent("on"+evType, fn); 
  return r; 
} else { 
  return false; 
} 
}

// Hook AddAutoPasswordButtons to window load
addEvent(window,'load',AddAutoPasswordButtons);

 

Then, we just need to edit /whmcs_path/admin/templates/v4/header.tpl to reference the new .js file.

I added the following on line 39 just before </head>:

<script type="text/javascript" src="autoPassword.js"></script>

This is what it looks like in context:

  });{/literal}
 {$jscode}
</script>
[b]<script type="text/javascript" src="autoPassword.js"></script>[/b]
</head>
<body>
<div id="topnav">

 

That's it! Let me know if you find this useful or see any problems. I'll update this thread if I make any changes.

Link to comment
Share on other sites

  • Replies 53
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

A nice tweak to this, would be to add it to the client registration form also on checkout, could this be done?

 

Good idea. The current version can't be implemented as-is on the customer portal for a couple reasons but it will definitely be possible with a few changes.

 

Cool. Don't forget to add it to the community contributions system in the client area!

 

I should have a new version later tonight or tomorrow. I'll post an update here and submit it to the community contributions when it's ready.

Link to comment
Share on other sites

In Administrators ocorred one error

 

 

Detalhes dos erros da página da Web

 

Agente de Usuário: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729)

Carimbo de data/hora: Fri, 3 Jul 2009 18:05:48 UTC

 

 

Mensagem: Unable to get the property type. There is no support for this command.

Linha: 28

Caractere: 32

Código: 0

URI: https://www.xxx.net/xxx/admin/autoPassword.js

 

 

Mensagem: Unable to get the property type. There is no support for this command.

Linha: 28

Caractere: 32

Código: 0

URI: https://www.xxx.net/xxx/admin/autoPassword.js

 

 

Mensagem: Unable to get the property type. There is no support for this command.

Linha: 28

Caractere: 32

Código: 0

URI: https://www.xxx.net/xxx/admin/autoPassword.js

 

Please help!!!

Link to comment
Share on other sites

Hi everyone,

 

I have just submitted the addon to the community contributions system and it is awaiting approval. In the mean time, you can download the new version of the addon here:

http://ike.to/whmcs_addons/password_generator/pwgen_1.0.zip

 

Note that I changed the Javascript file's name and location for consistency. Also, the header.tpl modification in the admin area has changed slightly. Full installation instructions are included in the zip file.

 

Other changes:

  • Added client-side integration instructions for the checkout page
  • Fixed the IE bug mentioned by ipglobe
  • Changed the "Generate" button placement so it will always be next to the password field

Link to comment
Share on other sites

I am using default cart and I tried to add, it came error. Cuz the example file are for v4, I am using default themes...

 

Did you copy my file for the "cart" orderform into your "default" orderform? If so, that would cause a problem. I didn't make examples for the other carts provided by the system, I could though if you want.

 

Upload a copy of the file you modified and I'll take a look.

Link to comment
Share on other sites

Actually, it just occurred to me that I think you meant you're using the "default" client-side theme as opposed to the "portal" theme. In that case, just edit the /templates/default/header.tpl file the same way you would edit the /templates/portal/header.tpl file.

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

Suggestions

 

Include: password_generator.js

 

( see attach file: clientareachangepw.tpl.jpg )

 

// compatibility for the template clientareachangepw.tpl

 

function GeneratePassword2() {

var p = GetRandomPassword();

var e = document.getElementsByName("newpw");

if (e.length>0) {

e[0].value = p;

if (e[0].type == "password") changeInputType(e[0],"text");

}

// For a "confirm password" field

e = document.getElementsByName("confirmpw");

if (e.length>0) {

e[0].value = p;

if (e[0].type == "password") changeInputType(e[0],"text");

}

}

 

in clientareachangepw.tpl

 

<input type="button" value="Gerar Senha" onclick="GeneratePassword2();"/>

 

 

Another suggestion:

 

/clientregister.tpl ( see attach file: clientregister.tpl.jpg )

 

Congratulations

clientareachangepw.tpl.jpg

clientregister.tpl.jpg

Link to comment
Share on other sites

  • 4 months later...

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