File indexing completed on 2024-12-22 05:36:23

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_SsoToken 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         $this->_resultRow = array_shift($resultSet);
0099 
0100         return $this->createAuthResult(Zend_Auth_Result::SUCCESS, $this->_identity, array('Authentication successful.'));
0101     }
0102 
0103     /**
0104      * @return array
0105      * @throws Zend_Exception
0106      */
0107     private function fetchUserData()
0108     {
0109         $sql = "
0110             SELECT `member`.*
0111             FROM `member`
0112             WHERE `member`.`is_active` = :active
0113             AND `member`.`is_deleted` = :deleted
0114             AND `member`.`login_method` = :login
0115             AND `member`.`member_id` = :memberId
0116             ";
0117 
0118         $this->_db->getProfiler()->setEnabled(true);
0119         $resultSet = $this->_db->fetchAll($sql, array(
0120             'active'   => Default_Model_DbTable_Member::MEMBER_ACTIVE,
0121             'deleted'  => Default_Model_DbTable_Member::MEMBER_NOT_DELETED,
0122             'login'    => Default_Model_DbTable_Member::MEMBER_LOGIN_LOCAL,
0123             'memberId' => $this->_identity
0124         ));
0125         Zend_Registry::get('logger')->debug(__METHOD__ . ' - sql take seconds: ' . $this->_db->getProfiler()
0126                                                                                              ->getLastQueryProfile()
0127                                                                                              ->getElapsedSecs())
0128         ;
0129         $this->_db->getProfiler()->setEnabled(false);
0130 
0131         return $resultSet;
0132     }
0133 
0134     /**
0135      * @param $code
0136      * @param $identity
0137      * @param $messages
0138      *
0139      * @return Zend_Auth_Result
0140      */
0141     protected function createAuthResult($code, $identity, $messages)
0142     {
0143         return new Zend_Auth_Result($code, $identity, $messages);
0144     }
0145 
0146     /**
0147      * getResultRowObject() - Returns the result row as a stdClass object
0148      *
0149      * @param  string|array $returnColumns
0150      * @param  string|array $omitColumns
0151      *
0152      * @return stdClass|boolean
0153      */
0154     public function getResultRowObject($returnColumns = null, $omitColumns = null)
0155     {
0156         if (!$this->_resultRow) {
0157             return false;
0158         }
0159 
0160         $returnObject = new stdClass();
0161 
0162         if (null !== $returnColumns) {
0163 
0164             $availableColumns = array_keys($this->_resultRow);
0165             foreach ((array)$returnColumns as $returnColumn) {
0166                 if (in_array($returnColumn, $availableColumns)) {
0167                     $returnObject->{$returnColumn} = $this->_resultRow[$returnColumn];
0168                 }
0169             }
0170 
0171             return $returnObject;
0172         } else if (null !== $omitColumns) {
0173 
0174             $omitColumns = (array)$omitColumns;
0175             foreach ($this->_resultRow as $resultColumn => $resultValue) {
0176                 if (!in_array($resultColumn, $omitColumns)) {
0177                     $returnObject->{$resultColumn} = $resultValue;
0178                 }
0179             }
0180 
0181             return $returnObject;
0182         } else {
0183 
0184             foreach ($this->_resultRow as $resultColumn => $resultValue) {
0185                 $returnObject->{$resultColumn} = $resultValue;
0186             }
0187 
0188             return $returnObject;
0189         }
0190     }
0191 
0192 }