2009-02-24 Esteban Sanchez <estebans@artica.es>

* include/auth/mysql.php: Added get_user_id(). Removed field check on
	create_user(). Allow to pass an structure to the functions and use
	get_user_id() when needed.

	* include/auth/ldap.php: Added get_user_id().

	* include/functions_db.php: Improved get_group_users() to return real
	users instead of tusuario_perfil values. Also a filter parameter was
	added.
	
	* include/functions_messages.php: Use get_user_id() on
	create_message_group() because get_group_users() now returns
	user structures. Style correction.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1477 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Esteban Sanchez 2009-02-24 11:20:34 +00:00
parent 206804a9e1
commit 12e7f2dfc6
5 changed files with 97 additions and 52 deletions

View File

@ -1,3 +1,19 @@
2009-02-24 Esteban Sanchez <estebans@artica.es>
* include/auth/mysql.php: Added get_user_id(). Removed field check on
create_user(). Allow to pass an structure to the functions and use
get_user_id() when needed.
* include/auth/ldap.php: Added get_user_id().
* include/functions_db.php: Improved get_group_users() to return real
users instead of tusuario_perfil values. Also a filter parameter was
added.
* include/functions_messages.php: Use get_user_id() on
create_message_group() because get_group_users() now returns
user structures. Style correction.
2009-02-24 Esteban Sanchez <estebans@artica.es>
* include/functions_db.php: Added get_db_row_filter(). Added

View File

