DataObjects/Core_event_audit.php
[Pman.Core] / DataObjects / Person.php
index 0771cf8..08f805f 100644 (file)
@@ -18,14 +18,16 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     public $email;                           // string(128)  not_null
     public $company_id;                      // int(11)  
     public $role;                            // string(32)  not_null
-    public $active;                          // int(11)  
+    public $active;                          // int(11)  not_null
     public $remarks;                         // blob(65535)  not_null blob
     public $passwd;                          // string(64)  not_null
     public $owner_id;                        // int(11)  not_null
     public $lang;                            // string(8)  
     public $no_reset_sent;                   // int(11)  
-    public $project_id;                      // int(11)  
     public $action_type;                     // string(32)  
+    public $project_id;                      // int(11)  
+    public $deleted_by;                      // int(11)  not_null
+    public $deleted_dt;                      // datetime(19)  binary
 
     
     /* the code above is auto generated do not remove the tag below */
@@ -114,44 +116,17 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     {
         return empty($this->name) ? $this->email : $this->name;
     } 
-    function whereAddIn($key, $list, $type) {
-        $ar = array();
-        foreach($list as $k) {
-            $ar[] = $type =='int' ? (int)$k : $this->escape($k);
-        }
-        if (!$ar) {
-            return;
-        }
-        $this->whereAdd("$key IN (". implode(',', $ar). ')');
-    }
-    function fetchAll($k= false, $v = false) 
-    {
-        // should it even do this!!!?!?
-        if ($k !== false && 
-                (   // only do this is we have not been explicit..
-                    empty($this->_query['data_select']) || 
-                    ($this->_query['data_select'] == '*')
-                )
-            ) {
-            $this->selectAdd();
-            $this->selectAdd($k);
-            if ($v !== false) {
-                $this->selectAdd($v);
-            }
+    function verifyAuth()
+    { 
+        $ff= HTML_FlexyFramework::get();
+        if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $this->company()->comptype) {
+            $ff->page->jerr("Login not permited to outside companies");
         }
+        return true;
         
-        $this->find();
-        $ret = array();
-        while ($this->fetch()) {
-            if ($v !== false) {
-                $ret[$this->$k] = $this->$v;
-                continue;
-            }
-            $ret[] = $k === false ? clone($this) : $this->$k;
-        }
-        return $ret;
-         
-    }
+    }    
+   
+   
     //   ---------------- authentication / passwords and keys stuff  ----------------
     function isAuth()
     {
@@ -163,11 +138,30 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
             $u = DB_DataObject::factory('Person');
             if ($u->get($a->id)) { //&& strlen($u->passwd)) {
+                $u->verifyAuth();
+                
                 return true;
             }
+            
             $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
             
         }
+        // local auth - 
+        $u = DB_DataObject::factory('Person');
+        $ff = HTML_FlexyFramework::get();
+        if (!empty($ff->Pman['local_autoauth']) && 
+            (!empty($_SERVER['SERVER_ADDR'])) &&
+            ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
+            ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
+            $u->get('email', $ff->Pman['local_autoauth'])
+        ) {
+            $db = $this->getDatabaseConnection();
+            $sesPrefix = $db->dsn['database'];
+            $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
+            return true;
+        }
+           
+        
         // not in session or not matched...
         $u = DB_DataObject::factory('Person');
         $u->whereAdd(' LENGTH(passwd) > 0');
@@ -213,6 +207,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     function login()
     {
         $this->isAuth(); // force session start..
+        $this->verifyAuth();
          $db = $this->getDatabaseConnection();
         $sesPrefix = $db->dsn['database'];
         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
@@ -238,6 +233,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     } 
     function checkPassword($val)
     {
+        
         if (substr($this->passwd,0,1) == '$') {
             return crypt($val,$this->passwd) == $this->passwd ;
         }
@@ -290,6 +286,9 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         
         $aur = $this->toArray();
         
+        if ($this->id < 1) {
+            return $aur;
+        }
         
         
         //DB_DataObject::debugLevel(1);
@@ -320,19 +319,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
                 }
             }
         }
-        /*
-        now handled by interface...
-        
-        $lang = empty($this->lang) ? 'en' : $this->lang;
-        if (empty($_SESSION['Pman_I18N'][$lang])) {
-            require_once 'Pman/I18N.php';
-            $x = new Pman_I18N();
-            $x->setSession($this);
-            
-        }
-        
-        $aur['i18n'] =$_SESSION['Pman_I18N'][$lang];
-        */
+      
         // perms + groups.
         $aur['perms']  = $this->getPerms();
         $g = DB_DataObject::Factory('Group_Members');
