<?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>The Zend Framework in Practice</title>
	<atom:link href="http://zf.gm-ram.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://zf.gm-ram.com</link>
	<description>Developing Web Applications with the Zend Framework</description>
	<lastBuildDate>Mon, 02 Jan 2012 10:20:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Beginning Databases with PostgreSQL by Neil Matthew and Richard Stones</title>
		<link>http://zf.gm-ram.com/posts/beginning-databases-with-postgresql-by-neil-matthew-and-richard-stones/</link>
		<comments>http://zf.gm-ram.com/posts/beginning-databases-with-postgresql-by-neil-matthew-and-richard-stones/#comments</comments>
		<pubDate>Sun, 17 Jul 2011 13:31:29 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Book Reviews]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=929</guid>
		<description><![CDATA[A little while back I need to work with a custom framework that used PostgreSQL as its database. In particular, I needed to be able to set the site up on various machines both for testing and deployment purposes. As I had absolutely no experience of working with PostgreSQL, I went looking for a suitable [...]]]></description>
			<content:encoded><![CDATA[<p><iframe src="http://rcm-uk.amazon.co.uk/e/cm?t=thezenfrainpr-21&#038;o=2&#038;p=8&#038;l=as1&#038;asins=1590594789&#038;ref=tf_til&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></p>
<p>A little while back I need to work with a custom framework that used PostgreSQL as its database. In particular, I needed to be able to set the site up on various machines both for testing and deployment purposes. As I had absolutely no experience of working with PostgreSQL, I went looking for a suitable book, and settled on this one. It proved to be an excellent choice.<span id="more-929"></span></p>
<p>The book covers the following broad areas relating to PostgreSQL:</p>
<ul>
<li>Installing and configuring PostgreSQL</li>
<li>Working with PostgreSQL via the command line, via various GUIs and via the browser</li>
<li>Programming with PostgreSQL using C, PHP, Perl, Java and C#</li>
</ul>
<p>However, the book also has value in that it covers more general topics of database design and how to construct more complex SQL queries, something which I have found is not generally well explained. For instance, most PHP books that also cover MySQL give a basic coverage of the SELECT, INSERT, UPDATE, and DELETE commands, and perhaps a bit on adding/editing/removing databases and users, but not much assistance on important but tricky topics such as how to combine data from multiple tables.</p>
<p>One thing I found was that PostgreSQL is just that bit more complex that MySQL to set up and administer, so the book was especially valuable in that respect. I was told that PostgreSQL aspired to be an open source replacement for Oracle. I suppose that they have succeeded, in that it can be just as much a pain to use at times.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/beginning-databases-with-postgresql-by-neil-matthew-and-richard-stones/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Storing Arrays in a Database Using JSON</title>
		<link>http://zf.gm-ram.com/posts/storing-arrays-in-a-database-using-json/</link>
		<comments>http://zf.gm-ram.com/posts/storing-arrays-in-a-database-using-json/#comments</comments>
		<pubDate>Sun, 17 Jul 2011 11:32:25 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Json]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=925</guid>
		<description><![CDATA[Sometimes you need to store arrays in your MySQL (or other relational) database, but don&#8217;t want to spread the information over multiple tables. For instance, I have been working on a CMS for a website, where you need to be able to add a variable selection of images, download links and products to the page [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to store arrays in your MySQL (or other relational) database, but don&#8217;t want to spread the information over multiple tables. For instance, I have been working on a CMS for a website, where you need to be able to add a variable selection of images, download links and products to the page configuration. Storing these sets of information in a single field was obviously a far cleaner solution. However, I needed a simple way to encode/decode the data.<span id="more-925"></span></p>
<p>The download links could be reduced to a comma-separate list of tags, as there was already a lookup table mapping the tag against the appropriate image (for the download type) and the link itself. However, the information required for the images and products was more complex. For example, for the images, I needed to be able to store the &#8220;src&#8221;, &#8220;height&#8221;, &#8220;width&#8221; and &#8220;alt&#8221; attributes. Each image was represented by an array with these as named elements; these image arrays were themselves grouped together into an array representing all the images.</p>
<p>The solution I used for this (and the product list, which was even more complex) was to store the array as a JSON string in the database. So to convert the array to a JSON string I used the appropriate Zend Framework class as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$images_json</span> <span style="color: #339933;">=</span> Zend_Json<span style="color: #339933;">::</span><span style="color: #004000;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To convert the JSON string back to an array I used the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$image_list</span> <span style="color: #339933;">=</span> Zend_Json<span style="color: #339933;">::</span><span style="color: #004000;">decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$images_json</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/storing-arrays-in-a-database-using-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using CKEditor with the Zend Framework</title>
		<link>http://zf.gm-ram.com/posts/using-ckeditor-with-the-zend-framework/</link>
		<comments>http://zf.gm-ram.com/posts/using-ckeditor-with-the-zend-framework/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 16:39:24 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[ZendX_JQuery]]></category>
		<category><![CDATA[Zend_Form]]></category>
		<category><![CDATA[Zend_Layout]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=913</guid>
		<description><![CDATA[At the moment I am working on create a system that allows non-technical users to easily add and edit product pages on one of the website I have developed. Until now, adding or modifying a page has required modifications to the database and/or a PHP script file, along with uploading any new resources, such as [...]]]></description>
			<content:encoded><![CDATA[<p>At the moment I am working on create a system that allows non-technical users to easily add and edit product pages on one of the website I have developed. Until now, adding or modifying a page has required modifications to the database and/or a PHP script file, along with uploading any new resources, such as images or documents. This has meant in practice that either I or one of the more technically minded people at the client has tended to do these changes. The idea of the new system is that it will allow anyone to add/edit product pages.<span id="more-913"></span></p>
<p>Each product page has a standard layout. The page divides into two vertical sections; the top vertical section is divided into two columns. The left column contains the main text describing the products on that page; the right column contains supplementary images and download links. The bottom vertical section contains a jQuery UI tab control, with one tab for each section of products being offered for sale on that page.</p>
<p>I needed to allow the user to edit the HTML used in the top left section. However, I didn&#8217;t want the user to have to write raw HTML, but rather to use a WYSIWYG editor. I identified the Open Source JavaScript editor CKEditor as a suitable candidate.</p>
<p>The process of integrating this editor with the existing Zend Framework application proved to be pretty simple:</p>
<ol>
<li>Download the latest version of the editor from <a href="http://ckeditor.com/" title="CKEditor website">the CKEditor website</a>.</li>
<li>Having unpacked the download, I uploaded the CKEditor directory (<em>ckeditor</em>) under the public web root (<em>htdocs</em>).</li>
<li>The layout script already contained the following in the page head, which forced the appropriate tags to load and enable jQuery to be added:

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">jQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">enable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>I added the following at the top of the view scripts that were going to make use of the editor. Note that the ID of the form field that was going to be enhanced by the editor was <strong>description</strong>.

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// CKEditor</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">jQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addJavascriptFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/ckeditor/ckeditor.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">jQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addJavascriptFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/ckeditor/adapters/jquery.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">jQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addOnload</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;$('#description').ckeditor();&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>I created a class to handle the form that used the editor. The class was named <strong>Application_Form_ProductPage</strong> and ultimately derived from Zend_Form via Application_Form_Abstract.</li>
<li>The init method of the class contained the following code to add the textarea that was enhanced going to be the editor:

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'textarea'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'description'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'label'</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Product Description'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'decorators'</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFieldDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
</ol>
<p>As a result of the able, the textarea was transformed into a fully-blown WYSIWYG editor. The raw HTML was passed behind the scenes, and was added to/retrieved from the database.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/using-ckeditor-with-the-zend-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Latest News</title>
		<link>http://zf.gm-ram.com/posts/latest-news/</link>
		<comments>http://zf.gm-ram.com/posts/latest-news/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 17:55:52 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=906</guid>
		<description><![CDATA[Due to some problems with my ISP, I temporarily lost the main GM-RAM website. As I was planning a revamp of the site, I decided not to restore the old version, but put up a simple front page with the basic company details. As a result, you can no longer see in action what has [...]]]></description>
			<content:encoded><![CDATA[<p>Due to some problems with my ISP, I temporarily lost the main GM-RAM website. As I was planning a revamp of the site, I decided not to restore the old version, but put up a simple front page with the basic company details. As a result, you can no longer see in action what has been described in some of the previous posts. I am hoping to put up a new site fairly soon, with content that reflects more accurately the company&#8217;s activities.</p>
<p>On another note, blogging has been light this year as I have been distracted by other, non-PHP based projects. However, I intend to put up a few new posts over the next week or so, given that my recent work has involved using PHP and related technologies again.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/latest-news/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Access Control</title>
		<link>http://zf.gm-ram.com/posts/simple-access-control/</link>
		<comments>http://zf.gm-ram.com/posts/simple-access-control/#comments</comments>
		<pubDate>Fri, 28 Jan 2011 18:26:39 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Auth]]></category>
		<category><![CDATA[Zend_Controller]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=896</guid>
		<description><![CDATA[In my previous post, I looked at using a controller plugin as part of a more complex access control system based on an access control list. However, sometimes a simpler solution makes sense. For instance, in a recent project, I just needed to restrict access to the admin section of the site. The admin section [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://zf.gm-ram.com/posts/the-access-control-plugin/">my previous post</a>, I looked at using a controller plugin as part of a more complex access control system based on an access control list. However, sometimes a simpler solution makes sense. For instance, in a recent project, I just needed to restrict access to the admin section of the site. The admin section was contained within the admin module. I therefore need to check only two things:</p>
<ol>
<li>Was the user trying to access something in the admin section?</li>
<li>Was the user logged in as admin?</li>
</ol>
<p>I still needed to add the mechanism for logging in, but there was no need for an access control list, and the implementation of the Access Control Plugin was much simpler.<span id="more-896"></span></p>
<p>As before, the plugin needs to be registered in the configuration file:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.frontController.plugins.accessControl <span style="color: #000066; font-weight:bold;">=</span> 
  <span style="color: #933;">&quot;Application_Plugin_AccessControl&quot;</span></pre></div></div>

<p>The implementation of the plugin is much simpler, however:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Application_Plugin_AccessControl <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Plugin_Abstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> preDispatch<span style="color: #009900;">&#40;</span>Zend_Controller_Request_Abstract <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Get the current user role</span>
    <span style="color: #000088;">$role</span> <span style="color: #339933;">=</span> Application_Model_Identity_Current<span style="color: #339933;">::</span><span style="color: #004000;">getRole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Get the request module</span>
    <span style="color: #000088;">$module</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Determine whether the user is allowed to access the module</span>
    <span style="color: #000088;">$accessDenied</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'admin'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$role</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'admin'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$accessDenied</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// If access is denied, redirect to the login page</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$accessDenied</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'default'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setControllerName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setActionName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The workings of the class should be self-evident from the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/simple-access-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Access Control Plugin</title>
		<link>http://zf.gm-ram.com/posts/the-access-control-plugin/</link>
		<comments>http://zf.gm-ram.com/posts/the-access-control-plugin/#comments</comments>
		<pubDate>Wed, 08 Dec 2010 19:28:34 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Acl]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Auth]]></category>
		<category><![CDATA[Zend_Controller]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=877</guid>
		<description><![CDATA[This post deals with the last piece of the access control/authentication jigsaw: the Access Control plugin. It shows how an application can makes use of the Zend Framework&#8217;s controller plugin mechanism to apply the access control list to a user request. Let us start with the plugin itself. It is called Application_Plugin_AccessControl and located in [...]]]></description>
			<content:encoded><![CDATA[<p>This post deals with the last piece of the access control/authentication jigsaw: the Access Control plugin. It shows how an application can makes use of the Zend Framework&#8217;s controller plugin mechanism to apply the access control list to a user request.<span id="more-877"></span></p>
<p>Let us start with the plugin itself. It is called Application_Plugin_AccessControl and located in the <em>application/plugins</em> directory. To locate the class definition, the autoloader strips the standard prefix <strong>Application_</strong>, which is declared in the configuration file, from the class name, then maps the <strong>Plugin_</strong> part to that directory. The plugin itself is declared in the configuration file as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.frontController.plugins.accessControl <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Application_Plugin_AccessControl&quot;</span></pre></div></div>

<p>This ensures that the plugin is initialised and used during the despatch process.</p>
<p>The plugin itself is derived from Zend_Controller_Plugin_Abstract and implements the standard preDispatch method:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Application_Plugin_AccessControl <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Plugin_Abstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> preDispatch<span style="color: #009900;">&#40;</span>Zend_Controller_Request_Abstract <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// TODO: Apply access control</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>By implementing this method, the plugin will be called via it before any request is despatched to the relevant controller. This allows access control to be applied by redirecting a request, if the user is after something that it out of bounds.</p>
<p>The process of applying access control involves a series of steps. These are given below.</p>
<p>Step 1: Get the authentication object. This is used to determine whether the user is logged in, and if so, then as whom.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStorage</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Auth_Storage_Session<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Step 2: Get the access control list. This is done by using the static getAcl method of the Application_Model_Acl class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$acl</span> <span style="color: #339933;">=</span> Application_Model_Acl<span style="color: #339933;">::</span><span style="color: #004000;">getAcl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Step 3: Get the current user role and determine whether the user is logged in.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$role</span> <span style="color: #339933;">=</span> Application_Model_Identity_Current<span style="color: #339933;">::</span><span style="color: #004000;">getRole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$loggedIn</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasRole</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$role</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$role</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'guest'</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$loggedIn</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Step 4: Use the module name and controller name as the resource against which to check. Note that the module is added as a prefix, if it is not the default module.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$module</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getControllerName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$module</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$controller</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">else</span>
      <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$module</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">':'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$controller</span><span style="color: #339933;">;</span></pre></div></div>

<p>Step 5: Use the action name as the privilege. The privilege is a sub-division of a resource, to allow finer-grained control of access. In our case, we use it to distinguish between different actions under the same controller.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$privilege</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getActionName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Step 6: If the resource is not in the ACL, use the default resource and privilege.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">has</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'index'</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$privilege</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'index'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Step 7: Check whether the user is allowed access to the specified resource. If not, display the home page instead if the user is logged in, otherwise display the login page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isAllowed</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$role</span><span style="color: #339933;">,</span> <span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> <span style="color: #000088;">$privilege</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$loggedIn</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'default'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setControllerName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setActionName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'default'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setControllerName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setActionName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-access-control-plugin/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Access Control List Class</title>
		<link>http://zf.gm-ram.com/posts/the-access-control-list-class/</link>
		<comments>http://zf.gm-ram.com/posts/the-access-control-list-class/#comments</comments>
		<pubDate>Thu, 02 Dec 2010 10:59:15 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Acl]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=863</guid>
		<description><![CDATA[In a previous post, I discussed the creation of an Authentication class. In the following post, I discussed the creation of an Identity class. In this post I shall look at the creation of an Access Control List class. I&#8217;d like to start by considering the function of each of these classes, and the relationship [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post, <a href="http://zf.gm-ram.com/posts/the-authentication-class/">I discussed the creation of an Authentication class</a>. In the following post, <a href="http://zf.gm-ram.com/posts/the-identity-class/">I discussed the creation of an Identity class</a>. In this post I shall look at the creation of an Access Control List class.<span id="more-863"></span></p>
<p>I&#8217;d like to start by considering the function of each of these classes, and the relationship between them.</p>
<p>The Authentication class is concerned with verifying a person&#8217;s identity. The Identity class is used to encapsulate a person&#8217;s identity. The Access Control List class is a description of what particular persons are permitted to access.</p>
<p>The Identity class can be seen as acting as the <em>glue</em> between the Authentication and Access Control List class. The role is the primary piece of information stored in the Identity class that is used by the Access Control List class to determine what is accessible and what is not to that user.</p>
<p>We shall look at how the Access Control List class can be used in the next post. This post will be concerned with how the class is implemented.</p>
<p>The class is essentially a wrapper around a Zend_Acl object, which is initialised in the class. This allows the specific Access Control List used by an application to be stored in one place, while the common code for the other components can be reused directly between applications.</p>
<p>The skeleton code for the class is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Application_Model_Acl
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> static <span style="color: #000088;">$_acl</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getAcl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// Create the access control list</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Acl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// TODO: Create and add the roles</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// TODO: Create and add the resources</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// TODO: Set up the access control</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that the method for retrieving the Access Control List object is static, and that the object is only created if it has not already been initialised.</p>
<p>Zend_Acl works with two basic collections of objects: roles and resources.</p>
<p>Roles are used to group users, and assign permissions to those groups en masse.</p>
<p>In our example we have two roles, which are added like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #666666; font-style: italic;">// - Create the roles</span>
      <span style="color: #000088;">$guestRole</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Acl_Role<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'guest'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$userRole</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Acl_Role<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// - Add the roles</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addRole</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$guestRole</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addRole</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userRole</span><span style="color: #339933;">,</span> <span style="color: #000088;">$guestRole</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <strong>guest</strong> role represents anyone accessing the site who has not logged in. The <strong>user</strong> represents anyone accessing the site who has logged in.</p>
<p>Note that when adding the user role, we pass the guest role as the second parameter; this means that the user inherits all the permissions of the guest role. This is logical, as logging in should extend what a user can do on the site.</p>
<p>Resources represent things that a user might try to access on the website. In the scheme I am using here, each resource maps directly to a controller. Therefore, we need a resource for each controller used by the application.</p>
<p>Let us assume that the application has four controllers: index, error, login and user. We need to add a resource for each one.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #666666; font-style: italic;">// - Create the resources</span>
      <span style="color: #000088;">$indexResource</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Acl_Resource<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$errorResource</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Acl_Resource<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$loginResource</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Acl_Resource<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$userResource</span><span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Acl_Resource<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// - Add the resources</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addResource</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$indexResource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addResource</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errorResource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addResource</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$loginResource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addResource</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userResource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now we have defined the roles and resources, we can wire them up together, to indicate what can and cannot be accessed by users with a particular role.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #666666; font-style: italic;">// - Set up the access control</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deny</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allow</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errorResource</span><span style="color: #339933;">,</span> <span style="color: #000088;">$loginResource</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userRole</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$indexResource</span><span style="color: #339933;">,</span> <span style="color: #000088;">$userResource</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This scheme allows guests only to visit the login page (and to see the error page, if necessary). To visit the home page or user page, the user must first log in successfully.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-access-control-list-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Identity Class</title>
		<link>http://zf.gm-ram.com/posts/the-identity-class/</link>
		<comments>http://zf.gm-ram.com/posts/the-identity-class/#comments</comments>
		<pubDate>Thu, 25 Nov 2010 18:30:52 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Auth]]></category>
		<category><![CDATA[Zend_Db]]></category>
		<category><![CDATA[Zend_Log]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=843</guid>
		<description><![CDATA[In my previous post about creating an authentication class, I made use of another class, called Application_Model_Identity, to store the details of the user, who has logged in. In this post, I intended to look at some of the things that can be done with such a class, drawing on some of the applications that [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://zf.gm-ram.com/posts/the-authentication-class/">my previous post about creating an authentication class</a>, I made use of another class, called Application_Model_Identity, to store the details of the user, who has logged in. In this post, I intended to look at some of the things that can be done with such a class, drawing on some of the applications that I have developed myself.<span id="more-843"></span></p>
<p>A basic version of the class would look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Application_Model_Identity
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_role</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultRow</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultRow</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_username <span style="color: #339933;">=</span> <span style="color: #000088;">$resultRow</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultRow</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">role</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_role <span style="color: #339933;">=</span> <span style="color: #000088;">$resultRow</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">role</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultRow</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_name <span style="color: #339933;">=</span> <span style="color: #000088;">$resultRow</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUsername<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_username<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getRole<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_role<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_name<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This provides an unremarkable means of encapsulating the essential information retrieved via the result row.</p>
<p>As we shall see in a later post, having a role defined for each user is a basic method for providing more finely-grained access control. Typically, a site will allow users to log in at various levels, such as admin.</p>
<p>It is possible to do some more advanced things here.</p>
<p>For instance, in one application I have written, the user can be a member of one or more sales organisations. To support this we have an additional member variable to store these sales organisations:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_salesOrgs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We then extend the signature of the constructor to take the database adapter and logger as additional parameters:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultRow</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbAdapter</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Then in the constructor, below the lines</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultRow</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_name <span style="color: #339933;">=</span> <span style="color: #000088;">$resultRow</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span></pre></div></div>

<p>we add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_salesOrgs <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$dbAdapter</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        try
        <span style="color: #009900;">&#123;</span>
          <span style="color: #666666; font-style: italic;">// Get the user's sales organisations</span>
          <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteInto</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">&quot;SELECT sales_org_id, name FROM sales_org WHERE sales_org_id IN 
(SELECT DISTINCT sales_org_id FROM user_sales_org WHERE username=?)&quot;</span><span style="color: #339933;">,</span> 
<span style="color: #000088;">$resultRow</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_salesOrgs <span style="color: #339933;">=</span> <span style="color: #000088;">$dbAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAssoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$log</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">err</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #009900;">&#41;</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #339933;">-&gt;</span><span style="color: #004000;">crit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Database adapter not specified'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>We also add a means to retrieve the sales organisations:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getSalesOrgs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_salesOrgs<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>To retrieve the user&#8217;s identity, and therefore all the above information, elsewhere in the application, we use the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$identity</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-identity-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Authentication Class</title>
		<link>http://zf.gm-ram.com/posts/the-authentication-class/</link>
		<comments>http://zf.gm-ram.com/posts/the-authentication-class/#comments</comments>
		<pubDate>Sun, 21 Nov 2010 18:07:29 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Auth]]></category>
		<category><![CDATA[Zend_Db]]></category>
		<category><![CDATA[Zend_Log]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=828</guid>
		<description><![CDATA[I prefer to encapsulate the code for logging in and out in a class. In the example I am using for this series of posts, the class is called Application_Model_Login. We have already seen in this post how it is used in the Login Controller. The class has following methods and member variables: class Application_Model_Login [...]]]></description>
			<content:encoded><![CDATA[<p>I prefer to encapsulate the code for logging in and out in a class. In the example I am using for this series of posts, the class is called Application_Model_Login. We have already seen in <a href="http://zf.gm-ram.com/posts/the-login-controller/">this post</a> how it is used in the Login Controller.<span id="more-828"></span></p>
<p>The class has following methods and member variables:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Application_Model_Login
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_dbAdapter</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_log</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbAdapter</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// TODO: Insert construction code here</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// TODO: Insert login code here</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> logout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// TODO: Insert logout code here</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When using the class, we pass the constructor references to the database adapter and log objects to use; these are stored in the appropriate member variables:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbAdapter</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_dbAdapter <span style="color: #339933;">=</span> <span style="color: #000088;">$dbAdapter</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log <span style="color: #339933;">=</span> <span style="color: #000088;">$log</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The login method receives the username and password from the calling function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span></pre></div></div>

<p>The database adapter must be set, otherwise the class cannot authenticate against the database:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Check that the database adapter has been set</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_dbAdapter<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #339933;">-&gt;</span><span style="color: #004000;">crit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Database adapter not specified'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that the method uses the logger (if it has been supplied) to record this failure. Also, the method returns false to indicate that the login was unsuccessful.</p>
<p>Next, the method gets the date and time of the login attempt; this will be logged later:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Get the date and time of the attempt</span>
    <span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then, the method gets the global authentication object:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Get the authentication object</span>
    <span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next, the method creates an authentication adapter:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Set up the authentication adapter</span>
    <span style="color: #000088;">$authAdapter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Auth_Adapter_DbTable<span style="color: #009900;">&#40;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_dbAdapter<span style="color: #339933;">,</span> <span style="color: #0000ff;">'user'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'md5(?)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$authAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$authAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredential</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The adapter is passed the username and password supplied to the method. In this case, the database is assumed to have a <em>user</em> table, with fields named <em>username</em> and <em>password</em>. Also, the password has been encrypted using MD5.</p>
<p>The method is now ready to authenticate the user, using the global authentication object and the adapter:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Attempt to authenticate the user</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$authAdapter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If the attempt is successful, the method now gets the entire row in the database table associated with the user. This information is used to create the identity object. This object is an instance of the class Application_Model_Identity, which we will examine in a subsequent post. This identity is stored in the global authentication object. The application will be able to determine whether the user is logged in by the existance of the identity.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #666666; font-style: italic;">// Check the result of the attempt</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// Get the result row object</span>
      <span style="color: #000088;">$resultRow</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$authAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResultRowObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Create the identity object</span>
      <span style="color: #000088;">$identity</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_Identity<span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultRow</span><span style="color: #339933;">,</span> 
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_dbAdapter<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Store it in the current session</span>
      <span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStorage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$identity</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The method uses the logger (if it has been supplied) to record this success, including the date of the attempt:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Log the successful login</span>
        <span style="color: #000088;">$notice</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Successful login from %s by %s at %s'</span><span style="color: #339933;">,</span>
          <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #339933;">-&gt;</span><span style="color: #004000;">notice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$notice</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally, the method returns true, to indicate that the login succeded:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>If the login attempt fails, the method uses the logger (if it has been supplied) to record this failure, including the date of the attempt:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Log the failed login attempt</span>
        <span style="color: #000088;">$warn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'Failed login attempt (invalid username/password) from %s by %s at %s'</span><span style="color: #339933;">,</span>
          <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #339933;">-&gt;</span><span style="color: #004000;">warn</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$warn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>It then returns false, to indicate that the login failed:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The logout function is quite simple. We get the authentication object, then clear any identity stored in it:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> logout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Get the authentication object</span>
    <span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Clear the user identity to log out</span>
    <span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The application will now regard the user as being logged out.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-authentication-class/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The Login Page: The Login Form</title>
		<link>http://zf.gm-ram.com/posts/the-login-page-the-login-form/</link>
		<comments>http://zf.gm-ram.com/posts/the-login-page-the-login-form/#comments</comments>
		<pubDate>Mon, 15 Nov 2010 22:51:07 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Form]]></category>
		<category><![CDATA[Zend_Validate]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=817</guid>
		<description><![CDATA[The Login Form is a simple form, consisting of three elements: the username field, the password field and the login button. The form class is derived from an abstract base class of my devising. This contains the functionality shared between all forms in an application. The abstract base class is itself derived from Zend_Form. The [...]]]></description>
			<content:encoded><![CDATA[<p>The Login Form is a simple form, consisting of three elements: the username field, the password field and the login button.<span id="more-817"></span></p>
<p>The form class is derived from an abstract base class of my devising. This contains the functionality shared between all forms in an application. The abstract base class is itself derived from Zend_Form.</p>
<p>The abstract base class is defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> Application_Form_Abstract <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFieldDecorators<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ViewHelper'</span><span style="color: #339933;">,</span> 
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HtmlTag'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dd'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Label'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dt'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFirstFormError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessages</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$field</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$messages</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$messages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$message</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'field'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$error</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The class supplies contains two functions, both of which are connected to the rendering of error messages.</p>
<p>The first function modifies the standard field decorators to remove the error decorator, as I don&#8217;t usually want any error messages displayed beside the field. Instead, I prefer to show the first error message at the top of the form; it is the task of the second function to retrieve this.</p>
<p>The actual Login form class is straighforward:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Application_Form_Login <span style="color: #000000; font-weight: bold;">extends</span> CapArgo_Form_Abstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttrib</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'login_form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Username:'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'maxLength'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">64</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'required'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'filters'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'StringTrim'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'decorators'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFieldDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'errorMessages'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please enter your username'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Password:'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'maxLength'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">64</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'required'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'decorators'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFieldDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'errorMessages'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please enter your password'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'submit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'submit'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'ignore'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Login'</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above code needs little explanation.</p>
<p>Note that there is a call to <strong>$this->getFieldDecorators()</strong> in order to attach the proper decorators to the input fields.</p>
<p>Also, note that the <strong>required</strong> option is set to true, and an error message is supplied, should the user fail to enter either a username or password; this uses Zend_Validate_NotEmpty to check that the user has entered a value.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-login-page-the-login-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

