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