@ -120,6 +120,21 @@ function get_user_email ($id_user) {
return (string) $info["email"];
}
/**
* Get the user id field on a mixed structure.
*
* This function is needed to make auth system more compatible and independant.
*
* @param mixed User structure to get id. It might be a row returned from
* tusuario or tusuario_perfil. If it's not a row, the int value is returned.
*/
function get_user_id ($user) {
if (is_array ($user))
/* FIXME: Is this right? */
return $user['id_user'];
return (int) $user;
}
/**
* Gets the users info
*
@ -526,4 +541,4 @@ foreach ($opt_keys as $key) {
$config["auth_error"] = &$ldap_cache["error"];
unset ($req_keys, $opt_keys);
?>
?>

View File

@ -68,15 +68,39 @@ function is_user_admin ($id_user) {
return (bool) get_db_value ('is_admin', 'tusuario', 'id_user', $id_user);
}
/**
* Get the user id field on a mixed structure.
*
* This function is needed to make auth system more compatible and independant.
*
* @param mixed User structure to get id. It might be a row returned from
* tusuario or tusuario_perfil. If it's not a row, the int value is returned.
*
* @return int User id of the mixed parameter.
*/
function get_user_id ($user) {
if (is_array ($user)){
if (isset ($user['id_user']))
return (int) $user['id_user'];
elseif (isset ($user['id_usuario']))
return (int) $user['id_usuario'];
else
return false;
} else {
return (int) $user;
}
}
/**
* Check is a user exists in the system
*
* @param string User id.
* @param mixed User id.
*
* @return bool True if the user exists.
*/
function is_user ($id_user) {
$user = get_db_row ('tusuario', 'id_user', $id_user);
function is_user ($user) {
$user = get_db_row ('tusuario', 'id_user', get_user_id ($user));
if (! $user)
return false;
return true;
@ -85,34 +109,34 @@ function is_user ($id_user) {
/**
* Gets the users real name
*
* @param string User id.
* @param mixed User id.
*
* @return string The users full name
*/
function get_user_fullname ($id_user) {
return (string) get_db_value ('fullname', 'tusuario', 'id_user', $id_user);
function get_user_fullname ($user) {
return (string) get_db_value ('fullname', 'tusuario', 'id_user', get_user_id ($user));
}
/**
* Gets the users email
*
* @param string User id.
* @param mixed User id.
*
* @return string The users email address
*/
function get_user_email ($id_user) {
return (string) get_db_value ('email', 'tusuario', 'id_user', $id_user);
function get_user_email ($user) {
return (string) get_db_value ('email', 'tusuario', 'id_user', get_user_id ($user));
}
/**
* Gets a Users info
*
* @param string User id
* @param mixed User id
*
* @return mixed An array of users
*/
function get_user_info ($id_user) {
return get_db_row ("tusuario", "id_user", $id_user);
function get_user_info ($user) {
return get_db_row ("tusuario", "id_user", get_user_id ($user));
}
/**
@ -162,27 +186,11 @@ function process_user_contact ($id_user) {
* @return bool false
*/
function create_user ($id_user, $password, $user_info) {
$values = array ();
$values = $user_info;
$values["id_user"] = $id_user;
$values["password"] = md5 ($password);
$values["last_connect"] = 0;
$values["registered"] = get_system_time ();
foreach ($user_info as $key => $value) {
switch ($key) {
case "fullname":
case "firstname":
case "lastname":
case "middlename":
case "comments":
case "email":
case "phone":
$values[$key] = $value;
break;
default:
continue; //ignore
}
}
return process_sql_insert ("tusuario", $values);
}

View File

@ -2796,21 +2796,25 @@ function process_sql_delete ($table, $where, $where_join = 'AND') {
*
* @return array An array with all the users or an empty array
*/
function get_group_users ($id_group) {
$result = get_db_value_filter ("id_usuario", "tusuario_perfil",
array ("id_grupo" => (int) $id_group));
function get_group_users ($id_group, $filter = false) {
if (! is_array ($filter))
$filter = array ();
$filter['id_grupo'] = (int) $id_group;
$result = get_db_all_rows_filter ("tusuario_perfil", $filter);
if ($result === false)
return array ();
//This removes stale users from the list. This can happen if switched to another auth scheme
//(internal users still exist) or external auth has users removed/inactivated from the list (eg. LDAP)
$retval = array ();
foreach ($result as $key => $user) {
if (!is_user ($user)) {
unset ($result[$key]);
} else {
array_push ($retval, get_user_info ($user));
}
}
if (empty ($result)) {
return array ();
}
return $result;
return $retval;
}
?>

View File

@ -34,12 +34,12 @@ function create_message ($usuario_origen, $usuario_destino, $subject, $mensaje)
return false; //Users don't exist so don't send to them
}
$values = array ("id_usuario_origen" => $usuario_origen,
"id_usuario_destino" => $usuario_destino,
"subject" => safe_input ($subject),
"mensaje" => safe_input ($mensaje),
"timestamp" => get_system_time ()
);
$values = array ();
$values["id_usuario_origen"] = $usuario_origen;
$values["id_usuario_destino"] = $usuario_destino;
$values["subject"] = safe_input ($subject);
$values["mensaje"] = safe_input ($mensaje);
$values["timestamp"] = get_system_time ();
$return = process_sql_insert ("tmensajes", $values);
@ -53,10 +53,10 @@ function create_message ($usuario_origen, $usuario_destino, $subject, $mensaje)
/**
* Creates private messages to be forwarded to groups
*
* @param string $usuario_origen The sender of the message
* @param string $dest_group The receivers (group) of the message
* @param string $subject Subject of the message (much like E-Mail)
* @param string $mensaje The actual message. This message will be cleaned by safe_input
* @param string The sender of the message
* @param string The receivers (group) of the message
* @param string Subject of the message (much like E-Mail)
* @param string The actual message. This message will be cleaned by safe_input
* (html is allowed but loose html chars will be translated)
*
* @return bool true when delivered, false in case of error
@ -65,10 +65,12 @@ function create_message_group ($usuario_origen, $dest_group, $subject, $mensaje)
$users = get_users_info ();
$group_users = get_group_users ($dest_group);
if (!array_key_exists ($usuario_origen, $users)) {
return false; //Users don't exist so don't send to them
if (! array_key_exists ($usuario_origen, $users)) {
//Users don't exist in the system
return false;
} elseif (empty ($group_users)) {
return true; //There are no users in the group, so it hasn't failed although it hasn't done anything.
//There are no users in the group, so it hasn't failed although it hasn't done anything.
return true;
}
//Start transaction so that if it fails somewhere along the way, we roll back
@ -76,7 +78,7 @@ function create_message_group ($usuario_origen, $dest_group, $subject, $mensaje)
process_sql ("START TRANSACTION;");
foreach ($group_users as $user) {
$return = create_message ($usuario_origen, $user, $subject, $mensaje);
$return = create_message ($usuario_origen, get_user_id ($user), $subject, $mensaje);
if ($return === false) {
//Error sending message, rollback and return false
process_sql ("ROLLBACK;");
@ -217,4 +219,4 @@ function get_message_overview ($order = "status", $order_dir = "ASC") {
return $result;
}
?>
?>