@@ -351,27 +338,62 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     {
          //DB_DataObject::debugLevel(1);
         // find out all the groups they are a member of.. + Default..
+        
+        // ------ INIITIALIZE IF NO GROUPS ARE SET UP.
+        
         $g = DB_DataObject::Factory('Group_Rights');
         if (!$g->count()) {
             $g->genDefault();
         }
+        
         if ($this->id < 0) {
-            return $g->adminRights();
+            return $g->adminRights(); // system is not set up - so they get full rights.
         }
         
+        $g = DB_DataObject::Factory('Group_Members');
+        if (!$g->count()) {
+            // add the current user to the admin group..
+            $g = DB_DataObject::Factory('Groups');
+            if ($g->get('name', 'Administrators')) {
+                $gm = DB_DataObject::Factory('Group_Members');
+                $gm->group_id = $g->id;
+                $gm->user_id = $this->id;
+                $gm->insert();
+            }
+            
+        }
+        
+        // ------ STANDARD PERMISSION HANDLING.
+        
         $g = DB_DataObject::Factory('Group_Members');
         $grps = $g->listGroupMembership($this);
+       // print_r($grps);
         $isAdmin = $g->inAdmin;
-       // var_dump($grps);
+        //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
         // the load all the perms for those groups, and add them all together..
         // then load all those 
         $g = DB_DataObject::Factory('Group_Rights');
         $ret =  $g->listPermsFromGroupIds($grps, $isAdmin);
-       // echo '<PRE>';print_r($ret);
+        //echo '<PRE>';print_r($ret);
         return $ret;
          
         
     }
+    /**
+     *Basic group fetching - probably needs to filter by type eventually.
+     *
+     */
+    
+    function groups()
+    {
+        $g = DB_DataObject::Factory('Group_Members');
+        $grps = $g->listGroupMembership($this);
+        $g = DB_DataObject::Factory('Groups');
+        $g->whereAddIn('id', $grps, 'int');
+        return $g->fetchAll();
+        
+    }
+    
     function hasPerm($name, $lvl) 
     {
         static $pcache = array();
@@ -397,18 +419,30 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         }
         if (!empty($q['query']['person_internal_only_all'])) {
             // must be internal and not current user (need for distribution list)
-            $this->whereAdd(" join_company_id_id.isOwner = 1");
+            $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
             
         }
         // -- for distribution
         if (!empty($q['query']['person_internal_only'])) {
             // must be internal and not current user (need for distribution list)
-            $this->whereAdd(" join_company_id_id.isOwner = 1");
+            $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
             
             //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
             //    ".id  != ".$au->id);
             $this->whereAdd("Person.id != {$au->id}");
         } 
+        
+        if (!empty($q['query']['comptype_or_company_id'])) {
+           // DB_DataObject::debugLevel(1);
+            $bits = explode(',', $q['query']['comptype_or_company_id']);
+            $id = (int) array_pop($bits);
+            $ct = $this->escape($bits[0]);
+            
+            $this->whereAdd(" join_company_id_id.comptype = '$ct' OR Person.company_id = $id");
+            
+        }
+        
+        
         // staff list..
         if (!empty($q['query']['person_inactive'])) {
            // DB_Dataobject::debugLevel(1);
@@ -460,6 +494,25 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             }
         }
         
+        
+        if (!empty($q['query']['project_member_of'])) {
+               // this is also a flag to return if they are a member..
+            //DB_DataObject::debugLevel(1);
+            $do = DB_DataObject::factory('ProjectDirectory');
+            $do->project_id = $q['query']['project_member_of'];
+            
+            $this->joinAdd($do,array('joinType' => 'LEFT', 'useWhereAsOn' => true));
+            $this->selectAdd('IF(ProjectDirectory.id IS NULL, 0,  ProjectDirectory.id )  as is_member');
+                
+                
+            if (!empty($q['query']['project_member_filter'])) {
+                $this->having('is_member !=0');
+            
+            }
+            
+        }
+        
+        
         if (!empty($q['query']['search'])) {
             $s = $this->escape($q['query']['search']);
                     $this->whereAdd("
@@ -487,6 +540,9 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             ) {
             return true;
         }
+        if (empty($this->email)) {
+            return true;
+        }
         $xx = DB_Dataobject::factory('Person');
         $xx->setFrom(array(
             'email' => $this->email,
@@ -497,7 +553,33 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             return "Duplicate Email found";
         }
         return true;
-    }    
+    }
+    /**
+     *
+     * before Delete - delete significant dependancies..
+     * this is called after checkPerm..
+     */
+    
+    function beforeDelete()
+    {
+        
+        $e = DB_DataObject::Factory('Events');
+        $e->whereAdd('person_id = ' . $this->id);
+        $e->delete(true);
+        
+        // anything else?  
+        
+    }
+    
+    
+    /***
+     * Check if the a user has access to modify this item.
+     * @param String $lvl Level (eg. Core.Projects)
+     * @param Pman_Core_DataObjects_Person $au The authenticated user.
+     * @param boolean $changes alllow changes???
+     *
+     * @return false if no access..
+     */
     function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
     {