0
votes

I have a user registration page that launches a third party script for a game, i only want this page to finish loading if the user accepts the permission (it will set a HTTP variable). I currently have it prompting for permission but is loading the page in the background, how would i go about "waiting" or "reloading" the page to re-check if this variable is set yet?

Controller function for add

public function add() {
    if ($this->User->IGBRequireTrust()) {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }
}

Public function in Model that is called above

// Returns TRUE if the user trusts the site, FALSE otherwise.
    public function IGBRequireTrust()
    {
        if($_SERVER['HTTP_EVE_TRUSTED'] != 'yes')
        {
            echo "<script>CCPEVE.requestTrust('http://*.".$_SERVER['HTTP_HOST']."');</script>";
        }
        return true;
    }

The page needs to redirect back to the index function with a session flash msg set if they do not accept the permission. I tried adding an else in where it calls the IGBRequireTrust function but it seems to always do both.

UPDATE:

AppController Function

public function beforeFilter() { $this->Auth->allow('index', 'view');

if($this->name == 'Users' && $this->action == 'add'){
    //die('correct controller and action');
    if(isset($_SERVER['HTTP_EVE_TRUSTED'])){
        if($_SERVER['HTTP_EVE_TRUSTED'] != 'yes' && $this->action != 'promptEveTrusted'){
            $this->redirect(array('controller'=>'Users', 'action'=>'promptEveTrusted'));
        }
    } else {
        $this->Session->setFlash(__('Registration is only allowed through the EVE in game browser.'));
        $this->redirect(array('controller'=>'Users', 'action'=>'index'));
    }
//} else {
  //  die('wrong controller and action');
}

}

2

2 Answers

0
votes

Looks like you are injecting javascript into the page, which will not get rendered until after all php code. Maybe try using javascript as your logic here. Or jquery would be super easy. Also, $this->User->IGBRequireTrust() is always returning true. put return false in your if statement

// Returns TRUE if the user trusts the site, FALSE otherwise.
    public function IGBRequireTrust()
    {
        if($_SERVER['HTTP_EVE_TRUSTED'] != 'yes')
        {
            echo "<script>CCPEVE.requestTrust('http://*.".$_SERVER['HTTP_HOST']."');</script>";
            return false;
        }
        return true;
    }

<script>
var eve_trusted = '<?php echo $_SERVER['HTTP_EVE_TRUSTED']; ?>';
var declined_eve_url = '<?php echo $this->webroot; ?>users/unauthorized';
//Might have to wait for your script injection to load so we will wait for document ready
$(function(){
    if(!eve_trusted) window.location.href = declined_eve_url;
});
</script>

Here I would offer you help in canceling the page load with js but, I am not sure of the order that your main script will load.

Alternatively, you could clean all of this up by putting your logic in the beforeFilter of your AppController.ctp

function beforeFilter(){
    if($_SERVER['HTTP_EVE_TRUSTED'] != 'yes' && $this->action != 'promptEveTrusted')
        $this->redirect(array('controller'=>'User', 'action'=>'promptEveTrusted'));
}

//USERS CONTROLLER
function promptEveTrusted(){
    //This will give the user a blank template, comment if you don't wish this
    $this->layout = 'ajax';
}
//views / users / prompt_eve_trusted.ctp
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script>
  $(function(){
     CCPEVE.requestTrust('http://*.".$_SERVER['HTTP_HOST']."');
  });
 </script>
0
votes

I would load the page without the http variable and that would just show a message (by javascript, for example).

If the user accepts it, then, javascript will redirect him to the same page with a variable, which will then load everything you want when the variable is set.

If he doesn't accept it, it will just keep in the same page or redirect him to another one, as you prefer.