File indexing completed on 2025-01-19 05:20:54

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  *
0023  *    Created: 22.10.2016
0024  **/
0025 class Local_Auth_Adapter_RememberMe implements Local_Auth_Adapter_Interface
0026 {
0027 
0028     protected $_identity;
0029     protected $_credential;
0030     protected $_db;
0031     protected $_resultRow;
0032 
0033     /**
0034      * __construct() - Sets configuration options
0035      *
0036      * @param  Zend_Db_Adapter_Abstract $dbAdapter If null, default database adapter assumed
0037      * @param string                    $tableName
0038      *
0039      * @throws Zend_Auth_Adapter_Exception
0040      */
0041     public function __construct(Zend_Db_Adapter_Abstract $dbAdapter = null, $tableName = null)
0042     {
0043         $this->_db = $dbAdapter;
0044         if (empty($this->_db)) {
0045             $this->_db = Zend_Db_Table_Abstract::getDefaultAdapter();
0046             if (empty($this->_db)) {
0047                 throw new Zend_Auth_Adapter_Exception('No database adapter present');
0048             }
0049         }
0050     }
0051 
0052     /**
0053      * @param string $identity
0054      *
0055      * @return Zend_Auth_Adapter_Interface
0056      * @throws Zend_Exception
0057      */
0058     public function setIdentity($identity)
0059     {
0060         $this->_identity = $identity;
0061 
0062         return $this;
0063     }
0064 
0065     /**
0066      * @param string $credential
0067      *
0068      * @return Zend_Auth_Adapter_Interface
0069      * @throws Zend_Exception
0070      */
0071     public function setCredential($credential)
0072     {
0073         $this->_credential = $credential;
0074 
0075         return $this;
0076     }
0077 
0078     /**
0079      * Performs an authentication attempt
0080      *
0081      * @return Zend_Auth_Result
0082      * @throws Zend_Exception
0083      */
0084     public function authenticate()
0085     {
0086         $resultSet = $this->fetchUserData();
0087 
0088         if (count($resultSet) == 0) {
0089             return $this->createAuthResult(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, $this->_identity,
0090                 array('A record with the supplied identity could not be found.'));
0091         }
0092 
0093         if (count($resultSet) > 1) {
0094             return $this->createAuthResult(Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS, $this->_identity,
0095                 array('More than one record matches the supplied identity.'));
0096         }
0097 
0098         if (empty($resultSet[0]['email_checked'])) {
0099             return $this->createAuthResult(Local_Auth_Result::MAIL_ADDRESS_NOT_VALIDATED, $resultSet[0]['member_id'],
0100                 array('Mail address not validated.'));
0101         }
0102 
0103         if ($resultSet[0]['is_active'] == 0) {
0104             return $this->createAuthResult(Local_Auth_Result::ACCOUNT_INACTIVE, $this->_identity, array('User account is inactive.'));
0105         }
0106 
0107         $this->_resultRow = array_shift($resultSet);
0108 
0109         return $this->createAuthResult(Zend_Auth_Result::SUCCESS, $this->_identity, array('Authentication successful.'));
0110     }
0111 
0112     /**
0113      * @return array
0114      * @throws Zend_Exception
0115      */
0116     private function fetchUserData()
0117     {
0118         $sql = "
0119             SELECT `m`.*, `me`.`email_verification_value`, `me`.`email_checked`, `mei`.`external_id` 
0120             FROM `session`
0121             JOIN `member` AS `m` ON `m`.`member_id` = `session`.`member_id`
0122             JOIN member_email AS `me` ON m.member_id = me.email_member_id AND me.email_primary = 1
0123             LEFT JOIN `member_external_id` AS `mei` ON `mei`.`member_id` = `m`.`member_id`
0124             WHERE `m`.`is_active` = :active
0125             AND `m`.`is_deleted` = :deleted
0126             AND `m`.`login_method` = :login
0127             AND `session`.`member_id` = :member
0128             AND `session`.`remember_me_id` = :uuid
0129             AND `session`.`expiry` >= NOW()
0130             ";
0131 
0132         $this->_db->getProfiler()->setEnabled(true);
0133         $resultSet = $this->_db->fetchAll($sql, array(
0134             'active'  => Application_Model_DbTable_Member::MEMBER_ACTIVE,
0135             'deleted' => Application_Model_DbTable_Member::MEMBER_NOT_DELETED,
0136             'login'   => Application_Model_DbTable_Member::MEMBER_LOGIN_LOCAL,
0137             'member'  => $this->_identity,
0138             'uuid'    => $this->_credential
0139         ));
0140         Zend_Registry::get('logger')->debug(__METHOD__ . ' - sql take seconds: ' . $this->_db->getProfiler()
0141                                                                                              ->getLastQueryProfile()
0142                                                                                              ->getElapsedSecs())
0143         ;
0144         $this->_db->getProfiler()->setEnabled(false);
0145 
0146         return $resultSet;
0147     }
0148 
0149     /**
0150      * @param $code
0151      * @param $identity
0152      * @param $messages
0153      *
0154      * @return Zend_Auth_Result
0155      */
0156     protected function createAuthResult($code, $identity, $messages)
0157     {
0158         return new Zend_Auth_Result($code, $identity, $messages);
0159     }
0160 
0161     /**
0162      * getResultRowObject() - Returns the result row as a stdClass object
0163      *
0164      * @param  string|array $returnColumns
0165      * @param  string|array $omitColumns
0166      *
0167      * @return stdClass|boolean
0168      */
0169     public function getResultRowObject($returnColumns = null, $omitColumns = null)
0170     {
0171         if (!$this->_resultRow) {
0172             return false;
0173         }
0174 
0175         $returnObject = new stdClass();
0176 
0177         if (null !== $returnColumns) {
0178 
0179             $availableColumns = array_keys($this->_resultRow);
0180             foreach ((array)$returnColumns as $returnColumn) {
0181                 if (in_array($returnColumn, $availableColumns)) {
0182                     $returnObject->{$returnColumn} = $this->_resultRow[$returnColumn];
0183                 }
0184             }
0185 
0186             return $returnObject;
0187         } else if (null !== $omitColumns) {
0188 
0189             $omitColumns = (array)$omitColumns;
0190             foreach ($this->_resultRow as $resultColumn => $resultValue) {
0191                 if (!in_array($resultColumn, $omitColumns)) {
0192                     $returnObject->{$resultColumn} = $resultValue;
0193                 }
0194             }
0195 
0196             return $returnObject;
0197         } else {
0198 
0199             foreach ($this->_resultRow as $resultColumn => $resultValue) {
0200                 $returnObject->{$resultColumn} = $resultValue;
0201             }
0202 
0203             return $returnObject;
0204         }
0205     }
0206 
0207 }