]> git.agnieray.net Git - galette.git/commitdiff
Remove globals on authentication; refs #414
authorJohan Cwiklinski <johan@x-tnd.be>
Sun, 25 Oct 2015 18:53:24 +0000 (19:53 +0100)
committerJohan Cwiklinski <johan@x-tnd.be>
Sun, 25 Oct 2015 18:53:24 +0000 (19:53 +0100)
galette/includes/galette.inc.php
galette/index.php
galette/lib/Galette/Core/Authentication.php
galette/lib/Galette/Core/Login.php

index 15279a8acf853cc5554dcb2ccc7aadda54fbb47d..284801e34e1468fee5fc625663706c7456eacbb3 100644 (file)
@@ -312,8 +312,9 @@ if ( !$installer and !defined('GALETTE_TESTS') ) {
             $login = unserialize(
                 $session['login']
             );
+            $login->setDb($zdb);
         } else {
-            $login = new Core\Login();
+            $login = new Core\Login($zdb, $i18n, $session);
         }
 
         if (GALETTE_MODE === 'MAINT' && !$login->isSuperAdmin() ) {
index 930740e569aaa47f36e27595ccada83ac4084c96..18be52b74db1edead66abee475e901de4d011212 100644 (file)
@@ -65,7 +65,7 @@ if (isset($_POST['ident'])) {
     if ( $_POST['login'] == $preferences->pref_admin_login
         && $pw_superadmin
     ) {
-        $login->logAdmin($_POST['login']);
+        $login->logAdmin($_POST['login'], $preferences);
         $session['login'] = serialize($login);
         $hist->add(_T("Login"));
         if ( !isset($_COOKIE['show_galette_dashboard'])
index 1efcdb260b0fb103ad8fab8bfc7e2aca7174fe23..1d0cc2d21558a97ecfc8cc3d9aaf681377685ac8 100644 (file)
@@ -50,7 +50,7 @@ namespace Galette\Core;
  * @since     Available since 0.7dev - 2009-02-28
  */
 
-abstract class Authentication
+abstract class Authentication implements \Serializable
 {
     private $_login;
     private $_passe;
@@ -67,13 +67,6 @@ abstract class Authentication
     private $_managed_groups;
     private $_cron = false;
 
-    /**
-    * Default constructor
-    */
-    public function __construct()
-    {
-    }
-
     /**
     * Logs in user.
     *
@@ -101,10 +94,8 @@ abstract class Authentication
     *
     * @return void
     */
-    public function logAdmin($login)
+    public function logAdmin($login, Preferences $preferences)
     {
-        global $preferences;
-
         $this->_logged = true;
         $this->_name = 'Admin';
         $this->_login = $login;
@@ -303,4 +294,14 @@ abstract class Authentication
         $name = '_' . $name;
         $this->$name = $value;
     }
+
+    /**
+     * Apply get_object_vars to get private properties
+     *
+     * @return array
+     */
+    protected function getObjectVars()
+    {
+        return get_object_vars($this);
+    }
 }
index 62b1c6b3033bd083cfba5f43def4ded2765d7d31..ef886720a6caf5f8a38cf5c99bc30e7c0d2fcb8d 100644 (file)
@@ -61,6 +61,78 @@ class Login extends Authentication
     const TABLE = Adherent::TABLE;
     const PK = 'login_adh';
 
+    private $_zdb;
+    private $_i18n;
+    private $_session;
+
+    /**
+     * Instanciate object
+     *
+     * @param Db    $zdb     Database instance
+     * @param I18n  $i18n    I18n instance
+     * @param array $session Current session
+     */
+    public function __construct(Db $zdb, I18n $i18n, array &$session)
+    {
+        $this->setDb($zdb);
+        $this->_i18n = $i18n;
+        $this->_session = $session;
+    }
+
+    /**
+     * Set database instance
+     *
+     * @param Db $zdb Database instance
+     *
+     * @return void
+     */
+    public function setDb(Db $zdb)
+    {
+        $this->_zdb = $zdb;
+    }
+
+    /**
+     * Method to run on serialize()
+     *
+     * @return string
+     */
+    public function serialize()
+    {
+        $this->_zdb = null;
+
+        $vars = parent::getObjectVars();
+        $vars = array_merge(
+            $vars,
+            get_object_vars($this)
+        );
+
+        return base64_encode(
+            serialize($vars)
+        );
+    }
+
+    /**
+     * Method to run on unserialize()
+     *
+     * @param string $serialized Serialized data
+     *
+     * @return void
+     */
+    public function unserialize($serialized)
+    {
+        $serialized = unserialize(
+            base64_decode($serialized)
+        );
+
+        $locals = array_keys(get_object_vars($this));
+        foreach ($serialized as $key => $value) {
+            if (!in_array($key, $locals)) {
+                $key = substr($key, 1);
+            }
+            $this->$key = $value;
+        }
+    }
+
     /**
      * Logs in user.
      *
@@ -71,31 +143,12 @@ class Login extends Authentication
      */
     public function logIn($user, $passe)
     {
-        global $zdb, $i18n, $session;
-
         try {
-            $select = $zdb->select(self::TABLE, 'a');
-            $select->columns(
-                array(
-                    'id_adh',
-                    'bool_admin_adh',
-                    'nom_adh',
-                    'prenom_adh',
-                    'mdp_adh',
-                    'pref_lang',
-                    'activite_adh',
-                    'bool_exempt_adh',
-                    'date_echeance'
-                )
-            )->join(
-                array('b' => PREFIX_DB . Status::TABLE),
-                'a.' . Status::PK . '=b.' . Status::PK,
-                array('priorite_statut')
-            );
+            $select = $this->select();
             $select->where(array(self::PK => $user));
 
-            $results = $zdb->execute($select);
-            if ( $results->count() == 0 ) {
+            $results = $this->_zdb->execute($select);
+            if ($results->count() == 0) {
                 Analog::log(
                     'No entry found for login `' . $user . '`',
                     Analog::WARNING
@@ -105,12 +158,12 @@ class Login extends Authentication
                 $row = $results->current();
                 //check if pawwsord matches
                 $pw_checked = password_verify($passe, $row->mdp_adh);
-                if ( !$pw_checked ) {
+                if (!$pw_checked) {
                     //if password did not match, we try old md5 method
                     $pw_checked = (md5($passe) === $row->mdp_adh);
                 }
 
-                if ( $pw_checked === false ) {
+                if ($pw_checked === false) {
                     //Passwords mismatch. Log and return.
                     Analog::log(
                         'Passwords mismatch for login `' . $user . '`',
@@ -119,47 +172,7 @@ class Login extends Authentication
                     return false;
                 }
 
-                Analog::log('User `' . $user . '` logged in.', Analog::INFO);
-                $this->id = $row->id_adh;
-                $this->login = $user;
-                $this->passe = $row->mdp_adh;
-                $this->admin = $row->bool_admin_adh;
-                $this->name = $row->nom_adh;
-                $this->surname = $row->prenom_adh;
-                $this->lang = $row->pref_lang;
-                $i18n->changeLanguage($this->lang);
-                $session['lang'] = serialize($i18n);
-                $this->active = $row->activite_adh;
-                $this->logged = true;
-                if ( $row->priorite_statut < Members::NON_STAFF_MEMBERS ) {
-                    $this->staff = true;
-                }
-                //check if member is up to date
-                if ( $row->bool_exempt_adh == true ) {
-                    //member is due free, he's up to date.
-                    $this->uptodate = true;
-                } else {
-                    //let's check from end date, if present
-                    if ( $row->date_echeance == null ) {
-                        $this->uptodate = false;
-                    } else {
-                        $ech = new \DateTime($row->date_echeance);
-                        $now = new \DateTime();
-                        $now->setTime(0, 0, 0);
-                        $this->uptodate = $ech >= $now;
-                    }
-                }
-                //staff members and admins are de facto groups managers. For all
-                //others, get managed groups
-                if ( !$this->isSuperAdmin()
-                    && !$this->isAdmin()
-                    && !$this->isStaff()
-                ) {
-                    $this->managed_groups = Groups::loadManagedGroups(
-                        $this->id,
-                        false
-                    );
-                }
+                $this->logUser($row);
                 return true;
             }
         } catch (AdapterException $e) {
@@ -169,7 +182,7 @@ class Login extends Authentication
             );
             Analog::log($e->getTrace(), Analog::ERROR);
             return false;
-        } catch(\Exception $e) {
+        } catch (\Exception $e) {
             Analog::log(
                 'An error occured: ' . $e->getMessage(),
                 Analog::WARNING
@@ -179,6 +192,87 @@ class Login extends Authentication
         }
     }
 
+    /**
+     * Get select query without where clause
+     *
+     * @return \Zend\Db\Sql\Select
+     */
+    private function select()
+    {
+        $select = $this->_zdb->select(self::TABLE, 'a');
+        $select->columns(
+            array(
+                'id_adh',
+                'login_adh',
+                'bool_admin_adh',
+                'nom_adh',
+                'prenom_adh',
+                'mdp_adh',
+                'pref_lang',
+                'activite_adh',
+                'bool_exempt_adh',
+                'date_echeance'
+            )
+        )->join(
+            array('b' => PREFIX_DB . Status::TABLE),
+            'a.' . Status::PK . '=b.' . Status::PK,
+            array('priorite_statut')
+        );
+        return $select;
+    }
+
+    /**
+     * Populate object after successfull login
+     *
+     * @param \ArrayObject $row User informations
+     *
+     * @return void
+     */
+    private function logUser(\ArrayObject $row)
+    {
+        Analog::log('User `' . $row->login_adh . '` logged in.', Analog::INFO);
+        $this->id = $row->id_adh;
+        $this->login = $row->login_adh;
+        $this->passe = $row->mdp_adh;
+        $this->admin = $row->bool_admin_adh;
+        $this->name = $row->nom_adh;
+        $this->surname = $row->prenom_adh;
+        $this->lang = $row->pref_lang;
+        $this->_i18n->changeLanguage($this->lang);
+        $this->_session['lang'] = serialize($this->_i18n);
+        $this->active = $row->activite_adh;
+        $this->logged = true;
+        if ($row->priorite_statut < Members::NON_STAFF_MEMBERS) {
+            $this->staff = true;
+        }
+        //check if member is up to date
+        if ($row->bool_exempt_adh == true) {
+            //member is due free, he's up to date.
+            $this->uptodate = true;
+        } else {
+            //let's check from end date, if present
+            if ($row->date_echeance == null) {
+                $this->uptodate = false;
+            } else {
+                $ech = new \DateTime($row->date_echeance);
+                $now = new \DateTime();
+                $now->setTime(0, 0, 0);
+                $this->uptodate = $ech >= $now;
+            }
+        }
+        //staff members and admins are de facto groups managers. For all
+        //others, get managed groups
+        if (!$this->isSuperAdmin()
+            && !$this->isAdmin()
+            && !$this->isStaff()
+        ) {
+            $this->managed_groups = Groups::loadManagedGroups(
+                $this->id,
+                false
+            );
+        }
+    }
+
     /**
      * Does this login already exists ?
      * These function should be used for setting admin login into Preferences
@@ -189,14 +283,12 @@ class Login extends Authentication
      */
     public function loginExists($user)
     {
-        global $zdb;
-
         try {
-            $select = $zdb->select(self::TABLE);
+            $select = $this->_zdb->select(self::TABLE);
             $select->where(array(self::PK => $user));
-            $results = $zdb->execute($select);
+            $results = $this->_zdb->execute($select);
 
-            if ( $results->count() > 0 ) {
+            if ($results->count() > 0) {
                 /* We got results, user already exists */
                 return true;
             } else {