开发者

CakePHP - Saving one session variable deletes another

开发者 https://www.devze.com 2023-03-31 05:35 出处:网络
I\'m using session variables in CakePHP to store my relevant user Twitter and Facebook data, when the user logs in if he has linked his Twitter and FB accounts this information is saved in the session

I'm using session variables in CakePHP to store my relevant user Twitter and Facebook data, when the user logs in if he has linked his Twitter and FB accounts this information is saved in the session variable next to my own user data.

I have a screen where the user can link and unlink said social network data, the problem is the following:

Let's say I have both networks connected, I decide to disconnect from Facebook, the session variable for Facebook is deleted. Now I wish to reconnect to Facebook, I click on the Connect button, the Facebook data is saved but for some reason it deletes the Twitter variable.

The way my process works is the following:

1) User clicks on connect button. 2) User is directed to social network auth. 3) User is directed to a function that takes the required data, saves it in a session variable called NetworkData and is directed back to the page where he clicked the button. 4) The NetworkData is extracted, set as the according social network (Facebook or Twitter) in the session and is deleted from the session.

Code is as follows:

This is the function that the user is directed after login in to Twitter or FB:

function retrieve_network($page) {
      $networkData = null;
      $this->autoRender = false;
      $this->layout = 'ajax';
      if(isset($_GET['oauth_token'])) {
        $token = $this->开发者_如何学CTwitterHelper->setOAuthToken($_GET['oauth_token']);
        $userinfo = $this->TwitterHelper->getTwitterUserInfo();
        $networkData = array(
          'TwitterData' => array(
            'username' => $userinfo['username'],
            'name' => $userinfo['name'],
            'token' => $token->oauth_token,
            'token_secret' => $token->oauth_token_secret
          )
        );
      } else if (isset($_GET['code'])) {
        $token = $this->FacebookHelper->facebook->getAccessToken();
        $userinfo = $this->FacebookHelper->getUserInfo();
        $networkData = array(
          'FacebookData' => array(
            'username' => $userinfo['username'],
            'name' => $userinfo['name'],
            'email' => $userinfo['email'],
            'token' => $token,
            'link' => $userinfo['link'],
          )
        );
      }

      $this->Session->write('NetworkData', $networkData);

      if($page == 'settings') {
        $this->redirect(array('controller' => 'fonykers', 'action' => 'settings/networks'));
      }
    }

This is the function that retrieves what's in network data and sets it to the session:

function settings($tab) {
      $this->layout = 'frontend';  
      $this->Fonyker->recursive = -1;
      $this->TwitterData->recursive = -1;
      $this->FacebookData->recursive = -1;

      if(!$this->checkSessionCookie()) {
        $this->redirect(array('controller' => 'pages', 'action' => 'home'));
      }

      $fields = array(
        'Fonyker.id',
        'Fonyker.username',
        'Fonyker.name',
        'Fonyker.email',
        'Fonyker.gender',
        'Fonyker.birthdate',
        'Fonyker.image_url'
      );

      $fonyker = $this->Fonyker->find('first', array(
        'conditions' => array(
          'Fonyker.fonykid' => $this->Session->read('Fonyker.Fonyker.fonykid')
        ),
        'fields' => $fields
      ));


      $this->Fonyker->set($fonyker);

      $this->data = $fonyker;

      if($this->Session->read('NetworkData')) {
        $networkData = $this->Session->read('NetworkData');
        $this->Session->delete('NetworkData');  

        if($networkData['TwitterData']) {
          $networkData['TwitterData']['fonyker_id'] = $fonyker['Fonyker']['id']; 
          if($this->TwitterData->save($networkData)) {
            $this->Session->write('TwitterData', $networkData['TwitterData']);
          }
        } else if($networkData['FacebookData']) {
          $networkData['FacebookData']['fonyker_id'] = $fonyker['Fonyker']['id']; 
          if($this->FacebookData->save($networkData)) {
            $this->Session->write('FacebookData', $networkData['FacebookData']);
          }
        }
      }

      pr($this->Session->read());

      if(!$this->Session->read('TwitterData')) {
        $this->TwitterHelper->setTwitterObj();
        $this->set('twitterUrl', $this->TwitterHelper->twitterObj->getAuthorizeUrl(null, array('oauth_callback' => 'http://127.0.0.1/fonykweb/pages/retrieve_network/settings')));
      } else {
        $this->set('twitterUrl', '#');
      }


      if(!$this->Session->read('FacebookData')) {
        $this->set('facebookUrl', $this->FacebookHelper->facebook->getLoginUrl(array('redirect_uri' => 'http://localhost/fonykweb/pages/retrieve_network/settings','scope' => 'email,user_birthday,publish_stream,offline_access')));
      } else {
        $this->set('facebookUrl', '#');
      } 

      $this->set('tab', $tab);
    }

