Pman/Login.php
[Pman.Base] / Pman / Login.php
index 7a33364..1839cdd 100644 (file)
@@ -2,6 +2,25 @@
 
 require_once 'Pman.php';
 
+/***
+* 
+* Auth wrapper..
+* 
+* User class must provide the following features.
+* 
+* logout()
+* isAuth() 
+* getAuthUser();
+* authUserArray() 
+* active()  -- is user active. // or set prior to checking..
+* authUserName(n) - sets the value prior to a find(true)
+* checkPassword($_REQUEST['password'])) {
+* login();
+* lang(val) - to set the language..
+*/
+
+
+
 class Pman_Login extends Pman
 { 
     
@@ -29,7 +48,11 @@ class Pman_Login extends Pman
             $u = $this->getAuthUser();
             //print_r($u);
             if ($u) {
+                
                 $this->addEvent('LOGOUT');
+                $e = DB_DataObject::factory('Events');
+              
+                session_regenerate_id(true);
                 $u->logout();
             }
             // log it..
@@ -40,6 +63,7 @@ class Pman_Login extends Pman
         
         // general query...
         if (!empty($_REQUEST['getAuthUser'])) {
+            //DB_Dataobject::debugLevel(5);
             $this->sendAuthUserDetails();
             exit;
            
@@ -47,13 +71,18 @@ class Pman_Login extends Pman
         if (!empty($_REQUEST['username'])) {
             $this->post();
         }
+        if (!empty($_REQUEST['switch'])) {
+            $this->switchUser($_REQUEST['switch']);
+        }
+        
+        
         $this->jerr("INVALID REQUEST");
         exit;
     }
     
     function sendAuthUserDetails()
     {
-        
+       // DB_DataObject::debugLevel(1);
         $ff = HTML_FlexyFramework::get();
         $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
         
@@ -65,7 +94,7 @@ class Pman_Login extends Pman
         $au = $u->getAuthUser();
         
         $aur = $au->authUserArray();
-        
+         
         /** -- these need modulizing somehow! **/
         
         if ($this->hasModule('Fax')) {
@@ -94,10 +123,31 @@ class Pman_Login extends Pman
     }
 
     
+    function switchUser($id)
+    {
+        // first check they have perms to do this..
+        if (!$this->authUser || ($this->authUser->company_id_comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
+            $this->jerr("User switching not permitted");
+        }
+        $old = clone($this->authUser);
+        $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
+        $u = DB_DataObject::factory($tbl);
+        $u->get($id);
+        if (!$u->active()) {
+            $this->jerr('Account disabled');
+        }
+        $u->login();
+            // we might need this later..
+        $this->addEvent("SWITCH USER", false, session_id());
+        
+        
+    }
+    
+    
     var $domObj = false;
     function post()
     {
-        
+        //DB_DataObject::debugLevel(1);
         if (!empty($_REQUEST['getAuthUser'])) {
             $this->sendAuthUserDetails();
             exit;
@@ -114,37 +164,39 @@ class Pman_Login extends Pman
             return $this->changePassword($_REQUEST);
         }
         
-         $ff = HTML_FlexyFramework::get();
+        // login attempt..
+        
+        $ff = HTML_FlexyFramework::get();
         $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
         
        
         $u = DB_DataObject::factory($tbl);
         //$u->active = 1;
-        $u->whereAdd('LENGTH(passwd) > 1');
-        //$u->company_id = $this->company->id;
+        
         
         if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
             $this->jerr('You typed the wrong Username or Password (0)');
             exit;
         }
-         
-        $u->email = $_REQUEST['username'];
+        
+        $u->authUserName($_REQUEST['username']);
+        
+        
         if ($u->count() > 1 || !$u->find(true)) {
             $this->jerr('You typed the wrong Username or Password  (1)');
             exit;
         }
         
-        if (!$u->active) {
+        if (!$u->active()) {
             $this->jerr('Account disabled');
         }
         
         if ($u->checkPassword($_REQUEST['password'])) {
             $u->login();
-            $this->AddEvent("LOGIN");
-            if (!empty($_REQUEST['lang']) && $_REQUEST['lang'] != $u->lang) {
-                $uu = clone($u);
-                $uu->lang = $_REQUEST['lang'];
-                $uu->update();
+            // we might need this later..
+            $this->addEvent("LOGIN", false, session_id());
+            if (!empty($_REQUEST['lang'])) {
+                $u->lang($_REQUEST['lang']);
             }
              // log it..