<?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 &#187; Zend_Log</title>
	<atom:link href="http://zf.gm-ram.com/topics/zend_log/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>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>Creating a Datestamped Log File using the Standard Log Resource Plugin</title>
		<link>http://zf.gm-ram.com/posts/creating-a-datestamped-log-file-using-the-standard-log-resource-plugin/</link>
		<comments>http://zf.gm-ram.com/posts/creating-a-datestamped-log-file-using-the-standard-log-resource-plugin/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 22:40:43 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Log]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=600</guid>
		<description><![CDATA[In a previous post, I looked at creating a custom resource plugin to create a logger. Since I wrote that code, a standard resource plugin to perform this function has been created. At first sight the standard plugin is inferior, in that the custom plugin allows for the insertion of a date into the log [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="/posts/bootstrapping-the-application-a-custom-log-resource-plugin/">previous post</a>, I looked at creating a custom resource plugin to create a logger. Since I wrote that code, a standard resource plugin to perform this function has been created. At first sight the standard plugin is inferior, in that the custom plugin allows for the insertion of a date into the log filename, which allows a new log to be created every day, while the standard plugin requires a fixed filename specified in the configuration file. However, I have worked out a simple mechanism for adding a date stamp to the filename, which I shall share in this article.<span id="more-600"></span></p>
<p>The key things is that it is possible to use constants in the configuration file. The first step is therefore to define a constant to represent the current date stamp in <em>htdocs/index.php</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Define current date</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DATESTAMP'</span><span style="color: #339933;">,</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This should come after the definitions of APPLICATION_PATH and APPLICATION_ENV (see <a href="/posts/bootstrapping-the-application-the-index-php-file/">the earlier post on index.php</a>).</p>
<p>This constant can now be used in the configuration file (<em>application/configs/application.ini</em>) to declare the log filename, along with the other necessary settings:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.log.stream.writerName <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Stream&quot;</span>
resources.log.stream.writerParams.stream <span style="color: #000066; font-weight:bold;">=</span> 
  APPLICATION_PATH <span style="color: #933;">&quot;/logs/log_&quot;</span> DATESTAMP
resources.log.stream.writerParams.mode <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;a&quot;</span></pre></div></div>

<p>To use the logger, you can fetch the object created like this from within the controller:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bootstrap</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getInvokeArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'bootstrap'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$log</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bootstrap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'log'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then use the logger in the normal fashion, e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><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: #0000ff;">'Page not found'</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/creating-a-datestamped-log-file-using-the-standard-log-resource-plugin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Bootstrapping the Application: a Custom Log Resource Plugin</title>
		<link>http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-log-resource-plugin/</link>
		<comments>http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-log-resource-plugin/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 08:57:27 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Log]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=182</guid>
		<description><![CDATA[So far, we have been looking at how to use several of the standard resource plugins that come with Zend_Application in order to initialise application resources. It is possible, indeed encouraged, to create your own custom resource plugins to handle the initialisation of resources that fall outside their scope. One very useful class is Zend_Log, [...]]]></description>
			<content:encoded><![CDATA[<p>So far, we have been looking at how to use several of the standard resource plugins that come with Zend_Application in order to initialise application resources. It is possible, indeed encouraged, to create your own custom resource plugins to handle the initialisation of resources that fall outside their scope.<span id="more-182"></span></p>
<p>One very useful class is Zend_Log, which provides a handy mechanism for logging application errors or user activity. I have created a custom resource plugin called <strong>Default_Resource_Log</strong>, which handles the initialisation of a Zend_Log, and storage of it in the registry for future retrieval.</p>
<p>To use custom resource plugins, you need to register a plugin prefix and associated directory. In the case of the sample application, the prefix is <strong>Default_Resource</strong> and the directory is <em>resources</em>; these are added to the configuration file like this to perform the registration:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">pluginPaths.Default_Resource <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> APPLICATION_PATH </span><span style="color: #933;">&quot;/resources&quot;</span></pre></div></div>

<p>The plugin itself is implemented in a file called <em>Log.php</em>, which is in the <em>resources</em> directory. Note that although the class is called Default_Resource_Log, the prefix is excluded when naming the file in which it is contained.</p>
<p>The basic class skeleton 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> Default_Resource_Log 
  <span style="color: #000000; font-weight: bold;">extends</span> Zend_Application_Resource_ResourceAbstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Add class methods and variables here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that Zend_Application_Resource_ResourceAbstract is the base class for resource plugins.</p>
<p>It has a member variable used to store the Zend_Log:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_log</span><span style="color: #339933;">;</span></pre></div></div>

<p>Zend_Application will call the <strong>init</strong> method of the class to retrieve the resource it initialises. The class uses the singleton pattern to implement this:</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> init<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><span style="color: #004000;">getLog</span><span style="color: #009900;">&#40;</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> getLog<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: #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: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOptions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'%d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$log</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Log<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Log_Writer_Stream<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend_Log'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</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;">=</span> <span style="color: #000088;">$log</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If Zend_Log has already been created, it returns the reference stored in <strong>$_log</strong>; otherwise, it creates the Zend_Log, stores a reference to it and returns that.</p>
<p>The reference to Zend_Log is also stored in the registry under the key Zend_Log; this allows it to be retrieved later by:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$log</span> <span style="color: #339933;">=</span> Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend_Log'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note the call to <strong>getOptions</strong>; this allows the resource plugin to retrieve any settings in the configuration file that apply to it. Here, the following line has been added to the configuration:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.log.file <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> APPLICATION_PATH </span><span style="color: #933;">&quot;/logs/application-%d.log&quot;</span></pre></div></div>

<p>This sets the location of the log file. Note that the resource plugin will look for the string <strong>%d</strong> in the name and replace it with the current date; this leads to a different log file being created every day.</p>
<p>Note that it is necessary to add at least one line relating to the resource plugin to the configuration file in order to have it initialised; if there were no parameters, you would just add:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.log<span style="">&#91;</span><span style="">&#93;</span> <span style="color: #000066; font-weight:bold;">=</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-log-resource-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

