fix up cli
[Pman.Core] / DataObjects / Person.php
1 <?php
2 /**
3  * Table Definition for Person
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Person extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'Person';                          // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $email;                           // string(128)  not_null
15     
16     public $company_id;                      // int(11)  
17     public $office_id;                       // int(11)  
18     
19     public $name;                            // string(128)  not_null
20     public $firstname;                            // string(128)  not_null
21     public $lastname;                            // string(128)  not_null
22     public $phone;                           // string(32)  not_null
23     public $fax;                             // string(32)  not_null
24     
25     public $role;                            // string(32)  not_null
26     public $remarks;                         // blob(65535)  not_null blob
27     public $passwd;                          // string(64)  not_null
28     public $owner_id;                        // int(11)  not_null
29     public $lang;                            // string(8)  
30     public $no_reset_sent;                   // int(11)  
31     public $action_type;                     // string(32)  
32     public $project_id;                      // int(11)
33
34     
35     public $active;                          // int(11)  not_null
36     public $deleted_by;                      // int(11)  not_null
37     public $deleted_dt;                      // datetime(19)  binary
38
39     
40     /* the code above is auto generated do not remove the tag below */
41     ###END_AUTOCODE
42     /**
43      *
44      * @param {String} $templateFile  (mail/XXXXXXX.txt) exclude the mail and .txt bit.
45      * @param {Array|Object} $args   data to send out..
46      * @return {Array|PEAR_Error} array of $recipents, $header, $body 
47      */
48     function buildMail($templateFile, $args)
49     {
50           
51         $args = (array) $args;
52         $content  = clone($this);
53         
54         foreach((array)$args as $k=>$v) {
55             $content->$k = $v;
56         }
57         
58         $ff = HTML_FlexyFramework::get();
59         
60         
61         //?? is this really the place for this???
62         if (
63                 !$ff->cli && 
64                 empty($args['no_auth']) &&
65                 !in_array($templateFile, array(
66                     // templates that can be sent without authentication.
67                      'password_reset' ,
68                      'password_welcome'
69                  ))
70             ) {
71             
72             $content->authUser = $this->getAuthUser();
73             if (!$content->authUser) {
74                 return PEAR::raiseError("Not authenticated");
75             }
76         }
77         
78         // should handle x-forwarded...
79         
80         $content->HTTP_HOST = isset($_SERVER["HTTP_HOST"]) ?
81             $_SERVER["HTTP_HOST"] :
82             (isset($ff->HTTP_HOST) ? $ff->HTTP_HOST : 'localhost');
83             
84         /* use the regex compiler, as it doesnt parse <tags */
85         require_once 'HTML/Template/Flexy.php';
86         $template = new HTML_Template_Flexy( array(
87             'compiler'    => 'Flexy',
88             'nonHTML' => true,
89             'filters' => array('SimpleTags','Mail'),
90             //     'debug'=>1,
91         ));
92         
93      
94          
95         
96         $template->compile("mail/$templateFile.txt");
97         
98         /* use variables from this object to ouput data. */
99         $mailtext = $template->bufferedOutputObject($content);
100         //echo "<PRE>";print_R($mailtext);
101         //print_R($mailtext);exit;
102         /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
103         require_once 'Mail/mimeDecode.php';
104         require_once 'Mail.php';
105         
106         $decoder = new Mail_mimeDecode($mailtext);
107         $parts = $decoder->getSendArray();
108         if (PEAR::isError($parts)) {
109             return $parts;
110             //echo "PROBLEM: {$parts->message}";
111             //exit;
112         } 
113         list($recipents,$headers,$body) = $parts;
114         $recipents = array($this->email);
115         if (!empty($content->bcc) && is_array($content->bcc)) {
116             $recipents =array_merge($recipents, $content->bcc);
117         }
118         $headers['Date'] = date('r');
119         
120         return array(
121             'recipients' => $recipents,
122             'headers'    => $headers,
123             'body'      => $body
124         );
125         
126         
127     }
128     
129     
130     /**
131      * send a template
132      * - user must be authenticate or args[no_auth] = true
133      *   or template = password_[reset|welcome]
134      * 
135      */
136     function sendTemplate($templateFile, $args)
137     {
138         
139         $ar = $this->buildMail($templateFile, $args);
140       
141         
142         //print_r($recipents);exit;
143         $mailOptions = PEAR::getStaticProperty('Mail','options');
144         $mail = Mail::factory("SMTP",$mailOptions);
145         
146         if (PEAR::isError($mail)) {
147             return $mail;
148         } 
149         $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
150         $ret = $mail->send($ar['recipients'],$ar['headers'],$ar['body']);
151         error_reporting($oe);
152        
153         return $ret;
154     
155     }
156     
157   
158     
159     
160     function getEmailFrom()
161     {
162         return '"' . addslashes($this->name) . '" <' . $this->email . '>';
163     }
164     function toEventString() 
165     {
166         return empty($this->name) ? $this->email : $this->name;
167     } 
168     function verifyAuth()
169     { 
170         $ff= HTML_FlexyFramework::get();
171         if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $this->company()->comptype) {
172             $ff->page->jerr("Login not permited to outside companies");
173         }
174         return true;
175         
176     }    
177    
178    
179     //   ---------------- authentication / passwords and keys stuff  ----------------
180     function isAuth()
181     {
182         $db = $this->getDatabaseConnection();
183         // we combine db + project names,
184         // otherwise if projects use different 'auth' objects
185         // then we get unserialize issues.
186         $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
187         
188         
189         @session_start();
190         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
191             // in session...
192             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
193             
194             $u = DB_DataObject::factory('Person');
195             if ($u->get($a->id)) { //&& strlen($u->passwd)) {
196                 $u->verifyAuth();
197                 
198                 return true;
199             }
200             
201             $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
202             
203         }
204         // local auth - 
205         $u = DB_DataObject::factory('Person');
206         $ff = HTML_FlexyFramework::get();
207         if (!empty($ff->Pman['local_autoauth']) && 
208             (!empty($_SERVER['SERVER_ADDR'])) &&
209             ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
210             ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
211             $u->get('email', $ff->Pman['local_autoauth'])
212         ) {
213             $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
214             return true;
215         }
216            
217         // http basic auth..
218         $u = DB_DataObject::factory('Person');
219
220         if (!empty($_SERVER['PHP_AUTH_USER']) 
221             &&
222             !empty($_SERVER['PHP_AUTH_PW'])
223             &&
224             $u->get('email', $_SERVER['PHP_AUTH_USER'])
225             &&
226             $u->checkPassword($_SERVER['PHP_AUTH_PW'])
227            ) {
228             $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
229             return true; 
230         }
231         
232         
233         
234         
235         // not in session or not matched...
236         $u = DB_DataObject::factory('Person');
237         $u->whereAdd(' LENGTH(passwd) > 0');
238         $n = $u->count();
239         $error =  PEAR::getStaticProperty('DB_DataObject','lastError');
240         if ($error) {
241             die($error->toString()); // not really a good thing to do...
242         }
243         if (!$n){ // authenticated as there are no users in the system...
244             return true;
245         }
246         
247         return false;
248         
249     }
250     function getAuthUser()
251     {
252         if (!$this->isAuth()) {
253             return false;
254         }
255         $db = $this->getDatabaseConnection();
256         $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
257         
258         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
259             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
260             
261             $u = DB_DataObject::factory('Person');
262             if ($u->get($a->id)) { /// && strlen($u->passwd)) {
263                 return clone($u);
264             }
265              
266         }
267         
268         $u = DB_DataObject::factory('Person');
269         $u->whereAdd(' LENGTH(passwd) > 0');
270         if (!$u->count()){
271             $u = DB_DataObject::factory('Person');
272             $u->id = -1;
273             return $u;
274             
275         }
276         return false;
277     }     
278     function login()
279     {
280         $this->isAuth(); // force session start..
281         $this->verifyAuth();
282         $db = $this->getDatabaseConnection();
283         // refresh admin group if we are logged in as one..
284         //DB_DataObject::debugLevel(1);
285         $g = DB_DataObject::factory('Groups');
286         $g->type = 0;
287         $g->get('name', 'Administrators');
288         $gm = DB_DataObject::Factory('Group_Members');
289         if (in_array($g->id,$gm->listGroupMembership($this))) {
290             // refresh admin groups.
291             $gr = DB_DataObject::Factory('Group_Rights');
292             $gr->applyDefs($g, 0);
293         }
294              
295         $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
296         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
297         
298     }
299     function logout()
300     {
301         $this->isAuth(); // force session start..
302          $db = $this->getDatabaseConnection();
303         $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
304         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
305         
306     }    
307     function genPassKey ($t) 
308     {
309         return md5($this->email . $t. $this->passwd);
310     }
311     function simpleAuthKey($m = 0)
312     {
313         $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
314         
315         return md5(implode(',' ,  array($month, $this->email , $this->passwd, $this->id)));
316     } 
317     function checkPassword($val)
318     {
319         
320         if (substr($this->passwd,0,1) == '$') {
321             return crypt($val,$this->passwd) == $this->passwd ;
322         }
323         // old style md5 passwords...- cant be used with courier....
324         return md5($val) == $this->passwd;
325     }
326     function setPassword($value) 
327     {
328         $salt='';
329         while(strlen($salt)<9) {
330             $salt.=chr(rand(64,126));
331             //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
332         }
333         $this->passwd = crypt($value, '$1$'. $salt. '$');
334        
335        
336     }      
337     
338     function company()
339     {
340         $x = DB_DataObject::factory('Companies');
341         $x->get($this->company_id);
342         return $x;
343     }
344     function loadCompany()
345     {
346         $this->company = $this->company();
347     }
348     
349     function active()
350     {
351         return $this->active;
352     }
353     function authUserName($n) // set username prior to acheck user exists query.
354     {
355         
356         $this->whereAdd('LENGTH(passwd) > 1'); 
357         $this->email = $n;
358     }
359     function lang($val)
360     {
361         if ($val == $this->lang) {
362             return;
363         }
364         $uu = clone($this);
365         $this->lang = $val;
366         $this->update($uu);
367
368     }
369             
370     
371     function authUserArray()
372     {
373         
374         $aur = $this->toArray();
375         
376         if ($this->id < 1) {
377             return $aur;
378         }
379         
380         
381         //DB_DataObject::debugLevel(1);
382         $c = DB_Dataobject::factory('Companies');
383         $im = DB_Dataobject::factory('Images');
384         $c->joinAdd($im, 'LEFT');
385         $c->selectAdd();
386         $c->selectAs($c, 'company_id_%s');
387         $c->selectAs($im, 'company_id_logo_id_%s');
388         $c->id = $this->company_id;
389         $c->limit(1);
390         $c->find(true);
391         
392         $aur = array_merge( $c->toArray(),$aur);
393         
394         if (empty($c->company_id_logo_id_id))  {
395                  
396             $im = DB_Dataobject::factory('Images');
397             $im->ontable = 'Companies';
398             $im->onid = $c->id;
399             $im->imgtype = 'LOGO';
400             $im->limit(1);
401             $im->selectAdd();
402             $im->selectAs($im,  'company_id_logo_id_%s');
403             if ($im->find(true)) {
404                     
405                 foreach($im->toArray() as $k=>$v) {
406                     $aur[$k] = $v;
407                 }
408             }
409         }
410       
411         // perms + groups.
412         $aur['perms']  = $this->getPerms();
413         $g = DB_DataObject::Factory('Group_Members');
414         $aur['groups']  = $g->listGroupMembership($this, 'name');
415         
416         $aur['passwd'] = '';
417         $aur['dailykey'] = '';
418         
419         
420         
421         return $aur;
422     }
423     
424     //   ----------PERMS------  ----------------
425     function getPerms() 
426     {
427          //DB_DataObject::debugLevel(1);
428         // find out all the groups they are a member of.. + Default..
429         
430         // ------ INIITIALIZE IF NO GROUPS ARE SET UP.
431         
432         $g = DB_DataObject::Factory('Group_Rights');
433         if (!$g->count()) {
434             $g->genDefault();
435         }
436         
437         if ($this->id < 0) {
438             return $g->adminRights(); // system is not set up - so they get full rights.
439         }
440         //DB_DataObject::debugLevel(1);
441         $g = DB_DataObject::Factory('Group_Members');
442         $g->whereAdd('group_id is NOT NULL AND user_id IS NOT NULL');
443         if (!$g->count()) {
444             // add the current user to the admin group..
445             $g = DB_DataObject::Factory('Groups');
446             if ($g->get('name', 'Administrators')) {
447                 $gm = DB_DataObject::Factory('Group_Members');
448                 $gm->group_id = $g->id;
449                 $gm->user_id = $this->id;
450                 $gm->insert();
451             }
452             
453         }
454         
455         // ------ STANDARD PERMISSION HANDLING.
456         
457         $g = DB_DataObject::Factory('Group_Members');
458         $grps = $g->listGroupMembership($this);
459        //var_dump($grps);
460         $isAdmin = $g->inAdmin;
461         //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
462         // the load all the perms for those groups, and add them all together..
463         // then load all those 
464         $g = DB_DataObject::Factory('Group_Rights');
465         $ret =  $g->listPermsFromGroupIds($grps, $isAdmin);
466         //echo '<PRE>';print_r($ret);
467         return $ret;
468          
469         
470     }
471     /**
472      *Basic group fetching - probably needs to filter by type eventually.
473      *
474      *@param String $what - fetchall() argument - eg. 'name' returns names of all groups that they are members of.
475      */
476     
477     function groups($what=false)
478     {
479         $g = DB_DataObject::Factory('Group_Members');
480         $grps = $g->listGroupMembership($this);
481         $g = DB_DataObject::Factory('Groups');
482         $g->whereAddIn('id', $grps, 'int');
483         return $g->fetchAll($what);
484         
485     }
486     
487     function hasPerm($name, $lvl) 
488     {
489         static $pcache = array();
490         
491         if (!isset($pcache[$this->id])) {
492             $pcache[$this->id] = $this->getPerms();
493         }
494        // echo "<PRE>";print_r($pcache[$au->id]);
495        // var_dump($pcache[$au->id]);
496         if (empty($pcache[$this->id][$name])) {
497             return false;
498         }
499         
500         return strpos($pcache[$this->id][$name], $lvl) > -1;
501         
502     }    
503     
504     //  ------------ROO HOOKS------------------------------------
505     function applyFilters($q, $au, $roo)
506     {
507         //DB_DataObject::DebugLevel(1);
508         if (!empty($q['query']['person_not_internal'])) {
509             $this->whereAdd(" join_company_id_id.isOwner = 0 ");
510         }
511         
512         
513         if (!empty($q['query']['person_internal_only_all'])) {
514             
515             
516             // must be internal and not current user (need for distribution list)
517             // user has a projectdirectory entry and role is not blank.
518             //DB_DataObject::DebugLevel(1);
519             $pd = DB_DataObject::factory('ProjectDirectory');
520             $pd->whereAdd("role != ''");
521             $pd->selectAdd();
522             $pd->selectAdd('distinct(person_id) as person_id');
523             $roled = $pd->fetchAll('person_id');
524             $rs = $roled  ? "  OR
525                     {$this->tableName()}.id IN (".implode(',', $roled) . ") 
526                     " : '';
527             $this->whereAdd(" join_company_id_id.comptype = 'OWNER' $rs ");
528             
529         }
530         // -- for distribution
531         if (!empty($q['query']['person_internal_only'])) {
532             // must be internal and not current user (need for distribution list)
533             $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
534             
535             //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
536             //    ".id  != ".$au->id);
537             $this->whereAdd("Person.id != {$au->id}");
538         } 
539         
540         if (!empty($q['query']['comptype_or_company_id'])) {
541            // DB_DataObject::debugLevel(1);
542             $bits = explode(',', $q['query']['comptype_or_company_id']);
543             $id = (int) array_pop($bits);
544             $ct = $this->escape($bits[0]);
545             
546             $this->whereAdd(" join_company_id_id.comptype = '$ct' OR Person.company_id = $id");
547             
548         }
549         
550         
551         // staff list..
552         if (!empty($q['query']['person_inactive'])) {
553            // DB_Dataobject::debugLevel(1);
554             $this->active = 1;
555         }
556         $tn_p = $this->tableName();
557         $tn_gm = DB_DataObject::Factory('Group_Members')->tableName();
558         $tn_g = DB_DataObject::Factory('Groups')->tableName();
559
560         ///---------------- Group views --------
561         if (!empty($q['query']['in_group'])) {
562             // DB_DataObject::debugLevel(1);
563             $ing = (int) $q['query']['in_group'];
564             if ($q['query']['in_group'] == -1) {
565              
566                 // list all staff who are not in a group.
567                 $this->whereAdd("Person.id NOT IN (
568                     SELECT distinct(user_id) FROM $tn_gm LEFT JOIN
569                         $tn_g ON $tn_g.id = $tn_gm.group_id
570                         WHERE $tn_g.type = ".$q['query']['type']."
571                     )");
572                 
573                 
574             } else {
575                 
576                 $this->whereAdd("$tn_p.id IN (
577                     SELECT distinct(user_id) FROM $tn_gm
578                         WHERE group_id = $ing
579                     )");
580                }
581             
582         }
583         
584         if (!empty($q['query']['not_in_directory'])) { 
585             // it's a Person list..
586             // DB_DATaobjecT::debugLevel(1);
587             
588             // specific to project directory which is single comp. login
589             //
590             $owncomp = DB_DataObject::Factory('Companies');
591             $owncomp->get('comptype', 'OWNER');
592             if ($q['company_id'] == $owncomp->id) {
593                 $this->active =1;
594             }
595             
596             
597
598             if ( $q['query']['not_in_directory'] > -1) {
599                 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
600                 // can list current - so that it does not break!!!
601                 $this->whereAdd("$tn_p.id NOT IN 
602                     ( SELECT distinct person_id FROM $tn_pd WHERE
603                         project_id = " . $q['query']['not_in_directory'] . " AND 
604                         company_id = " . $this->company_id . ')');
605             }
606         }
607            
608         if (!empty($q['query']['role'])) { 
609             // it's a Person list..
610             // DB_DATaobjecT::debugLevel(1);
611             
612             // specific to project directory which is single comp. login
613             //
614             $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
615                 // can list current - so that it does not break!!!
616             $this->whereAdd("$tn_p.id IN 
617                     ( SELECT distinct person_id FROM $tn_pd WHERE
618                         role = '". $this->escape($q['query']['role']) ."'
619             )");
620         
621         }
622         
623         
624         if (!empty($q['query']['project_member_of'])) {
625                // this is also a flag to return if they are a member..
626             //DB_DataObject::debugLevel(1);
627             $do = DB_DataObject::factory('ProjectDirectory');
628             $do->project_id = $q['query']['project_member_of'];
629             $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
630             $this->joinAdd($do,array('joinType' => 'LEFT', 'useWhereAsOn' => true));
631             $this->selectAdd("IF($tn_pd.id IS NULL, 0,  $tn_pd.id )  as is_member");
632                 
633                 
634             if (!empty($q['query']['project_member_filter'])) {
635                 $this->having('is_member !=0');
636             
637             }
638             
639         }
640         
641         
642         if (!empty($q['query']['search'])) {
643             $s = $this->escape($q['query']['search']);
644                     $this->whereAdd("
645                         $tn_p.name LIKE '%$s%'  OR
646                         $tn_p.email LIKE '%$s%'  OR
647                         $tn_p.role LIKE '%$s%'  OR
648                         $tn_p.remarks LIKE '%$s%' 
649                         
650                     ");
651         }
652         
653         //
654     }
655     function setFromRoo($ar, $roo)
656     {
657         $this->setFrom($ar);
658         if (!empty($ar['passwd1'])) {
659             $this->setPassword($ar['passwd1']);
660         }
661         
662         
663         if (    $this->id &&
664                 ($this->email == $roo->old->email)&&
665                 ($this->company_id == $roo->old->company_id)
666             ) {
667             return true;
668         }
669         if (empty($this->email)) {
670             return true;
671         }
672         $xx = DB_Dataobject::factory('Person');
673         $xx->setFrom(array(
674             'email' => $this->email,
675            // 'company_id' => $x->company_id
676         ));
677         
678         if ($xx->count()) {
679             return "Duplicate Email found";
680         }
681         return true;
682     }
683     /**
684      *
685      * before Delete - delete significant dependancies..
686      * this is called after checkPerm..
687      */
688     
689     function beforeDelete()
690     {
691         
692         $e = DB_DataObject::Factory('Events');
693         $e->whereAdd('person_id = ' . $this->id);
694         $e->delete(true);
695         
696         // anything else?  
697         
698     }
699     
700     
701     /***
702      * Check if the a user has access to modify this item.
703      * @param String $lvl Level (eg. Core.Projects)
704      * @param Pman_Core_DataObjects_Person $au The authenticated user.
705      * @param boolean $changes alllow changes???
706      *
707      * @return false if no access..
708      */
709     function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
710     {
711          
712        // do we have an empty system..
713         if ($au && $au->id == -1) {
714             return true;
715         }
716         
717         // determine if it's staff!!!
718          
719         if ($au->company()->comptype != 'OWNER') {
720             
721             // - can not change company!!!
722             if ($changes && 
723                 isset($changes['company_id']) && 
724                 $changes['company_id'] != $au->company_id) {
725                 return false;
726             }
727             // can only set new emails..
728             if ($changes && 
729                     !empty($this->email) && 
730                     isset($changes['email']) && 
731                     $changes['email'] != $this->email) {
732                 return false;
733             }
734             
735             // edit self... - what about other staff members...
736             
737             return $this->company_id == $au->company_id;
738         }
739          
740          
741         // yes, only owner company can mess with this...
742         $owncomp = DB_DataObject::Factory('Companies');
743         $owncomp->get('comptype', 'OWNER');
744         
745         $isStaff = ($this->company_id ==  $owncomp->id);
746         
747     
748         switch ($lvl) {
749             // extra case change passwod?
750             case 'P': //??? password
751                 // standard perms -- for editing + if the user is dowing them selves..
752                 $ret = $isStaff ? $au->hasPerm("Core.Staff", "E") : $au->hasPerm("Core.Person", "E");
753                 return $ret || $au->id == $this->id;
754             
755             default:                
756                 return $isStaff ? $au->hasPerm("Core.Staff", $lvl) : $au->hasPerm("Core.Person", $lvl);
757         
758         }
759         return false;
760     }
761     function onInsert($req, $roo)  
762     {
763         
764         if ($roo->authUser->id < 0) {
765             $g = DB_DataObject::factory('Groups');
766             $g->type = 0;
767             $g->get('name', 'Administrators');
768             
769             $p = DB_DataObject::factory('Group_Members');
770             $p->group_id = $g->id;
771             $p->user_id = $this->id;     
772             if (!$p->count()) {
773                 $p->insert();
774                 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
775             }
776             $this->login();
777         }
778         if (!empty($req['project_id_addto'])) {
779             $pd = DB_DataObject::factory('ProjectDirectory');
780             $pd->project_id = $req['project_id_addto'];
781             $pd->person_id = $this->id; 
782             $pd->ispm =0;
783             $pd->office_id = $this->office_id;
784             $pd->company_id = $this->company_id;
785             $pd->insert();
786         }
787         
788     }
789  }