Home V. Proxy Controlling

V. Proxy Controlling

a. What is a proxy

The proxy is the middleman between your client and the server. It gives you the ability to control what your client can do. The proxy is a PHP file. Your client (JavaScript) will send a HTTP request to this proxy and call the remote function on the other client through the XHR(Ajax) transport.

 

The proxy file will place within your application so that it can access your session data to determine the client’s ID. At current stage, the proxy only supports PHP application.

 

b. Extending proxy class

By default, there a file called proxy.php which already extended the proxy class (Jsrs_Proxy_Abstract) with the class name, “Standard”. The only function you need to override is the “_getClient” function.

 

This function is used to determine the type of client should be instantiate when the request was made. Because the proxy will be called by your client (JavaScript), you can determine the type of client by checking the session. For example Administrator, Author, Member or Normal User.

You can determine by using your session data:

protected function _getClient()
{
   switch ($_SESSION['user_type']) {
       "admin":
           return "Jsrs_Proxy_Client_Admin";
       "author":
           return "Jsrs_Proxy_Client_Author";
       "member":
           return "Jsrs_Proxy_Client_Member";
       default:
           return "Jsrs_Proxy_Client_User";
   }
}


The function is accessing the session data and determines the user’s type. Of course, this might different depends on your application. The function must return either the full class name or the client object itself. The client object must be child class from Jsrs_Proxy_Client_Abstract. Read on the next section on defining user’s type.

You can override the default property that defined in the Jsrs_Proxy_Abstract class too:

class Standard extends Jsrs_Proxy_Abstract
{
   protected $_host = "192.168.0.1";
   protected $_port = 12345;
   protected $_defaultPermissive = "denied";
}


If your JsRS server was hosted in another server, you might want to replace it with the IP address or the host name. You can change the listening port number if your server is listening at different port number. The $_defaultPermissive is to determine whether a message should be rejecting if the permission function was not defined (eg: sfPlus, cfSayHello). If you set to “denied”, means any server or client remote function that without defining the permission control function, will be rejected. By default, this value is “allowed”.

 

c. Defining the user's type

You might need to define the types of user that connected to your live application. By defining the user’s type, you can set the permission of each function for each type of user. By default, the “Standard” user class was defined for you. You must define at least 1 user type in order to run JsRS. For instance you wished to define a member user type:

class Jsrs_Proxy_Client_Member extends Jsrs_Proxy_Client_Abstract
{
    public function cfRedirect()
    {
        return false;
    }

    public function sfPlus()
    {
        return false;
    }
}



You should save this class file as Member.php under /proxy/client/. This proxy will prevent the Member type of user to call a remote function name “redirect”. Note the prefix in front of the function. “cf” is refer to client function, while “sf” is refer to server function. The users with Member type are NOT able to call the server function “plus” too. Any non-false value returned will allowed the message being pass through the proxy and send to the server. Throwing an exception will caused the message being reject:

public function sfPlus()
{
   throw new Jsrs_Proxy_Exception(“Message here”, “Title here”, 100);
}

 

Throwing the proxy exception (Jsrs_Proxy_Exception) will invoke the exception handler in the client (exceptionHandler). You can throw any other exception object such as RuntimeException or Exception, these exceptional objects will NOT invoke the exceptionHandler function in the client script.

Beside of controlling the remote function, you can control the event received too:

public function onClientConnected($client)
{
   if (false === in_array($client[“id”], $friendList)) {
       return false;
   }
}


The function above will check whether the connected client is actually in the friend list or not. If not, it will not send the event to the client (JavaScript) and thus, the “onClientConnected” event will NOT fired. Put this function inside your client class to control the event happened.

Note: For those functions that did not defined in your client class, by default, the message will be received or sent through. You can change this behavior by overriding the default value of $_defaultPermissive to “denied” in the Jsrs_Proxy_Abstract class.

d. Helpers

i. Introduction

The Helper class is to reduce the needs of extending your client class in order to perform some custom functionality. Most of the time, you might focus on the client class file (Jsrs_Proxy_Client_Class). For some client remote function, you might want to check the permission based on the MySQL database. Building the database connection on each client class is very troublesome neither extending your client class is a good idea too. What the Helper class can offer you is to allow you to define a class that access to the MySQL database and call it from your client class like this:

class Jsrs_Proxy_Client_Member extends Jsrs_Proxy_Client_Abstract
{
     public function cfSayHello()
     {
         return $this->_helper->IsAdmin($_SESSION[“user_id”]);
     }
}

 

You can use this helper “IsAdmin” on any client class with the same statement. Please be careful with the helper name, the first character must be capital letter and the name must be exactly same as the Helper class file.

 

The $_helper object is defined in the Jsrs_Proxy_Client_Abstract. You don’t have to worry about it. Just call it directly to get your result.

ii. The ServerBorker helper

By default, there is one helper class, “ServerBroker”. You can use this to invoke any server’s function without connecting to the server. The result returned is in array format. To call the function, you need at least one parameter which is the server’s function name. The second parameter has to be an array which represents your parameter. The parameter array can be an associative array. Example:

array(
    "parameter1" => "value1",
    "parameter2" => "value2"
)

 

To make a server function call, do this:

$result = $this->_helper->ServerBroker("plus", array(8, 50, 30));

The $result will return you an associative array with this value:

array(
    "result" => 88
)


Notice on how the parameter being passed. You have to put all the value in the array and pass the array in the second parameter of the Helper class.

iii. Creating your own Helper class

You must place your Helper class under /Proxy/Helper and your Helper class must inherit from Jsrs_Proxy_Helper_Abstract. You have to override the “run” method in order to perform your custom task. The “run” function is the default function when calling a Helper class:

$this->_helper->HelperClassName();
// Equivalent to Jsrs_Proxy_Helper_HelperClassName::run()

 

For instance you want to define a MySQL database accessing Helper, you created a Helper class file named Jsrs_Proxy_Helper_IsAdmin.php in /Proxy/Helper:

class Jsrs_Proxy_Helper_IsAdmin extends Jsrs_Proxy_Helper_Abstract
{
    public function run()
    {
        // Do your SQL connection and Query and return the Result.
    }
}

 

You can always extends your Helper class. For example you have few Helper class that needs to access to MySQL database, you can create a MySQL Helper class that will automatically connect to the database once the Helper class was called. Then you will extends those Helper that needs to access the database from this parent class.

Last modified on Monday, 09 August 2010 22:41