*
* Copyright 1999-2001, VA Linux Systems, Inc.
* Copyright 2009-2010, Roland Mas
+ * Copyright 2011, Franck Villaume - Capgemini
* http://fusionforge.org
*
- * This file is part of FusionForge.
+ * This file is part of FusionForge. FusionForge is free software;
+ * you can redistribute it and/or modify it under the terms of the
+ * GNU General Public License as published by the Free Software
+ * Foundation; either version 2 of the Licence, or (at your option)
+ * any later version.
*
- * FusionForge is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published
- * by the Free Software Foundation; either version 2 of the License,
- * or (at your option) any later version.
- *
- * FusionForge is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
+ * FusionForge is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with FusionForge; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * You should have received a copy of the GNU General Public License along
+ * with FusionForge; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
$USER_OBJ=array();
/**
* user_get_object_by_name() - Get User object by username.
* user_get_object is useful so you can pool user objects/save database queries
- * You should always use this instead of instantiating the object directly
+ * You should always use this instead of instantiating the object directly
*
* @param string The unix username - required
* @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
/**
* user_get_object() - Get User object by user ID.
* user_get_object is useful so you can pool user objects/save database queries
- * You should always use this instead of instantiating the object directly
+ * You should always use this instead of instantiating the object directly
*
* @param int The ID of the user - required
* @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
function &user_get_object($user_id, $res = false) {
//create a common set of group objects
//saves a little wear on the database
-
- //automatically checks group_type and
+
+ //automatically checks group_type and
//returns appropriate object
-
+
global $USER_OBJ;
if (!isset($USER_OBJ["_".$user_id."_"])) {
if ($res) {
return user_get_objects($arr);
}
+function &user_get_objects_by_email($email_arr) {
+ $res=db_query_params('SELECT user_id FROM users WHERE lower(email) = ANY ($1)',
+ array(db_string_array_to_any_clause ($email_arr)));
+ $arr =& util_result_column_to_array($res, 0);
+ return user_get_objects($arr);
+}
+
function &user_get_active_users() {
$res=db_query_params('SELECT user_id FROM users WHERE status=$1',
array('A'));
}
class GFUser extends Error {
- /**
+ /**
* Associative array of data from db.
*
* @var array $data_array.
*/
var $data_array;
-
+
/**
* Is this person a site super-admin?
*
* @param int The user_id
* @param int The database result set OR array of data
*/
- function GFUser($id=false,$res=false) {
+ function GFUser($id = false, $res = false) {
$this->Error();
if (!$id) {
//setting up an empty object
$this->is_logged_in=false;
return true;
}
-
+
/**
* create() - Create a new user.
*
* @param string The users preferred default timezone.
* @param string The users preference for receiving site updates by email.
* @param string The users preference for receiving community updates by email.
- * @param int The ID of the language preference.
+ * @param int The ID of the language preference.
* @param string The users preferred timezone.
* @param string The users Jabber address.
- * @param int The users Jabber preference.
- * @param int The users theme_id.
+ * @param int The users Jabber preference.
+ * @param int The users theme_id.
* @param string The users unix_box.
* @param string The users address.
* @param string The users address part 2.
* @param string The users title.
* @param char(2) The users ISO country_code.
* @param bool Whether to send an email or not
+ * @param int The users preference for tooltips
* @returns The newly created user ID
*
*/
- function create($unix_name,$firstname,$lastname,$password1,$password2,$email,
- $mail_site,$mail_va,$language_id,$timezone,$jabber_address,$jabber_only,$theme_id,
- $unix_box='shell',$address='',$address2='',$phone='',$fax='',$title='',$ccode='US',$send_mail=true) {
+ function create($unix_name, $firstname, $lastname, $password1, $password2, $email,
+ $mail_site, $mail_va, $language_id, $timezone, $jabber_address, $jabber_only, $theme_id,
+ $unix_box = 'shell', $address = '', $address2 = '', $phone = '', $fax = '', $title = '', $ccode = 'US', $send_mail = true, $tooltips = true) {
global $SYS;
if (!$theme_id) {
$this->setError(_('You must supply a theme'));
// ...based on the email address:
$email_array = explode ('@', $email, 2) ;
$email_u = $email_array [0];
- $l = ereg_replace('[^a-z0-9]', '', $email_u);
+ $l = preg_replace('/[^a-z0-9]/', '', $email_u);
$l = substr ($l, 0, 15);
// Is the user part of the email address okay?
if (account_namevalid($l)
}
// If we're really unlucky, then let's go brute-force
while (!$unix_name) {
- $c = substr (md5($email . rand()), 0, 15);
+ $c = substr (md5($email . util_randbytes()), 0, 15);
if (account_namevalid($c)
&& db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
array($c))) == 0) {
}
}
}
- $unix_name=strtolower($unix_name);
+ $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);
+ $confirm_hash = substr(md5($password1 . util_randbytes() . microtime()),0,16);
db_begin();
- $result = db_query_params ('INSERT INTO users (user_name,user_pw,unix_pw,realname,firstname,lastname,email,add_date,status,confirm_hash,mail_siteupdates,mail_va,language,timezone,jabber_address,jabber_only,unix_box,address,address2,phone,fax,title,ccode,theme_id) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24)',
- array ($unix_name,
- md5($password1),
- account_genunixpw($password1),
- htmlspecialchars($firstname.' '.$lastname),
- htmlspecialchars($firstname),
- htmlspecialchars($lastname),
- $email,
- time(),
- 'P',
- $confirm_hash,
- (($mail_site)?"1":"0"),
- (($mail_va)?"1":"0"),
- $language_id,
- $timezone,
- $jabber_address,
- $jabber_only,
- $unix_box,
- htmlspecialchars($address),
- htmlspecialchars($address2),
- htmlspecialchars($phone),
- htmlspecialchars($fax),
- htmlspecialchars($title),
- $ccode,
- $theme_id));
+ $result = db_query_params('INSERT INTO users (user_name,user_pw,unix_pw,realname,firstname,lastname,email,add_date,status,confirm_hash,mail_siteupdates,mail_va,language,timezone,jabber_address,jabber_only,unix_box,address,address2,phone,fax,title,ccode,theme_id,tooltips)
+ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25)',
+ array($unix_name,
+ md5($password1),
+ account_genunixpw($password1),
+ htmlspecialchars($firstname.' '.$lastname),
+ htmlspecialchars($firstname),
+ htmlspecialchars($lastname),
+ $email,
+ time(),
+ 'P',
+ $confirm_hash,
+ (($mail_site)?"1":"0"),
+ (($mail_va)?"1":"0"),
+ $language_id,
+ $timezone,
+ $jabber_address,
+ $jabber_only,
+ $unix_box,
+ htmlspecialchars($address),
+ htmlspecialchars($address2),
+ htmlspecialchars($phone),
+ htmlspecialchars($fax),
+ htmlspecialchars($title),
+ $ccode,
+ $theme_id,
+ $tooltips));
if (!$result) {
$this->setError(_('Insert Failed: ') . db_error());
db_rollback();
return false;
} else {
- $id = db_insertid($result,'users','user_id');
+ $id = db_insertid($result, 'users', 'user_id');
if (!$id) {
$this->setError('Could Not Get USERID: ' .db_error());
db_rollback();
$hook_params['user_id'] = $this->getID();
$hook_params['user_name'] = $unix_name;
$hook_params['user_password'] = $password1;
- plugin_hook ("user_create", $hook_params);
-
+ plugin_hook("user_create", $hook_params);
+
if ($send_mail) {
setup_gettext_from_lang_id($language_id);
$this->sendRegistrationEmail();
function sendRegistrationEmail() {
$message=stripcslashes(sprintf(_('Thank you for registering on the %3$s web site. You have
account with username %1$s created for you. In order
-to complete your registration, visit the following url:
+to complete your registration, visit the following url:
<%2$s>
-- the %3$s staff
'),
$this->getUnixName(),
- util_make_url ('/account/verify.php?confirm_hash=_'.$this->getConfirmHash()),
- forge_get_config ('forge_name')));
+ util_make_url('/account/verify.php?confirm_hash=_'.$this->getConfirmHash()),
+ forge_get_config('forge_name')));
util_send_message(
$this->getEmail(),
- sprintf(_('%1$s Account Registration'), forge_get_config ('forge_name')),
+ sprintf(_('%1$s Account Registration'), forge_get_config('forge_name')),
$message
);
}
db_rollback();
return false;
}
- $res = db_query_params ('DELETE FROM filemodule_monitor WHERE user_id=$1',
- array ($this->getID()));
+ $res = db_query_params('DELETE FROM filemodule_monitor WHERE user_id=$1',
+ array($this->getID()));
if (!$res) {
$this->setError('ERROR - ' . _('Could Not Delete From filemodule_monitor:') . ' '.db_error());
db_rollback();
* @param string The users fax.
* @param string The users title.
* @param string The users ccode.
+ * @param int The users preference for tooltips.
+ * @param string The users email.
*/
- function update($firstname,$lastname,$language_id,$timezone,$mail_site,$mail_va,$use_ratings,
- $jabber_address,$jabber_only,$theme_id,$address,$address2,$phone,$fax,$title,$ccode) {
+ function update($firstname, $lastname, $language_id, $timezone, $mail_site, $mail_va, $use_ratings,
+ $jabber_address, $jabber_only, $theme_id, $address, $address2, $phone, $fax, $title, $ccode, $tooltips, $email='') {
$mail_site = $mail_site ? 1 : 0;
$mail_va = $mail_va ? 1 : 0;
$block_ratings = $use_ratings ? 0 : 1;
return false;
}
if (!$jabber_only) {
- $jabber_only=0;
+ $jabber_only = 0;
} else {
- $jabber_only=1;
+ $jabber_only = 1;
}
db_begin();
- $res = db_query_params ('
- UPDATE users
- SET
- realname=$1,
- firstname=$2,
- lastname=$3,
- language=$4,
- timezone=$5,
- mail_siteupdates=$6,
- mail_va=$7,
- block_ratings=$8,
- jabber_address=$9,
- jabber_only=$10,
- address=$11,
- address2=$12,
- phone=$13,
- fax=$14,
- title=$15,
- ccode=$16,
- theme_id=$17
- WHERE user_id=$18',
- array (
- htmlspecialchars($firstname . ' ' .$lastname),
- htmlspecialchars($firstname),
- htmlspecialchars($lastname),
- $language_id,
- $timezone,
- $mail_site,
- $mail_va,
- $block_ratings,
- $jabber_address,
- $jabber_only,
- htmlspecialchars($address) ,
- htmlspecialchars($address2) ,
- htmlspecialchars($phone) ,
- htmlspecialchars($fax) ,
- htmlspecialchars($title) ,
- $ccode,
- $theme_id,
- $this->getID())) ;
+ $res = db_query_params('
+ UPDATE users
+ SET
+ realname=$1,
+ firstname=$2,
+ lastname=$3,
+ language=$4,
+ timezone=$5,
+ mail_siteupdates=$6,
+ mail_va=$7,
+ block_ratings=$8,
+ jabber_address=$9,
+ jabber_only=$10,
+ address=$11,
+ address2=$12,
+ phone=$13,
+ fax=$14,
+ title=$15,
+ ccode=$16,
+ theme_id=$17,
+ tooltips=$18
+ WHERE user_id=$19',
+ array (
+ htmlspecialchars($firstname . ' ' .$lastname),
+ htmlspecialchars($firstname),
+ htmlspecialchars($lastname),
+ $language_id,
+ $timezone,
+ $mail_site,
+ $mail_va,
+ $block_ratings,
+ $jabber_address,
+ $jabber_only,
+ htmlspecialchars($address),
+ htmlspecialchars($address2),
+ htmlspecialchars($phone),
+ htmlspecialchars($fax),
+ htmlspecialchars($title),
+ $ccode,
+ $theme_id,
+ $tooltips,
+ $this->getID()));
if (!$res) {
$this->setError(_('ERROR - Could Not Update User Object:'). ' ' .db_error());
db_rollback();
return false;
- } else {
- // If there's a transaction from using to not
- // using ratings, remove all rating made by the
- // user (ratings by others should not be removed,
- // as it opens possibility to abuse rate system)
- if (!$use_ratings && $this->usesRatings()) {
- db_query_params ('DELETE FROM user_ratings WHERE rated_by=$1',
- array($user_id));
- }
- if (!$this->fetchData($this->getID())) {
- db_rollback();
- return false;
- }
-
- $hook_params = array ();
- $hook_params['user'] = $this;
- $hook_params['user_id'] = $this->getID();
- plugin_hook ("user_update", $hook_params);
-
- db_commit();
- return true;
}
+
+ if ($email && $email != $this->getEmail()
+ && !$this->setEmail($email)) {
+ return false;
+ }
+
+ // If there's a transaction from using to not
+ // using ratings, remove all rating made by the
+ // user (ratings by others should not be removed,
+ // as it opens possibility to abuse rate system)
+ if (!$use_ratings && $this->usesRatings()) {
+ db_query_params('DELETE FROM user_ratings WHERE rated_by=$1',
+ array($user_id));
+ }
+ if (!$this->fetchData($this->getID())) {
+ db_rollback();
+ return false;
+ }
+
+ $hook_params = array ();
+ $hook_params['user'] = $this;
+ $hook_params['user_id'] = $this->getID();
+ plugin_hook ("user_update", $hook_params);
+
+ db_commit();
+ return true;
}
/**
$this->data_array = db_fetch_array($res);
return true;
}
-
+
/**
* getID - Simply return the user_id for this object.
*
$this->setError(_('ERROR: Invalid status value'));
return false;
}
+ if ($this->getStatus() != 'P' && $status == 'P') {
+ $this->setError('ERROR: You can\'t set pending status if user is suspend or active');
+ return false;
+ }
db_begin();
$res = db_query_params ('UPDATE users SET status=$1 WHERE user_id=$2',
$hook_params['user_id'] = $this->getID();
$hook_params['status'] = $status;
plugin_hook ("user_setstatus", $hook_params);
-
+
db_commit();
-
+
return true;
}
}
}
}
}
-
+
$this->data_array['unix_status']=$status;
db_commit();
return true;
function getMD5Passwd() {
return $this->data_array['user_pw'];
}
-
+
//Added to be compatible with codendi getUserPw function
function getUserPw() {
return $this->data_array['user_pw'];
function getEmail() {
return str_replace("\n", "", $this->data_array['email']);
}
-
+
/**
* getSha1Email - a SHA1 encoded hash of the email URI (including mailto: prefix)
- *
+ *
* @return string The SHA1 encoded value for the email
*/
function getSha1Email() {
$hook_params['user_id'] = $this->getID();
$hook_params['user_email'] = $email;
plugin_hook("user_setemail", $hook_params);
-
+
if (!$this->fetchData($this->getId())) {
db_rollback();
return false;
function setNewEmailAndHash($email, $hash='') {
if (!$hash) {
- $hash = substr(md5(strval(time()) . strval(mt_rand())), 0, 16);
+ $hash = substr(md5(strval(time()) . strval(util_randbytes())), 0, 16);
}
if (!$email || !validate_email($email)) {
* @return array Array of groups.
*/
function &getGroups($onlylocal = true) {
- $ids = array () ;
+ $ids = array();
foreach ($this->getRoles() as $r) {
if ($onlylocal) {
if ($r instanceof RoleExplicit
&& $r->getHomeProject() != NULL) {
- $ids[] = $r->getHomeProject()->getID() ;
+ $ids[] = $r->getHomeProject()->getID();
}
} else {
foreach ($r->getLinkedProjects() as $p) {
- $ids[] = $p->getID() ;
+ $ids[] = $p->getID();
}
}
}
$res = db_query_params('DELETE FROM user_preferences WHERE user_id=$1 AND preference_name=$2',
array ($this->getID(),
$preference_name));
- return $res;
+ return ((!$res || db_affected_rows($res) < 1) ? false : true);
}
/**
$preference_name,
$value,
time())) ;
- return $result;
}
}
+ return ((!$result || db_affected_rows($result) < 1) ? false : true);
}
/**
return true;
}
+ /**
+ * setMD5Passwd - Changes user's MD5 password.
+ *
+ * @param string The MD5-hashed password.
+ * @return boolean success.
+ */
+ function setMD5Passwd($md5) {
+ global $SYS;
+
+ db_begin();
+
+ if ($md5) {
+ $res = db_query_params('UPDATE users SET user_pw=$1 WHERE user_id=$2',
+ array($md5_pw,
+ $this->getID()));
+
+ if (!$res || db_affected_rows($res) < 1) {
+ $this->setError(_('ERROR - Could Not Change User Password:') . ' ' .db_error());
+ db_rollback();
+ return false;
+ }
+ }
+ db_commit();
+ return true;
+ }
+
+ /**
+ * setUnixPasswd - Changes user's Unix-hashed password.
+ *
+ * @param string The Unix-hashed password.
+ * @return boolean success.
+ */
+ function setUnixPasswd($unix) {
+ global $SYS;
+
+ db_begin();
+
+ if ($unix) {
+ $res = db_query_params('UPDATE users SET unix_pw=$1 WHERE user_id=$1',
+ array ($unix_pw,
+ $this->getID()));
+
+ if (!$res || db_affected_rows($res) < 1) {
+ $this->setError(_('ERROR - Could Not Change User Password:') . ' ' .db_error());
+ db_rollback();
+ return false;
+ }
+
+ // Now change system password, but only if corresponding
+ // entry exists (i.e. if user have shell access)
+ if ($SYS->sysCheckUser($this->getID())) {
+ if (!$SYS->sysUserSetAttribute($this->getID(),"userPassword",'{crypt}'.$unix)) {
+ $this->setError($SYS->getErrorMessage());
+ db_rollback();
+ return false;
+ }
+ }
+ }
+ db_commit();
+ return true;
+ }
+
/**
* usesRatings - whether user participates in rating system.
*
return !$this->data_array['block_ratings'];
}
+ /**
+ * usesTooltips - whether user enables or not tooltips.
+ *
+ * @return boolean success.
+ */
+ function usesTooltips() {
+ return $this->data_array['tooltips'];
+ }
+
/**
* getPlugins - get a list of all available user plugins
*
}
/**
- * usesPlugin - returns true if the user uses a particular plugin
+ * usesPlugin - returns true if the user uses a particular plugin
*
* @param string name of the plugin
* @return boolean whether plugin is being used or not
* @return boolean success.
*/
function getMailingsPrefs($mailing_id) {
- if ($mailing_id=='va') {
+ if ($mailing_id == 'va') {
return $this->data_array['mail_va'];
- } else if ($mailing_id=='site') {
+ } else if ($mailing_id == 'site') {
return $this->data_array['mail_siteupdates'];
} else {
return 0;
* @param boolean If false, disable general site mailings, else - all.
* @return boolean success.
*/
- function unsubscribeFromMailings($all=false) {
+ function unsubscribeFromMailings($all = false) {
$res1 = $res2 = $res3 = true;
$res1 = db_query_params ('UPDATE users SET mail_siteupdates=0, mail_va=0 WHERE user_id=$1',
array ($this->getID())) ;
if ($all) {
- $res2 = db_query_params ('DELETE FROM forum_monitored_forums WHERE user_id=$1',
+ $res2 = db_query_params('DELETE FROM forum_monitored_forums WHERE user_id=$1',
array ($this->getID())) ;
- $res3 = db_query_params ('DELETE FROM filemodule_monitor WHERE user_id=$1',
+ $res3 = db_query_params('DELETE FROM filemodule_monitor WHERE user_id=$1',
array ($this->getID())) ;
}
* @param object group object
* @return object Role object
*/
- function getRole(&$group) {
+ function getRole(&$g) {
+ if (is_int ($g) || is_string($g)) {
+ $group_id = $g;
+ } else {
+ $group_id = $g->getID();
+ }
foreach ($this->getRoles () as $r) {
- if ($r instanceof RoleExplicit
- && $r->getHomeProject() != NULL
- && $r->getHomeProject()->getID() == $group->getID()) {
- return $r;
+ if ($r instanceof RoleExplicit ) {
+ $homeproj = $r->getHomeProject();
+ if ($homeproj) {
+ if ($homeproj->getID() == $group_id) {
+ return $r;
+ }
+ }
}
}
return false;
}
- function getRoles () {
- return RBACEngine::getInstance()->getAvailableRolesForUser($this) ;
+ function getRoles() {
+ return RBACEngine::getInstance()->getAvailableRolesForUser($this);
}
/* Codendi Glue */
- function isMember($g,$type=0){
+ function isMember($g, $type = 0){
if (is_int ($g) || is_string($g)) {
$group = group_get_object ($g) ;
$group_id = $g ;
}
switch ($type) {
+ case 0:
+ default:
+ foreach ($this->getGroups() as $p) {
+ if ($p->getID() == $group_id) {
+ return true ;
+ }
+ }
+ return false ;
+ break;
case 'P2':
//pm admin
- return forge_check_perm_for_user($this,'pm_admin',$group_id) ;
- break;
+ return forge_check_perm_for_user($this, 'pm_admin', $group_id);
+ break;
case 'F2':
//forum admin
- return forge_check_perm_for_user($this,'forum_admin',$group_id) ;
- break;
+ return forge_check_perm_for_user($this, 'forum_admin', $group_id);
+ break;
case 'A':
//admin for this group
- return forge_check_perm_for_user($this,'project_admin',$group_id) ;
+ return forge_check_perm_for_user($this, 'project_admin', $group_id);
break;
case 'D1':
//document editor
- return forge_check_perm_for_user($this,'docman',$group_id,'admin') ;
- break;
- case '0':
- default:
- foreach ($this->getGroups() as $p) {
- if ($p->getID() == $group_id) {
- return true ;
- }
- }
- return false ;
+ return forge_check_perm_for_user($this, 'docman', $group_id, 'admin');
break;
}
}
/**
- * user_ismember() - DEPRECATED; DO NOT USE!
- *
+ * user_ismember() - DEPRECATED; DO NOT USE! (TODO: document what should be used instead)
+ * Replace user_ismember(1[, 'A']) with forge_check_global_perm('forge_admin')
+ * Replace user_ismember($group_id, 'A') with forge_check_perm('project_admin', $group_id)
+ * For now, keep user_ismember($group_id) alone
* @param int The Group ID
* @param int The Type
* @deprecated
}
/**
- * user_getname() - DEPRECATED; DO NOT USE!
+ * user_getname() - DEPRECATED; DO NOT USE! (TODO: document what should be used instead)
*
* @param int The User ID
* @deprecated