<?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>umop apisdn &#187; authentication</title>
	<atom:link href="http://pournaras.zilsen.com/tag/authentication/feed/" rel="self" type="application/rss+xml" />
	<link>http://pournaras.zilsen.com</link>
	<description>Thoughts, comments, guides and discoveries</description>
	<lastBuildDate>Thu, 17 May 2012 08:26:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Introduction to CakePHP authentication</title>
		<link>http://pournaras.zilsen.com/2009/03/29/cakephp-auth/</link>
		<comments>http://pournaras.zilsen.com/2009/03/29/cakephp-auth/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 07:19:22 +0000</pubDate>
		<dc:creator>Takis</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[cakePHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://pournaras.zilsen.com/?p=19</guid>
		<description><![CDATA[This is a tutorial which can be used by those who want to setup a basic user administration and authentication in CakePHP, one of the most popular MVC PHP frameworks around. We will be using the Auth component of Cake and we will create some administration screens. Setting up the database First of all we [...]]]></description>
			<content:encoded><![CDATA[<p>This is a tutorial which can be used by those who want to setup a basic user administration and authentication in <a  title="CakePHP homepage" href="http://cakephp.org">CakePHP</a>, one of the most popular <acronym title="Model-View-Controller">MVC</acronym> PHP frameworks around. We will be using the Auth component of Cake and we will create some administration screens.</p>
<h4>Setting up the database</h4>
<p>First of all we need one table to store our users. Cake&#8217;s Auth module can automate some of the process, as long as we are following some naming conventions. Our table consists of 4 fields: id, username, password and role:</p>
<pre class="brush: sql">
CREATE TABLE users (

  id INT(10) UNSIGNED NOT NULL auto_increment,
  username VARCHAR(20) NOT NULL,
  password VARCHAR(50) NOT NULL,
  role ENUM(&#039;user&#039;,&#039;admin&#039;) NOT NULL DEFAULT &#039;user&#039;,
  PRIMARY KEY  (`id`)

);
</pre>
<p>The &#8220;id&#8221; is the primary key of the table. By using this name, we are helping Cake to take care of possible Model associations without having to modify anything. The pair of fields &#8220;username&#8221; and &#8220;password&#8221; are expected by the Auth component (again, this is configurable). Finally, the &#8220;role&#8221; field will be used in order to distinguish basic users from administrators.</p>
<p>Careful readers will notice that the &#8220;password&#8221; field is rather large. This is because all passwords stored in the database are <a  title="Hash function" href="http://en.wikipedia.org/wiki/Hash_function">hashed</a> and, in the end, the field length will be much longer than the original (depending on the algorithm used).</p>
<h4>Modeling the table</h4>
<p>You can either bake the table into a Model or write the Model on your own. The basic Model looks like this:</p>
<pre class="brush: php">
/**
 *
 * Filename: /app/models/user.php
 *
**/
class User extends AppModel {

  var $name = &#039;User&#039;;

}
</pre>
<p>We are going to add some validation rules:</p>
<ul>
<li>the username must be non-empty and unique</li>
<li>the password must be non-empty</li>
<li>the role can either be &#8216;user&#8217; or &#8216;admin&#8217;</li>
</ul>
<p>Additionally, we would like to have some sort of password confirmation mechanism, which can be used either during the user registration or during the user edit. By implementing those rules, our code becomes:</p>
<pre class="brush: php">
/**
 *
 * Filename: /app/models/user.php
 *
**/
class User extends AppModel {
  var $name = &#039;User&#039;;

  var $validate = array(

    &#039;username&#039; =&gt; array(
      &#039;notEmpty&#039; =&gt; array(
        &#039;rule&#039; =&gt; &#039;notEmpty&#039;,
        &#039;message&#039; =&gt; &#039;The username cannot be empty&#039;
      ),
      &#039;isUnique&#039; =&gt; array(
        &#039;rule&#039; =&gt; &#039;isUnique&#039;,
        &#039;message&#039; =&gt; &#039;The username is already taken.&#039;
      )
    ),

    &#039;password&#039; =&gt; array(
      &#039;notEmpty&#039; =&gt; array(
        &#039;rule&#039; =&gt; &#039;notEmpty&#039;,
        &#039;message&#039; =&gt; &#039;The password cannot be empty&#039;
      ),
      &#039;confirmPassword&#039; =&gt; array(
        &#039;rule&#039; =&gt; array(&#039;confirmPassword&#039;, &#039;password_confirm&#039;),
        &#039;message&#039; =&gt; &#039;Please enter the same password twice&#039;
      )
    ),

    &#039;role&#039; =&gt; array(
      &#039;rule&#039; =&gt; array(&#039;inlist&#039;, array(&#039;user&#039;, &#039;admin&#039;)),
      &#039;message&#039; =&gt; &quot;A user&#039;s role must either be &#039;user&#039; or &#039;admin&#039;&quot;
      )
    );

  function confirmPassword( $original, $confirmationField )  {

    return $this-&gt;data[$this-&gt;name][$confirmationField] === $original[&#039;password&#039;];

  }

}
</pre>
<p>The password confirmation is implemented in lines 27-29. This part of the code tells Cake that we will be providing the Model with two different fields (&#8220;password&#8221; and &#8220;password_confirm&#8221;) and that we would like to apply the function named &#8220;confirmPassword&#8221; listed bellow (lines 39-43). Have a look at the relevant manual page (&#8220;<a  href="http://book.cakephp.org/view/152/Adding-your-own-Validation-Methods" title="CakePHP manual">Adding your own Validation Methods</a>&#8220;) to read how you can use your own functions for advanced field validation.</p>
<p>Go ahead ahead and play around with the code provided. Bake your own Controller and see if you can create some forms which will allow you to add new users and edit the existing ones.</p>
<p>~~~to be continued&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://pournaras.zilsen.com/2009/03/29/cakephp-auth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

