File indexing completed on 2024-05-12 06:02:09

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 class Local_Auth_AdapterFactory
0024 {
0025 
0026     const LOGIN_INFINITY = 'infinity';
0027     const LOGIN_HIVE = 'encryptionHive01';
0028     const LOGIN_PLING = 'encryptionPling01';
0029     const LOGIN_DEFAULT = 'default';
0030     const LOGIN_SSO = 'singleSingOn';
0031 
0032     /**
0033      * @param null $userIdentity
0034      * @param null $loginMethod
0035      *
0036      * @return Local_Auth_Adapter_Interface
0037      * @throws Zend_Auth_Adapter_Exception
0038      * @throws Zend_Exception
0039      */
0040     public static function getAuthAdapter($userIdentity = null, $credential = null, $loginMethod = null)
0041     {
0042         if (empty($loginMethod)) {
0043             $loginMethod = self::detectHashMethod($userIdentity, $credential);
0044         }
0045 
0046         return self::createAuthAdapter($loginMethod);
0047     }
0048 
0049     /**
0050      * @param $identity
0051      *
0052      * @return string
0053      */
0054     protected static function detectHashMethod($identity, $credential)
0055     {
0056         //$modelMember = new Default_Model_Member();
0057         //$memberData = $modelMember->findActiveMemberByIdentity($identity, $credential);
0058         $validator = new Zend_Validate_EmailAddress();
0059         if ($validator->isValid($identity)) {
0060             $sql = "SELECT * FROM member AS m WHERE mail = :identity AND (`password` = :passHive OR `password` = :passOcs)";
0061         } else {
0062             $sql = "SELECT * FROM member AS m WHERE username = :identity AND (`password` = :passHive OR `password` = :passOcs)";
0063         }
0064 
0065         $memberData = Zend_Db_Table::getDefaultAdapter()->fetchRow($sql, array('identity' => $identity,
0066                                                                                'passHive' => Local_Auth_Adapter_Ocs::getEncryptedPassword($credential, Default_Model_DbTable_Member::PASSWORD_TYPE_HIVE),
0067                                                                                'passOcs' => Local_Auth_Adapter_Ocs::getEncryptedPassword($credential, Default_Model_DbTable_Member::PASSWORD_TYPE_OCS)
0068             )
0069         );
0070 
0071         if (count($memberData) == 0) {
0072             return self::LOGIN_DEFAULT;
0073         }
0074 
0075         if (Default_Model_Member::PASSWORD_TYPE_HIVE == $memberData['password_type']) {
0076             return self::LOGIN_HIVE;
0077         }
0078         //if ($modelMember->isHiveUser($memberData)) {
0079         //    return self::LOGIN_HIVE;
0080         //}
0081 
0082         return self::LOGIN_DEFAULT;
0083     }
0084 
0085     /**
0086      * @param $provider
0087      *
0088      * @return Local_Auth_Adapter_Ocs|Local_Auth_Adapter_RememberMe|Local_Auth_Adapter_SsoToken
0089      * @throws Zend_Auth_Adapter_Exception
0090      * @throws Zend_Exception
0091      */
0092     protected static function createAuthAdapter($provider)
0093     {
0094         switch ($provider) {
0095             case self::LOGIN_INFINITY:
0096                 $authAdapter = new Local_Auth_Adapter_RememberMe(Zend_Registry::get('db'));
0097                 break;
0098 
0099             case self::LOGIN_SSO:
0100                 $authAdapter = new Local_Auth_Adapter_SsoToken(Zend_Registry::get('db'));
0101                 break;
0102 
0103             case self::LOGIN_HIVE:
0104                 $authAdapter = new Local_Auth_Adapter_Ocs(Zend_Registry::get('db'), 'member');
0105                 $authAdapter->setEncryption(Local_Auth_Adapter_Ocs::SHA);
0106                 break;
0107 
0108             case self::LOGIN_PLING:
0109             case self::LOGIN_DEFAULT:
0110             default:
0111                 $authAdapter = new Local_Auth_Adapter_Ocs(Zend_Registry::get('db'), 'member');
0112                 $authAdapter->setEncryption(Local_Auth_Adapter_Ocs::MD5);
0113                 break;
0114         }
0115 
0116         return $authAdapter;
0117     }
0118 
0119 }