public function authenticate() { $yahooOpenId = new BKH_OpenID_Yahoo(); $myYahooInfo = json_decode($yahooOpenId->getUserProfile(), true); if ($myYahooInfo && isset($myYahooInfo['profile'])) { $myYahooProfile = $myYahooInfo['profile']; //var_dump($myYahooProfile); die; $identity = new stdClass(); $identity->user_type = "yahoo"; //Check user exists in database. $yahooTable = BKH_Db_TableAdapter_Locator::get("YahooUser"); $userTable = BKH_Db_TableAdapter_Locator::get("User"); $email = ""; if (isset($myYahooProfile->emails[1])) { $email = $myYahooProfile['emails'][1]['handle']; } else { $email = $myYahooProfile['emails'][0]['handle']; } $userInfoInDb = $yahooTable->findYahooUserByYahooId($myYahooProfile['guid']); if (!$userInfoInDb) { //If not exist in db //Insert to DB $insertUserData = array(); $insertUserData['display_name'] = $myYahooProfile['nickname']; $insertUserData['type'] = "yahoo"; $newUserId = $userTable->insertUser($insertUserData); if ($newUserId) { //Insert user to Yahoo Table $insertUserFBData = array(); $insertUserFBData['id'] = $newUserId; $insertUserFBData['yahoo_id'] = $myYahooProfile['guid']; //$insertUserFBData['display_name'] = $myYahooProfile['nickname']; $insertUserFBData['email'] = $email; $newYahooUserId = $yahooTable->insertUser($insertUserFBData); $biUserTable = new BKH_Db_TableAdapter_BI_BiUser(); $biUserTable->insertBiUser($newUserId); if (!$newYahooUserId) { $log = BKH_Log::getLog(); $log->logMessage("Could not insert yahoo user to USER table.", Zend_Log::ERR); } $identity->user_name = $myYahooProfile['nickname']; $identity->email = $email; $identity->user_id = $newUserId; } else { $log = BKH_Log::getLog(); $log->logMessage("Could not insert yahoo user to USER table.", Zend_Log::ERR); throw new BKH_Exception("Could not insert Yahoo user to database"); } } else { //TODO: Update information to DB again $identity->user_name = $myYahooProfile['nickname']; $identity->email = $email; $identity->user_id = $myYahooProfile['guid']; } return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity, array()); } else { throw new BKH_Exception("Could not retrive your information, please check again"); } return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, $this->consumerKey); }
public function authenticate() { //$myGoogleInfo = $this->getUserInformation(); $googleOpenId = new BKH_OpenID_Google(); $myGoogleInfo = $googleOpenId->getUserProfile(); if ($myGoogleInfo) { $xml = simplexml_load_string($myGoogleInfo, 'SimpleXMLElement', LIBXML_NOCDATA); unset($myGoogleInfo); //$xml = new SimpleXMLElement($content); if ($xml && isset($xml->id) && isset($xml->author->name) && isset($xml->author->email)) { $myGoogleInfo['name'] = (string) $xml->author->name; $myGoogleInfo['email'] = (string) $xml->author->email; $myGoogleInfo['id'] = (string) $xml->id; $identity = new stdClass(); $identity->user_type = "google"; //Check user exists in database. $googleTable = BKH_Db_TableAdapter_Locator::get("GoogleUser"); $userTable = BKH_Db_TableAdapter_Locator::get("User"); $userInfoInDb = $googleTable->findGoogleUserByGoogleId($myGoogleInfo['id']); if (!$userInfoInDb) { //If not exist in db //Insert to DB $insertUserData = array(); $insertUserData['display_name'] = $myGoogleInfo['name']; $insertUserData['type'] = "google"; $newUserId = $userTable->insertUser($insertUserData); if ($newUserId) { //Insert user to Google Table $insertUserFBData = array(); $insertUserFBData['id'] = $newUserId; $insertUserFBData['google_id'] = $myGoogleInfo['id']; //$insertUserFBData['display_name'] = $myGoogleInfo['name']; $insertUserFBData['email'] = $myGoogleInfo['email']; $newGoogleUserId = $googleTable->insertUser($insertUserFBData); $biUserTable = new BKH_Db_TableAdapter_BI_BiUser(); $biUserTable->insertBiUser($newUserId); if (!$newGoogleUserId) { $log = BKH_Log::getLog(); $log->logMessage("Could not insert google user to USER table.", Zend_Log::ERR); } $identity->user_name = $myGoogleInfo['name']; $identity->email = $myGoogleInfo['email']; $identity->user_id = $newUserId; } else { $log = BKH_Log::getLog(); $log->logMessage("Could not insert google user to USER table.", Zend_Log::ERR); throw new BKH_Exception("Could not insert Google user to database"); } } else { //TODO: Update information to DB again $identity->user_name = $myGoogleInfo['name']; $identity->email = $myGoogleInfo['email']; $identity->user_id = $userInfoInDb['id']; } return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity, array()); } } return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, $this->consumerKey); }