public function executeOauthcallback(sfWebRequest $request) { $code = $request->getParameter('code'); $state = $request->getParameter('state'); /* indicador_id */ $handle_error = $request->getParameter('error'); $indicator = IndicatorsScPeer::retrieveByPK($state); $id_tree = $indicator->getTreeId(); $current_user = $this->getUser()->getAttribute(sfConfig::get('app_session_current_user')); $c = new Criteria(); $c->add(UserGoogleConfigurationPeer::USER_ID, $current_user->getId()); $google_configuration = UserGoogleConfigurationPeer::doSelectOne($c); if (!is_object($google_configuration)) { $this->getUser()->setFlash('msg', 'Primero debes configurar los datos de tu cuenta Google'); $this->redirect('@configuration'); } $google_client_id = $google_configuration->getGoogleClientId(); $google_client_secret = $google_configuration->getGoogleClientSecret(); if ($handle_error == 'access_denied') { //$this->getUser()->setFlash('message','Oops, ha ocurrido un error. Intenta nuevamente.'); return $this->redirect('@edit_strategy?id_tree=' . $id_tree . '&node_id=' . $indicator->getId() . '&response=error&from=g'); } else { if ($code != '' && $state != '') { $postdata = http_build_query(array('code' => $code, 'client_id' => $google_client_id, 'client_secret' => $google_client_secret, 'redirect_uri' => $this->getController()->genUrl('@oauthcallback', true), 'grant_type' => 'authorization_code')); $opts = array('http' => array('method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'content' => $postdata)); $context = stream_context_create($opts); $result = @file_get_contents('https://accounts.google.com/o/oauth2/token', false, $context); if ($result === FALSE) { $this->getUser()->setFlash('msg', 'Configurar correctamente'); $this->redirect('@configuration'); } else { $json = json_decode($result); //consultamos el email del usuario, para adjuntarlo al registro $userinfo = @file_get_contents('https://www.googleapis.com/oauth2/v1/userinfo?access_token=' . $json->{'access_token'}); $json_userinfo = json_decode($userinfo); if ($userinfo === FALSE) { $this->redirect('@edit_strategy?id_tree=' . $id_tree . '&node_id=' . $indicator->getId() . '&response=error&from=g'); } else { /* si $json->{'refresh_token'} devuelve nada, es porque anteriormente ya permitió el acceso */ if (isset($json->{'refresh_token'})) { //primera vez que pide permiso de acceso //si existe el refresh_token, se reemplaza, ya que es porque anteriormente ha revocado el permiso de acceso $c = new Criteria(); $c->add(IndicadoresScGoogleAnalyticsPeer::GOOGLE_USER_EMAIL, $json_userinfo->{'email'}); $c->add(IndicadoresScGoogleAnalyticsPeer::INDICADOR_ID, $indicator->getId()); $indicadores_sc_google_analytics_object = IndicadoresScGoogleAnalyticsPeer::doSelectOne($c); if (is_object($indicadores_sc_google_analytics_object)) { //obtener nodos "del arbol $id_tree" del mismo google_user_email $c = new Criteria(); //$c->add(IndicatorsScPeer::TREE_ID, $id_tree); $c->add(IndicatorsScPeer::FLAG, 'habilitado'); $c->add(IndicatorsScPeer::ULTIMO_NODO, 1); $c->add(IndicatorsScPeer::DET_NETWORK_ATTRIBUTE_ID, null, Criteria::NOT_EQUAL); $c->add(IndicadoresScGoogleAnalyticsPeer::GOOGLE_USER_EMAIL, $json_userinfo->{'email'}); $c->addJoin(IndicadoresScGoogleAnalyticsPeer::INDICADOR_ID, IndicatorsScPeer::ID); $lista_nodos = IndicatorsScPeer::doSelect($c); //obtener nodos con conector exterior pertenecientes al usuario $json_userinfo->{'email'} foreach ($lista_nodos as $row) { //actualizar su refresh_token $c = new Criteria(); $c->add(IndicadoresScGoogleAnalyticsPeer::INDICADOR_ID, $row->getId()); $temp = IndicadoresScGoogleAnalyticsPeer::doSelectOne($c); $temp->setRefreshToken($json->{'refresh_token'}); $temp->save(); } } else { //sino, se crea $indicadores_sc_google_analytics = new IndicadoresScGoogleAnalytics(); $indicadores_sc_google_analytics->setRefreshToken($json->{'refresh_token'}); $indicadores_sc_google_analytics->setGoogleUserEmail($json_userinfo->{'email'}); $indicadores_sc_google_analytics->setIndicadorId($indicator->getId()); $indicadores_sc_google_analytics->save(); } $this->redirect('@edit_strategy?id_tree=' . $id_tree . '&node_id=' . $indicator->getId() . '&response=success&from=g'); } else { //buscar los nodos del mismo arbol $c = new Criteria(); $c->add(IndicadoresScGoogleAnalyticsPeer::GOOGLE_USER_EMAIL, $json_userinfo->{'email'}); //$c->add(IndicatorsScPeer::TREE_ID, $indicator->getTreeId());//puede ser del mismo arbol u otro $c->add(IndicatorsScPeer::FLAG, 'habilitado'); $c->addJoin(IndicadoresScGoogleAnalyticsPeer::INDICADOR_ID, IndicatorsScPeer::ID); $object = IndicadoresScGoogleAnalyticsPeer::doSelectOne($c); if (is_object($object)) { $indicadores_sc_google_analytics = new IndicadoresScGoogleAnalytics(); $indicadores_sc_google_analytics->setRefreshToken($object->getRefreshToken()); $indicadores_sc_google_analytics->setGoogleUserEmail($json_userinfo->{'email'}); $indicadores_sc_google_analytics->setIndicadorId($indicator->getId()); $indicadores_sc_google_analytics->save(); } $this->redirect('@edit_strategy?id_tree=' . $id_tree . '&node_id=' . $indicator->getId() . '&response=success&from=g'); } } } } } }
private function useRefreshToken($hdIndicadorId) { $c = new Criteria(); $c->add(IndicadoresScGoogleAnalyticsPeer::INDICADOR_ID, $hdIndicadorId); $google_analytics = IndicadoresScGoogleAnalyticsPeer::doSelectOne($c); if (is_object($google_analytics)) { $postdata = http_build_query(array('client_id' => sfConfig::get('app_google_client_id'), 'client_secret' => sfConfig::get('app_google_client_secret'), 'refresh_token' => $google_analytics->getRefreshToken(), 'grant_type' => 'refresh_token')); $opts = array('http' => array('method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'content' => $postdata)); $context = stream_context_create($opts); $result = file_get_contents('https://accounts.google.com/o/oauth2/token', false, $context); $json = json_decode($result); return $json->{'access_token'}; } else { return null; } }