<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Revolves &#187; CodeIgniter</title>
	<atom:link href="http://www.revolves.net/tag/codeigniter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.revolves.net</link>
	<description>Innovation</description>
	<lastBuildDate>Wed, 05 May 2010 14:30:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using Zend Acl With CodeIgniter</title>
		<link>http://www.revolves.net/2008/12/18/using-zend-acl-with-codeigniter/</link>
		<comments>http://www.revolves.net/2008/12/18/using-zend-acl-with-codeigniter/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 13:27:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.revolves.net/?p=24</guid>
		<description><![CDATA[You&#8217;ll need to scroll horizontally to see the source codes. Best is to copy them and paste in your text editor I was just taking a look at the CodeIgniter forum, and found that many people require some sort of an Acl component to make their lives easier. Of course, Zend framework has an excellent [...]]]></description>
			<content:encoded><![CDATA[<p><em>You&#8217;ll need to scroll horizontally to see the source codes. Best is to copy them and paste in your text editor</em><br />
I was just taking a look at the CodeIgniter forum, and found that many people require some sort of an Acl component to make their lives easier. Of course, Zend framework has an excellent tried and tested Acl component. Also, the framework&#8217;s loose coupoling allows to just use a small part of it.</p>
<p>Seeing this, I tried implementing Zend&#8217;s Acl component into a default CI installation. Now, this is a very dirty work, I&#8217;ve just thrown stuffs here and there. But you&#8217;ll at least get an idea on how to use it. The major code goes into a CI library, which initializes the Acl object, roles, resources and rules for them.<br />
<span id="more-24"></span><br />
Lets begin with the directory structure,</p>
<p>I created a new directory called <i>Custom</i> inside the <i>application</i> directory. And here&#8217;s how the folder structure looks inside of <i>Custom</i>,</p>
<p>Custom<br />
 -Zend<br />
  &#8211;Acl<br />
  &#8211;Acl.php<br />
  &#8211;Exception.php (You&#8217;ll need this file too)</p>
<p>If you download the Zend framework, those are the only files you&#8217;ll need. Remember, you&#8217;ll need to have a folder called <i>Zend</i>, since this is how Zend refers to them in all of its includes. I apppend the <i>Custom</i> directory into PHP&#8217;s include path, since putting the <i>Zend</i> directory directly into the include path throws errors. That&#8217;s the reason I have the <i>Custom</i> folder in the first place. You&#8217;ll see all the include path stuff ahead, so don&#8217;t start worrying about them now.</p>
<p>Lets go to the library part. Now, lemme tell you beforehand that I&#8217;ve made no effort to organize code. My objective was to make the Acl component work in a convenient way. So pardon the bad coding practice if any <img src='http://www.revolves.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>In the <i>libraries</i> folder within your <i>application</i> folder, created a new library file named Zacl.php (I know I&#8217;m not creative). This is it&#8217;s contents.</p>
<pre class="brush: php;">&lt;?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Zacl
{
    function __construct()
    {
        session_start();
        //Append Zend's folder in PHP's include path
        set_include_path(get_include_path() . PATH_SEPARATOR . BASEPATH . &quot;application/Custom&quot;);

        //Load the Acl class
        require_once 'Zend/Acl.php';
        require_once 'Zend/Acl/Role.php';
        require_once 'Zend/Acl/Resource.php';

        //Create a new Acl object
        $this-&gt;acl = new Zend_Acl();

        /**
         * Add roles and resources. Check Zend's documentation for excellent
         * information on all these.
         * http://framework.zend.com/manual/en/zend.acl.html
         */
        $this-&gt;acl-&gt;addRole(new Zend_Acl_Role('guest'));
        $this-&gt;acl-&gt;addRole(new Zend_Acl_Role('users'),array('guest'));

        /**
         * Add some resources
         */
        $this-&gt;acl-&gt;add(new Zend_Acl_Resource('users_login'));
        $this-&gt;acl-&gt;add(new Zend_Acl_Resource('users_profile'));

        /**
         * Set rules for Acl
         */
        $this-&gt;acl-&gt;deny(); //Deny everything, so as to follow a whitelist approach.
        $this-&gt;acl-&gt;allow('guest','users_login');
        $this-&gt;acl-&gt;allow('users','users_profile');
    }

    function check_acl($resource)
    {
        if (!$this-&gt;acl-&gt;has($resource))
        {
            return 1;
        }

        if (isset($_SESSION['user_id']))
        {
            $role = 'users';
        }
        else
        {
            $role = 'guest';
        }

        return $this-&gt;acl-&gt;isAllowed($role,$resource);
    }

}</pre>
<p>Now, I have a constructor, which sets up everything. First of all, don&#8217;t be scared by the <i>session_start()</i> stuff, I just included it for a simple login/logout mechanism you&#8217;ll see later, to check if everythings fine. Even though I&#8217;ve commented the source code, I&#8217;ll just give a gist of what I&#8217;m doing.</p>
<p>1. Loading all required classes for Zend Acl.<br />
2. Adding Roles.<br />
3. Adding Resources (I elaborate on this below)<br />
4. Set access rules</p>
<p>If you see, the &#8216;users&#8217; role is actually a child of &#8216;guest&#8217; role, thus inheriting properties from it. You&#8217;ll need to check Zend&#8217;s manual to know about the syntax and stuff, since its explained quite well.</p>
<p>Now, both role and resource names are arbitrary. You can set them as whatever you want. I&#8217;ve used a small convention for resource part. I&#8217;ve named resources as &#8220;controller_action&#8221;. This is because, I can easily get controller and action name from the URI, and can easily query Acl from any controller.</p>
<p>In the next part, I simply set the access rules (quite self explanatory). I have a function called check_acl(), which takes a parameter named $resource. This is passed by the controller to the function, using the convention I told you about above. Zend throws an exception if a given resource couldn&#8217;t be found. Thus, if a resource is not in the Acl, I return 1, saying that its allowed to be accessed.</p>
<p>If the user has a session variable user_id, it means he&#8217;s logged in, thus I set his role to &#8216;users&#8217;. Else, his role is &#8216;guest&#8217;.</p>
<p>Now, to the controller path. I created a controller called Users, having this,</p>
<pre class="brush: php;">&lt;?php

class Users extends Controller
{
    function __construct()
    {
        parent::__construct();
        $this-&gt;load-&gt;library('zacl');

        $resource = $this-&gt;uri-&gt;segment(1) . '_' . $this-&gt;uri-&gt;segment(2);
        if (!$this-&gt;zacl-&gt;check_acl($resource))
        {
            $this-&gt;load-&gt;helper('url');
            redirect('/users/login');
        }
    }

    function login()
    {
        echo &quot;The login page&quot;;
    }

    function profile()
    {
        echo &quot;The restricted profile page&lt;br /&gt;&quot;;
        echo &quot;Your user id: &quot; . $_SESSION['user_id'];
    }

    function setsess()
    {
        $_SESSION['user_id'] = 1;
    }

    function remsess()
    {
        unset($_SESSION['user_id']);
    }
}</pre>
<p>In the constructor, I load the Acl library, and pass the controller_action to check_acl() function. If the output is 1, user is allowed, else he isn&#8217;t. If he isn&#8217;t, I redirect him to login page.</p>
<p>The login() and profile() functions are simply dummy functions, to check if Acl is working properly. If you look at the library, you&#8217;ll find that guest cannot access profile, while only a user can. The setsess() and remsess() functions are to set and remove user session to simulate logging in and logging out.</p>
<p>Well, I guess that&#8217;s it. I know I went a bit fast, but I really got tired typing this much. Anyways, if you have any sort of doubts, or if my code has some errors, please point out by commenting below! I&#8217;ve tested the above code in WAMP.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.revolves.net/2008/12/03/playing-with-zend-framework-17/" rel="bookmark" title="December 3, 2008">Playing With Zend Framework 1.7</a></li>
<li><a href="http://www.revolves.net/2008/12/07/an-update-on-my-zend-framework-17-experience/" rel="bookmark" title="December 7, 2008">An Update On My Zend Framework 1.7 Experience</a></li>
</ul>
<p><!-- Similar Posts took 4.669 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.revolves.net/2008/12/18/using-zend-acl-with-codeigniter/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.267 seconds -->