This is the function that removes the network if the user wishes:

function remove_network($network) {
      $this->autoRender = false;
      $this->Fonyker->recursive = -1;
      $this->TwitterData->recursive = -1;
      $this->FacebookData->recursive = -1;
      $response = null;

      if($network == 'twitter') {
        $twitterData = $this->TwitterData->find('first', array(
          'conditions' => array(
            'TwitterData.fonyker_id' => $this->Session->read('TwitterData.fonyker_id')
          )
        ));

        if($this->TwitterData->delete($twitterData['TwitterData']['id'], false)) {
          $this->TwitterHelper->setTwitterObj();
          $twitterUrl = $this->TwitterHelper->twitterObj->getAuthorizeUrl(null, array('oauth_callback' => 'http://127.0.0.1/fonykweb/pages/retrieve_network/settings'));
          $this->Session->delete('TwitterData');
          $response = json_encode(array('ok' => true, 'url' => $twitterUrl));
        } else {
          $response = json_encode(array('ok' => false)); 
        }
      }

      if($network == 'facebook') {
        $facebookData = $this->FacebookData->find('first', array(
          'conditions' => array(
            'FacebookData.fonyker_id' => $this->Session->read('FacebookData.fonyker_id')
          )
        ));

        if($this->FacebookData->delete($facebookData['FacebookData']['id'], false)) {
          $facebookUrl = $this->FacebookHelper->facebook->getLoginUrl(array('redirect_uri' => 'http://localhost/fonykweb/pages/retrieve_network/settings','scope' => 'email,user_birthday,publish_stream,offline_access'));
          $this->Session->delete('FacebookData');
          $response = json_encode(array('ok' => true, 'url' => $facebookUrl));
        } else {
          $response = json_encode(array('ok' => false)); 
        }

      }

  echo $response; 
}

View code:

<script type="text/javascript">
  $(document).ready(function() {
    var splitUrl = window.location.href.split('/');
    $('#' + splitUrl[splitUrl.length - 1] + '-tab').addClass('active-tab');
    $('#' + splitUrl[splitUrl.length - 1] + '-tab').children().addClass('active-tab');
  });
</script>
<div class="prepend-1 prepend-top span-23">
<div class="tabs span-22">
  <ul>
    <li id="account-tab">
      <a href="<?php echo $html->url(array('controller' => 'fonykers', 'action' => 'settings'), true); ?>/account">
        Account
      </a>
    </li>
    <li id="password-tab">
      <a href="<?php echo $html->url(array('controller' => 'fonykers', 'action' => 'settings'), true); ?>/password">
        Password 
      </a>
    </li>
    <li id="notifications-tab">
      <a href="<?php echo $html->url(array('controller' => 'fonykers', 'action' => 'settings'), true); ?>/notifications">
        Notifications 
      </a>
    </li>
    <li id="networks-tab">
      <a href="<?php echo $html->url(array('controller' => 'fonykers', 'action' => 'settings'), true); ?>/networks"> 
       Social Networks 
      </a>
    </li>
  </ul>
</div>
<div class="tab-content prepend-top prepend-1">
  <?php 
    if($tab == 'account') {
      echo $this->element('settings/account');
    } else if ($tab == 'password') {
      echo $this->element('settings/password');
    } else if ($tab == 'notifications') {
      echo $this->element('settings/notifications');
    } else {
      echo $this->element('settings/networks');
    }
  ?>
</div>
</div>

Element code:

