From 7c37db8ace6aa37def9e7e926b8e8be34d4cde82 Mon Sep 17 00:00:00 2001 From: "matthias.grimm" Date: Sun, 8 May 2005 20:31:40 +0200 Subject: mysql create user function This patch adds the missing function createuser in the mysql auth module. Some new SQL statements have to be defined so that it works: $conf['auth']['mysql']['getgroupid'] to get the ID of a given group $conf['auth']['mysql']['adduser'] to add a user to the database $conf['auth']['mysql']['addusergroup'] to let the user join a given group darcs-hash:20050508183140-45302-de96a42fd79801a5e9ab14cb476f56b2c9432d7c.gz --- inc/auth_mysql.php | 61 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 10 deletions(-) (limited to 'inc/auth_mysql.php') diff --git a/inc/auth_mysql.php b/inc/auth_mysql.php index a4fafec3b..ac835ae17 100644 --- a/inc/auth_mysql.php +++ b/inc/auth_mysql.php @@ -43,10 +43,11 @@ function auth_mysql_runsql($sql_string) { $resultarray[]=$temparray; } mysql_free_result ($result); - } - if (mysql_insert_id($link)) { + } elseif (mysql_insert_id($link)) { $resultarray = mysql_insert_id($link); //give back ID on insert - } + } else + $resultarray = 0; // asure that the return value is valid + mysql_close ($link); return $resultarray; } @@ -55,7 +56,9 @@ function auth_mysql_runsql($sql_string) { * Check user+password [required auth function] * * Checks if the given user exists and the given - * plaintext password is correct + * plaintext password is correct. Furtheron it + * might be checked wether the user is member of + * the right group * * @author Andreas Gohr * @return bool @@ -65,6 +68,7 @@ function auth_checkPass($user,$pass){ $cnf = $conf['auth']['mysql']; $sql = str_replace('%u',addslashes($user),$cnf['passcheck']); + $sql = str_replace('%g',addslashes($conf['defaultgroup']),$sql); $sql = str_replace('%p',addslashes($pass),$sql); $result = auth_mysql_runsql($sql); return(count($result)); @@ -107,14 +111,51 @@ function auth_getUserData($user){ /** * Create a new User [required auth function] * - * Not implemented + * user string username + * pass string password + * name string full name of the user + * mail string email address * - * @author Andreas Gohr + * Returns false if the user already exists, null when an error + * occoured and the cleartext password of the new user if + * everything went well. + * + * The user HAS TO be added to the default group by this + * function + * + * @author Matthias Grimm */ -function auth_createUser($user,$name,$mail){ - msg("Sorry. Creating users is not supported by the MySQL backend, yet",-1); - return null; -} +function auth_createUser($user,$pass,$name,$mail){ + global $conf; + $cnf = $conf['auth']['mysql']; + + $info = auth_getUserData($user); + if ($info != false) return false; + + $sql = str_replace('%g',$conf['defaultgroup'],$cnf['getgroupid']); + $result = auth_mysql_runsql($sql); + + if (count($result) == 1) { + $gid = $result[0]['gid']; + + $sql = str_replace('%u',$user,$cnf['adduser']); + $sql = str_replace('%p',$pass,$sql); + $sql = str_replace('%n',$name,$sql); + $sql = str_replace('%e',$mail,$sql); + $uid = auth_mysql_runsql($sql); + + if ($uid != 0) { + $sql = str_replace('%uid',$uid,$cnf['addusergroup']); + $sql = str_replace('%gid',$gid,$sql); + auth_mysql_runsql($sql); + return $pass; + } else + msg("Registering of the new user '$user' failed!", -1); + } else + msg("The default group is not cleanly defined in the database!", -1); + return null; +} + //Setup VIM: ex: et ts=2 enc=utf-8 : -- cgit v1.2.3