[evolvis-commits] r13339: Generate a loginname if not provided and email is guaranteed to be unique

mirabilos at evolvis.org mirabilos at evolvis.org
Mon Feb 28 02:32:11 CET 2011


Author: mirabilos
Date: 2011-02-28 02:32:11 +0100 (Mon, 28 Feb 2011)
New Revision: 13339

Modified:
   trunk/gforge_base/evolvisforge-5.1/gforge/common/include/User.class.php
   trunk/gforge_base/evolvisforge-5.1/gforge/www/account/register.php
Log:
Generate a loginname if not provided and email is guaranteed to be unique

Modified: trunk/gforge_base/evolvisforge-5.1/gforge/common/include/User.class.php
===================================================================
--- trunk/gforge_base/evolvisforge-5.1/gforge/common/include/User.class.php	2011-02-28 01:32:09 UTC (rev 13338)
+++ trunk/gforge_base/evolvisforge-5.1/gforge/common/include/User.class.php	2011-02-28 01:32:11 UTC (rev 13339)
@@ -219,9 +219,11 @@
 			$this->setError(_('You must supply a theme'));
 			return false;
 		}
-		if (!$unix_name) {
-			$this->setError(_('You must supply a username'));
-			return false;
+		if (! $GLOBALS['sys_require_unique_email']) {
+			if (!$unix_name) {
+				$this->setError(_('You must supply a username'));
+				return false;
+			}
 		}
 		if (!$firstname) {
 			$this->setError(_('You must supply a first name'));
@@ -243,11 +245,6 @@
 			$this->setError(_('Invalid Password:'));
 			return false;
 		}
-		$unix_name=strtolower($unix_name);
-		if (!account_namevalid($unix_name)) {
-			$this->setError(_('Invalid Unix Name.'));
-			return false;
-		}
 		if (!validate_email($email)) {
 			$this->setError(_('Invalid Email Address'));
 			return false;
@@ -261,8 +258,8 @@
 		} else {
 			$jabber_only=1;
 		}
-		if (db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name LIKE $1',
-					       array ($unix_name))) > 0) {
+		if ($unix_name && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name LIKE $1',
+							     array ($unix_name))) > 0) {
 			$this->setError(_('That username already exists.'));
 			return false;
 		}
@@ -273,6 +270,44 @@
 				return false;
 			}
 		}
+		if ($GLOBALS['sys_require_unique_email'] && !$unix_name) {
+			// Let's generate a loginname for the user
+			// ...based on the email address:
+			$email_array = explode ('@', $email, 2) ;
+			$email_u = $email_array [0] ;
+			$l = ereg_replace ('[^a-z0-9]', '', $email_u) ;
+			$l = substr ($l, 0, 15) ;
+			// Is the user part of the email address okay?
+			if (account_namevalid($l)
+			    && db_numrows(db_query("SELECT user_id FROM users WHERE user_name = '$l'")) == 0) {
+				$unix_name = $l ;
+			} else {
+				// No? What if we add a number at the end?
+				$i = 0 ;
+				while ($i < 1000) {
+					$c = substr ($l, 0, 15-strlen ("$i")) . "$i" ;
+					if (account_namevalid($c)
+					    && db_numrows(db_query("SELECT user_id FROM users WHERE user_name = '$c'")) == 0) {
+						$unix_name = $c ;
+						break;
+					}
+					$i++ ;
+				}
+			}
+			// If we're really unlucky, then let's go brute-force
+			while (!$unix_name) {
+				$c = substr (md5($email . rand()), 0, 15) ;
+				if (account_namevalid($c)
+				    && db_numrows(db_query("SELECT user_id FROM users WHERE user_name = '$c'")) == 0) {
+					$unix_name = $c ;
+				}
+			}
+		}
+		$unix_name=strtolower($unix_name);
+		if (!account_namevalid($unix_name)) {
+			$this->setError(_('Invalid Unix Name.'));
+			return false;
+		}
 		// if we got this far, it must be good
 		$confirm_hash = substr(md5($password1 . rand() . microtime()),0,16);
 		db_begin();

Modified: trunk/gforge_base/evolvisforge-5.1/gforge/www/account/register.php
===================================================================
--- trunk/gforge_base/evolvisforge-5.1/gforge/www/account/register.php	2011-02-28 01:32:09 UTC (rev 13338)
+++ trunk/gforge_base/evolvisforge-5.1/gforge/www/account/register.php	2011-02-28 01:32:11 UTC (rev 13339)
@@ -111,7 +111,12 @@
 <form action="<?php echo util_make_url('/account/register.php'); ?>" method="post">
 <input type="hidden" name="form_key" value="<?php echo form_generate_key(); ?>"/>
 <p>
-<?php echo _('Login Name (do not use uppercase letters):'); echo utils_requiredField(); ?><br />
+<?php
+if ($GLOBALS['sys_require_unique_email']) {
+	echo _('Login Name (no uppercase letters; leave empty to have it generated automatically)');
+} else {
+	echo _('Login Name (do not use uppercase letters):'); echo utils_requiredField();
+} ?><br />
 <input type="text" name="unix_name" value="<?php print(htmlspecialchars(stripslashes($unix_name))); ?>" />
 </p>
 <p>



More information about the evolvis-commits mailing list