<script type="text/javascript">
  $(document).ready(function(){

    var deleteNetwork = function(network, button) {
      $.ajax({
        url: '<?php echo $html->url('/fonykers/remove_network/', true); ?>' + network,
        dataType: 'json',
        type: 'POST',
        success: function(response) {
          if(response.ok) {
            button.replaceWith('<a id="'+network+'-connect" class="connect-button connect" href="'+response.url+'" class="span-3">Connect</a>');
          }
        }
      });  
    }

    if($('#twitter-connect').attr('href') == '#'){
      $('#twitter-connect').addClass('connected');
      $('#twitter-connect').html('Connected');
    } else {
      $('#twitter-connect').addClass('connect');
      $('#twitter-connect').html('Connect'); 
    }

    if($('#facebook-connect').attr('href') == '#'){
      $('#facebook-connect').addClass('connected');
      $('#facebook-connect').html('Connected');
    } else {
      $('#facebook-connect').addClass('connect');
      $('#facebook-connect').html('Connect'); 
    }

    $('.connected').hover(
      function() { 
        $(this).removeClass('connected');
        $(this).addClass('disconnect');
        $(this).html('Disconnect')
      },
      function() {
        $(this).removeClass('disconnect');
        $(this).addClass('connected');
        $(this).html('Connected')
      }
    );

    $('#twitter-connect').click(function(event) {
      if($(this).attr('href') == '#') {
        event.preventDefault();
        deleteNetwork('twitter', $(this));
      }
    });

    $('#facebook-connect').click(function(event) {
      if($(this).attr('href') == '#') {
        event.preventDefault();
        deleteNetwork('facebook', $(this));
      }
    });

  });
</script>
<div class="span-4 prepend-top">
<div class="span-4">
  <div class="span-1">
    <?php echo $html->image('twitter-connect.png', array('alt' => 'Twitter', 'class' => 'span-1',  'style' => 'height:40px;width:40px')); ?>
  </div>
  <div class="span-3 last">
    <a id="twitter-connect" class="connect-button" href="<?php echo $twitterUrl; ?>" class="span-3"></a>
  </div>
</div>
<div class="span-4 prepend-top">
  <div class="span-1">
  <?php echo $html->image('facebook-connect.png', array('alt' => 'Twitter', 'class' => 'span-1', 'style' => 'height:40px;width:40px')); ?>
  </div>
  <div class="span-3 last">
    <a id="facebook-connect" class="connect-button" href="<?php echo $facebookUrl; ?>"></a>
  </div>
</div>
</div>

Sorry for the long post.


In your retrieve_network action, which I am assuming you call when a user reconnects to a particular service, you are overwriting the NetworkData session variable if you find 1 particular Service has been connected to:

if(isset($_GET['oauth_token'])) {...} 

OR

if(isset($_GET['code'])) {...}

You set $networkData to the returned services object and then overwrite the whole session via:

$this->Session->write('NetworkData', $networkData);

Following your code, I would always check to see if an existing service is currently in session and if so do not overwrite the whole session, just add the particular data to the existing NetworkData session array:

if($this->Session->read('NetworkData.TwitterData')){
    $facebookData = array(
                'username' => $userinfo['username'],
                'name' => $userinfo['name'],
                'email' => $userinfo['email'],
                'token' => $token,
                'link' => $userinfo['link'],
    );
    $this->Session->write('NetworkData.FacebookData', $facebookData);
}

Note: This is just an example showing how you can achieve this. I would refactor that method with better logic for this particular situation, perhaps storing TwitterData and FacebookData in their own separate arrays as opposed to a larger more complex NetworkData array. Additionally, you can access $_GET params via $this->params['url']['paramname'] to maintain cakePHP convention.


I think the issue is that you are replacing network data, instead of merging it.

This means that when you login with FB for example, you replace any twitter data with FB, instead of keeping them both.

I'm hoping that maybe instead of

$this->Session->write('NetworkData', $networkData);

that

$this->Session->write('NetworkData', array_merge($networkData, $this->Session->read('NetworkData'));

might work.

Otherwise, it seems to me the session deleting code shouldn't be interacting for one to delete the other. I could have missed something in skimming it though. I'd do some echo()ing / debugging to follow the code execution paths.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号