Tuesday, October 6, 2009

How to set basic authentication for php in flex 4

I have listed down the steps which will help you to use basic authentication for php services using Zend in FlashBuilder4

1. Have a function called initAcl($acl) in the php service similar to the one given below. All the functions inside the service which need authentication should be mentioned in this function( i.e., initAcl)

function initAcl(Zend_Acl $acl) {
$acl->allow('role1', null, 'function1');
$acl->allow('role2', null, array('function1', 'function2'));
return true;
}

Please find a sample service here

2. Have a security.xml file which specifies the user roles and has user name and passwords stored in it. Sample contents of the xml file is given below.

<roles>
<role id='admin'>
<user name='user1' password='pwd'/>
</role>
<role id='hr'>
<user name='user2' password='pwd2'/>
</role>
</roles>

3. Security.xml file can be placed anywhere (placing it outside the webroot is recommended. ) The path has to be specified in the gateway.php file . Also a small code has to be added in the gateway.php file as given below.

$auth = new Zend_Amf_Adobe_Auth("C:\wamp\security.xml");
$server->setAuth($auth);
$server->setAcl($auth->getAcl());

Refer to this gateway.php file to know where exactly should the above code be added

4. below code has to be added in the mxml, where serviceName1 is the name of the service created.

serviceName1.serviceControl.setCredentials(
'username','password',null);
Support for basic authentication in php is added in Beta 2 release of FlashBuilder 4.