home | contact us
» Posts tagged "auth"

Items Tagged: auth


I have recently been working on a Magento project that required a custom API integration with Zen Desk.

Magento is based on Zend Framework which is nice because it means we can use the Zend_Rest_Client object to handle the API integration which provides a nice layer of abstraction and sorts out a lot of the heavy lifting required.

However I came across a terminal issue trying to get it working properly with HTTP Basic Authentication which the Zen Desk API uses.

I found some instructions which recommended creating a Zend_Uri_Http object and then setting the username and password against this. However as Zen Desk uses the email address as the username component, this failed completely.

In the end the solution I found was to grab the static HTTP client object that the REST client is built upon and call the public setAuth method on this. This way the Authorisation header is correctly created, encoded and appended to the headers.

Logically it feels like a dirty way to do things, grabbing the HTTP client statically to do this, but it works and I can’t see how else you would do it.

For info here is the code:

/**
     * @return Zend_Rest_Client 
     */
    protected function getClient() {
        if (null === $this->_client) {
            $url = $this->getApiUrl();
            $uri = Zend_Uri_Http::fromString($url);
            $this->_client = new Zend_Rest_Client($uri);
            Zend_Service_Abstract::getHttpClient()->setAuth($this->getUsername(),$this->getPassword());
        }
        return $this->_client;
    }

 

Reading the generally very clear and concise Yii documentation you can quickly get started with the framework.

However one issue that I struggled to be clear on is setting up the database driven authorisation system.

I recommend reading this blog post which finally helped me to understand wth I was supposed to do. The official documentation is here which you should also read.

There are a few key points that are not really clear but are important.

1. You need to create the tables.

There is a file: yii/framework/web/auth/schema-mysql.sql

You need to run this on your database to build the necessary tables#

2. Creating the rules – use the Yii Shell

You will see examples of PHP code that defines rules etc. You may wonder where the hell you are supposed to put this code and also is it really a good idea to run this kind of code on every page load, seems odd.

Well the answer is you are only meant to run it once and you can either make a PHP script that runs it or even nicer is to use the Yii interactive shell and paste the lines in one at a time to execute them.

Using the Yii Shell

Go to your web root on the command line (I’m using Linux, good luck if you aren’t)

cd ~/Projects/yii-projects/public

Now run the yiic tool from the web root with the shell flag

protected/yiic shell

You now have an interactive PHP shell with the Yii framework available. You can create models, crud etc as you might have done in Gii, but you can also run PHP code directly, including working with Yii objects – such as the authManager

Try this:

Paste this line into your Yii shell and hit return

$auth=Yii:app()->authManager;

Then this line and hit return

var_dump($auth);

You should see a var dump of the CDbAuthManager object

Now when you see examples such as this:

$auth=Yii::app()->authManager;
 
$auth->createOperation('createPost','create a post');
$auth->createOperation('readPost','read a post');
$auth->createOperation('updatePost','update a post');
$auth->createOperation('deletePost','delete a post');

You realise that this is something you can do ONCE to set up these roles, and a great way to do that is via the Yii Shell.

Have fun ;)


 
rss icon