3 // SourceForge: Breaking Down the Barriers to Open Source Development
4 // Copyright 1999-2000 (c) The SourceForge Crew
5 // http://sourceforge.net
7 // $Id: User.class,v 1.27 2000/12/14 02:08:29 tperdue Exp $
12 User object by Tim Perdue, October 11, 2000
14 Sets up database results and preferences for a user and abstracts this info
16 GENERALLY YOU SHOULD NEVER INSTANTIATE THIS OBJECT DIRECTLY
17 USE user_get_object() to instantiate properly - this will pool the objects
18 and increase efficiency
25 * You can now optionally pass in a db result
26 * handle. If you do, it re-uses that query
27 * to instantiate the objects
29 * IMPORTANT! That db result must contain all fields
30 * from users table or you will have problems
37 * user_get_object is useful so you can pool user objects/save database queries
38 * You should always use this instead of instantiating the object directly
40 * @param user_unix_name required
41 * @param res result set handle ("SELECT * FROM USERS WHERE user_id=xx")
42 * @returns a user object or false on failure
46 function &user_get_object_by_name($user_name,$res=false) {
48 $res=db_query("SELECT * FROM users WHERE user_name='$user_name'");
50 return user_get_object(db_result($res,0,'user_id'),$res);
55 * user_get_object is useful so you can pool user objects/save database queries
56 * You should always use this instead of instantiating the object directly
58 * @param user_id required
59 * @param res result set handle ("SELECT * FROM USERS WHERE user_id=xx")
60 * @returns a user object or false on failure
64 function &user_get_object($user_id,$res=false) {
65 //create a common set of group objects
66 //saves a little wear on the database
68 //automatically checks group_type and
69 //returns appropriate object
72 if (!isset($USER_OBJ["_".$user_id."_"])) {
74 //the db result handle was passed in
76 $res=db_query("SELECT * FROM users WHERE user_id='$user_id'");
78 if (!$res || db_numrows($res) < 1) {
79 $USER_OBJ["_".$user_id."_"]=false;
81 $USER_OBJ["_".$user_id."_"]= new User($user_id,$res);
84 return $USER_OBJ["_".$user_id."_"];
87 class User extends Error {
88 //associative array of data from db
93 //database result set handle
96 //is this person a site super-admin?
99 //is this person the logged in user?
102 //array of preferences
106 * User($id,$res) - CONSTRUCTOR - GENERALLY DON'T USE THIS
108 * instead use the user_get_object() function call
109 * @param $id required - user_id
110 * @param $res optional - database result set
112 function User($id,$res=false) {
116 $this->db_result=db_query("SELECT * FROM users WHERE user_id='$id'");
118 $this->db_result=$res;
120 if (db_numrows($this->db_result) < 1) {
121 //function in class we extended
122 $this->setError('User Not Found');
123 $this->data_array=array();
125 //set up an associative array for use by other functions
127 db_reset_result($this->db_result);
129 $this->data_array=db_fetch_array($this->db_result);
131 $this->user_id=$this->data_array['user_id'];
132 $this->is_super_user=false;
133 $this->is_logged_in=false;
138 * getData() - Return database result handle for direct access
140 * Generally should NOT be used - here for supporting deprecated group.php
141 * @returns database result set handle
144 db_reset_result($this->db_result);
145 return $this->db_result;
149 * refreshUserData() - May need to refresh database fields
151 * if an update occurred and you need to access the updated info
153 function refreshUserData() {
154 $this->db_result=db_query("SELECT * FROM users WHERE user_id='". $this->getUserId() ."'");
155 $this->data_array=db_fetch_array($this->db_result);
159 * getUserId() - Simply return the user_id for this object
161 * @returns this user's user_id number
163 function getUserId() {
164 return $this->user_id;
170 * Statuses include P,A,D
171 * @returns this user's status flag
173 function getStatus() {
174 return $this->data_array['status'];
180 * Database field status of 'A' returns true
181 * @returns true or false
183 function isActive() {
184 if ($this->getStatus()=='A') {
192 * getUnixStatus() - Status of activation of unix account
194 * @returns (N)one, (A)ctive, (S)uspended or (D)eleted
196 function getUnixStatus() {
197 return $this->data_array['unix_status'];
201 * setUnixStatus() - Sets status of activation of unix account
203 * @returns true/false
205 function setUnixStatus($status) {
206 $res=db_query("UPDATE users ".
207 "SET unix_status='$status' ".
208 "WHERE user_id='". $this->getUserId()."'");
211 $this->setError('ERROR - Could Not Update User Unix Status');
214 $this->data_array['unix_status']=$status;
222 * @returns this user's unix/login name
224 function getUnixName() {
225 return strtolower($this->data_array['user_name']);
231 * @returns this user's unix crypted passwd
233 function getUnixPasswd() {
234 return $this->data_array['unix_pw'];
240 * @returns this user's MD5-crypted passwd
242 function getMD5Passwd() {
243 return $this->data_array['user_pw'];
249 * @returns this user's email address
251 function getEmail() {
252 return $this->data_array['email'];
258 * @sets user's email address
260 function setEmail($email) {
261 $res=db_query("UPDATE users ".
262 "SET email='$email' ".
263 "WHERE user_id='". $this->getUserId()."'");
266 $this->setError('ERROR - Could Not Update User Email');
269 $this->data_array['email']=$email;
277 * @returns this user's timezone setting
279 function getRealName() {
280 return $this->data_array['realname'];
286 * @returns this user's unix time since account was opened
288 function getAddDate() {
289 return $this->data_array['add_date'];
295 * @returns this user's timezone setting
297 function getTimeZone() {
298 return $this->data_array['timezone'];
304 * @returns this user's preferred shell
306 function getShell() {
307 return $this->data_array['shell'];
313 * @sets user's preferred shell
315 function setShell($shell) {
316 if (!sf_ldap_user_change_attribute($this->getUserId(),"loginShell",$shell)) {
317 $this->setError(sf_ldap_get_error_msg());
321 $res=db_query("UPDATE users ".
322 "SET shell='$shell' ".
323 "WHERE user_id='". $this->getUserId()."'");
326 $this->setError('ERROR - Could Not Update User Unix Shell');
329 $this->data_array['shell']=$shell;
337 * @returns this user's unix_uid
339 function getUnixUID() {
340 return $this->data_array['unix_uid'];
346 * @returns this user's language_id
348 function getLanguage() {
349 return $this->data_array['language_id'];
353 * setLoggedIn($val) - Really only used by session code
355 * @param $val optional
357 function setLoggedIn($val=true) {
358 $this->is_logged_in=$val;
360 //if this is the logged in user -
361 //see if they are a super user
362 $sql="SELECT * FROM user_group ".
363 "WHERE user_id='". $this->getUserId() ."' AND group_id='1' AND admin_flags='A'";
364 $result=db_query($sql);
365 if (!$result || db_numrows($result) < 1) {
366 $this->is_super_user=false;
368 $this->is_super_user=true;
376 * @returns true or false
378 function isLoggedIn() {
379 return $this->is_logged_in;
385 * @returns true or false
387 function isSuperUser() {
388 return $this->is_super_user;
392 * setPreference($preference_name,$value)
394 * @param $preference_name the unique field name for this preference
395 * @param $value the value you are setting this preference to
396 * @returns true or false on failure
398 function setPreference($preference_name,$value) {
399 $preference_name=strtolower(trim($preference_name));
400 $result=db_query("UPDATE user_preferences SET preference_value='$value',set_date='". time() ."' ".
401 "WHERE user_id='". $this->getUserId() ."' ".
402 "AND preference_name='$preference_name'");
403 if (db_affected_rows($result) < 1) {
405 $result=db_query("INSERT INTO user_preferences (user_id,preference_name,preference_value,set_date) ".
406 "VALUES ('". $this->getUserId() ."','$preference_name','$value','". time() ."')");
412 * getPreference($preference_name)
414 * @param $preference_name the unique field name for this preference
415 * @returns the preference or false on failure
417 function getPreference($preference_name) {
418 $preference_name=strtolower(trim($preference_name));
420 First check to see if we have already fetched the preferences
422 if ($this->user_pref) {
423 //echo "\n\nPrefs were fetched already";
424 if ($this->user_pref["$preference_name"]) {
425 //we have fetched prefs - return part of array
426 return $this->user_pref["$preference_name"];
428 //we have fetched prefs, but this pref hasn't been set
432 //we haven't returned prefs - go to the db
433 $result=db_query("SELECT preference_name,preference_value FROM user_preferences ".
434 "WHERE user_id='". $this->getUserId() ."'");
435 if (db_numrows($result) < 1) {
436 // echo "\n\nNo Prefs Found";
440 //iterate and put the results into an array
441 for ($i=0; $i<db_numrows($result); $i++) {
442 $pref[db_result($result,$i,'preference_name')]=db_result($result,$i,'preference_value');
444 $this->user_pref=$pref;
445 if ($this->user_pref["$preference_name"]) {
446 //we have fetched prefs - return part of array
447 return $this->user_pref["$preference_name"];
449 //we have fetched prefs, but this pref hasn't been set
457 * setUpUnixUID() - Sets up this user's unix_uid for shell access
459 * @returns true on success false on failure
461 function setUpUnixUID() {
462 global $sys_database_type;
463 if ($this->getUnixUID() > 1) {
465 // already have unix_uid
469 //get the next unix uid
472 hack to simulate sequences in mysql
474 if ($sys_database_type=='mysql') {
475 $res=db_query("INSERT INTO unix_uids (id) values ('')");
476 $unixid=db_insertid($res,'unix_uids','id');
477 db_free_result($res);
479 $res=db_query("SELECT nextval('unix_uid_seq')");
480 $unixid=db_result($res,0,0);
481 db_free_result($res);
484 $this->setError('ERROR - Could Not Get Next Unix UID');
487 $res=db_query("UPDATE users ".
488 "SET unix_status='A',unix_uid='$unixid' ".
489 "WHERE user_id='". $this->getUserId()."'");
491 if (!$res || db_affected_rows($res) < 1) {
492 $this->setError('ERROR - Could Not Update User Account Flags');
495 $this->data_array['unix_uid']=$unixid;
502 * setPasswd($passwd) - Changes user's password
504 * @returns true on success false on failure
506 function setPasswd($passwd) {
507 $unix_pw=account_genunixpw($passwd);
509 if (!sf_ldap_user_set_attribute($this->getUserId(),"userPassword",'{crypt}'.$unix_pw)) {
510 $this->setError(sf_ldap_get_error_msg());
514 $res=db_query("UPDATE users SET user_pw='" . md5($passwd) . "',"
515 . "unix_pw='" . $unix_pw . "' WHERE "
516 . "user_id=" . $this->getUserId());
518 if (!$res || db_affected_rows($res) < 1) {
519 $this->setError('ERROR - Could Not Change User Password');
532 Session wrapping code
539 * Get user_id of logged in user
542 function user_getid() {
545 return $G_SESSION->getUserId();
553 * See if user is logged in
555 function user_isloggedin() {
559 return $G_SESSION->isLoggedIn();
566 * user_is_super_user()
567 * Check if logged in user is super user
569 function user_is_super_user() {
572 return $G_SESSION->isSuperUser();
584 EVERYTHING BELOW HERE IS DEPRECATED
587 DO NOT USE FOR ANY NEW CODE
596 function user_ismember($group_id,$type=0) {
597 if (!user_isloggedin()) {
601 $project=&group_get_object($group_id);
603 $type=strtoupper($type);
608 return $project->userIsBugAdmin();
613 return $project->userIsPMAdmin();
618 return $project->userIsPatchAdmin();
623 return $project->userIsForumAdmin();
628 return $project->userIsSupportAdmin();
633 return $project->userIsMember();
637 //admin for this group
638 return $project->userIsAdmin();
643 return $project->userIsDocEditor();
648 return $project->userIsReleaseTechnician();
660 function user_getname($user_id = false) {
661 // use current user if one is not passed in
663 if (user_isloggedin()) {
664 $user=&user_get_object(user_getid());
666 return $user->getUnixName();
668 return 'Error getting user';
674 $user=&user_get_object($user_id);
676 return $user->getUnixName();
678 return 'Invalid User';
684 function user_getrealname($user_id) {
685 $user=&user_get_object($user_id);
687 return $user->getUnixName();
689 return 'Invalid User';
694 function user_get_result_set($user_id) {
695 $user=&user_get_object($user_id);
697 return $user->getData();
704 function user_get_result_set_from_unix($user_name) {
705 return db_query("SELECT * FROM users WHERE user_name='$user_name'");
709 function user_get_timezone() {
710 if (user_isloggedin()) {
711 $user=&user_get_object(user_getid());
712 return $user->getTimeZone();
719 function user_get_language() {
720 if (user_isloggedin()) {
721 $user=&user_get_object(user_getid());
722 return $user->getLanguage();
729 function &user_set_preference($preference_name,$value) {
730 if (user_isloggedin()) {
731 $user=&user_get_object(user_getid());
732 //echo "\n\nSetting Preference: ";
733 //$success=$user->setPreference($preference_name,$value);
736 return $user->setPreference($preference_name,$value);
743 function &user_get_preference($preference_name) {
744 if (user_isloggedin()) {
745 $user=&user_get_object(user_getid());
746 return $user->getPreference($preference_name);