<?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>Jeff Combs - IT Professional / Web Developer &#187; CakePHP</title>
	<atom:link href="http://jcombs.net/category/cakephp/feed" rel="self" type="application/rss+xml" />
	<link>http://jcombs.net</link>
	<description>System Administration</description>
	<lastBuildDate>Sat, 24 Dec 2011 17:56:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Password Hashing in CakePHP</title>
		<link>http://jcombs.net/cakephp/password-hashing-in-cakephp</link>
		<comments>http://jcombs.net/cakephp/password-hashing-in-cakephp#comments</comments>
		<pubDate>Wed, 27 Jul 2011 14:12:31 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://jcombs.net/?p=196</guid>
		<description><![CDATA[In a recent project I needed to add some validation to my User model. I&#8217;m using the Auth Component that is included with CakePHP. In this particular instance I wanted to allow a change password form. The form would have three fields (current_password, new_password, confirm_password). In the Model I wanted to first check if the [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project I needed to add some validation to my User model.  I&#8217;m using the Auth Component that is included with CakePHP.  In this particular instance I wanted to allow a change password form.  The form would have three fields (current_password, new_password, confirm_password).  In the Model I wanted to first check if the current password was entered correctly.  </p>
<p>Validation should always happen in the model so I created a new function that would check for the current password for the logged in user.  The Auth component automatically hashes the password with SHA1 and uses the Security Salt as part of the password string to create the hash, so I needed to hash the &#8220;current_password&#8221; field from the form to check for a match.  This is where I ran into the problem.  I tried using the following:</p>
<pre class="brush: php">
function checkCurrentPassword($data) {
    $id = $this-&gt;data[$this-&gt;alias][&#039;id&#039;]; // passed the user ID from the form as a hidden field
    $pwd = $this-&gt;field(&#039;password&#039;, array(&#039;id&#039; =&gt; $id)); // get the current password from the database
    if(Security::hash($data[&#039;current_password&#039;]) != $pwd) {
        return false;
    }
    return true;
}
</pre>
<p>You can see that $id is passed from the form and $pwd is a variable for the current password in the database.  Auth will automatically hash an input with the name &#8220;password&#8221;, but my form is using &#8220;current_password&#8221;, so it is sent in cleartext.  This needs to be hashed first.  I attempted to use the Security::hash function but my validated kept failing.  </p>
<p>As it turns out the Security::hash function is only using SHA1 without the Security Salt added.  What I was able to do is use the AuthComponent::password function instead which does use the Security Salt configured in core.php.  New code looks like:</p>
<pre class="brush: php">
function checkCurrentPassword($data) {
    $id = $this-&gt;data[$this-&gt;alias][&#039;id&#039;];
    $pwd = $this-&gt;field(&#039;password&#039;, array(&#039;id&#039; =&gt; $id));
    if(AuthComponent::password($data[&#039;current_password&#039;]) != $pwd) {
        return false;
    }
    return true;
}
</pre>
<p>The validate array would look like this:</p>
<pre class="brush: php">
var $validate = array(
&#039;current_password&#039; =&gt; array(
    &#039;rule&#039; =&gt; &#039;checkCurrentPassword&#039;,
    &#039;message&#039; =&gt; &#039;Current password was not entered correctly&#039;
    )
);
</pre>
<p>Update:  Security::hash actually takes a third parameter documented in the API to use the Security.salt value</p>
<blockquote><p>
Create a hash from string using given method. Fallback on next available method.</p>
<p>Parameters:</p>
<p>        string $string required</p>
<p>        String to hash<br />
        string $type optional NULL</p>
<p>        Method to use (sha1/sha256/md5)<br />
        boolean $salt optional false</p>
<p>        If true, automatically appends the application&#8217;s salt value to $string (Security.salt)
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://jcombs.net/cakephp/password-hashing-in-cakephp/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cakephp form input using select with options</title>
		<link>http://jcombs.net/cakephp/cakephp-form-input-using-select-with-options</link>
		<comments>http://jcombs.net/cakephp/cakephp-form-input-using-select-with-options#comments</comments>
		<pubDate>Sun, 17 Jul 2011 16:58:05 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://jcombs.net/?p=189</guid>
		<description><![CDATA[There are times that you need to control the form->input helper with specific type setting and additional options. This is probably more comment when building custom form inputs that aren&#8217;t automagically being set from the model/controller. I recently had to build a list on distinct dates, but prefer to user $this->Form->input, rather than $this->Form->select. I [...]]]></description>
			<content:encoded><![CDATA[<p>There are times that you need to control the form->input helper with specific type setting and additional options.  This is probably more comment when building custom form inputs that aren&#8217;t automagically being set from the model/controller.  </p>
<p>I recently had to build a list on distinct dates, but prefer to user $this->Form->input, rather than $this->Form->select.  I first built an indexed array containing my date fields.</p>
<blockquote><p>
array(<br />
  [xxxx-xx-xx] => xxxx-xx-xx<br />
  [yyyy-yy-yy] => yyyy-yy-yy<br />
)
</p></blockquote>
<p>We&#8217;ll call this array $options.  You can then build the input using something like</p>
<pre class="brush: php">
$this-&gt;form-&gt;input(&#039;Input Name&#039;, array(
  &#039;type&#039; =&gt; &#039;select&#039;,
  &#039;options&#039; =&gt; &#039;options&#039;,
  &#039;label&#039; =&gt; &#039;label&#039;,
  &#039;empty&#039; =&gt; &#039;No data selected&#039;
);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://jcombs.net/cakephp/cakephp-form-input-using-select-with-options/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP 1.3 Virtual Fields</title>
		<link>http://jcombs.net/cakephp/cakephp-1-3-virtual-fields</link>
		<comments>http://jcombs.net/cakephp/cakephp-1-3-virtual-fields#comments</comments>
		<pubDate>Fri, 01 Jul 2011 00:12:12 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://jcombs.net/?p=184</guid>
		<description><![CDATA[A new feature of CakePHP 1.3 is to create VirtualFields. Why do you care? Typically Cake can use the $displayField variable of a model for drop down lists in your views. Suppose a User model contains two columns, firstname and lastname. There is no way to concatenate this with the $displayField variable. You can however, [...]]]></description>
			<content:encoded><![CDATA[<p>A new feature of CakePHP 1.3 is to create VirtualFields. Why do you care?  Typically Cake can use the $displayField variable of a model for drop down lists in your views.  Suppose a User model contains two columns, firstname and lastname.  There is no way to concatenate this with the $displayField variable.  You can however, use $virtualFields to create a new variable that can be assigned to $displayField.</p>
<p>My example uses an Employee model.</p>
<pre class="brush: php">
class Employee extends AppModel {
	var $name = &#039;Employee&#039;;
        var $virtualFields = array(&#039;full_name&#039; =&gt; &#039;CONCAT(Employee.firstname, &quot; &quot;, Employee.lastname)&#039;);
        var $displayField = &#039;full_name&#039;;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://jcombs.net/cakephp/cakephp-1-3-virtual-fields/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP Auth Component Problem</title>
		<link>http://jcombs.net/cakephp/cakephp-auth-component</link>
		<comments>http://jcombs.net/cakephp/cakephp-auth-component#comments</comments>
		<pubDate>Thu, 15 Oct 2009 05:34:44 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://jcombs.net/?p=6</guid>
		<description><![CDATA[I&#8217;ve been using CakePHP for my last few projects and recently ran into a problem that was driving me nuts. I have a few pages that don&#8217;t require any authentication. You can allow pages to be viewed by calling $this->Auth->allow(&#8216;function_name&#8217;) in your beforeFilter() method. So, I set up my app_controller class with a before filter [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://jcombs.net/wp-content/uploads/2009/10/cakephp.jpg" alt="cakephp" title="cakephp" width="240" height="229" class="alignright size-full wp-image-91" /></p>
<p>I&#8217;ve been using CakePHP for my last few projects and recently ran into a problem that was driving me nuts.  I have a few pages that don&#8217;t require any authentication.  You can allow pages to be viewed by calling $this->Auth->allow(&#8216;function_name&#8217;) in your beforeFilter() method.  So, I set up my app_controller class with a before filter that looks something like this.</p>
<pre class="brush: php">
&lt;?php

class AppController extends Controller {

    var $helpers = array(&#039;Html&#039;, &#039;Form&#039;, &#039;Javascript&#039;);
    var $components = array(&#039;Auth&#039;);

    function beforeFilter() {
        $this-&gt;Auth-&gt;autoRedirect = false;
        $this-&gt;Auth-&gt;loginAction = array(&#039;controller&#039; =&gt; &#039;users&#039;, &#039;action&#039; =&gt; &#039;login&#039;);
        $this-&gt;Auth-&gt;allow(&#039;display&#039;);
    }

}

?&gt;
</pre>
<p><span id="more-6"></span></p>
<p>&#8216;Display&#8217; is a method in the pages controller, so all static pages can be shown without authentication.  This works fine, on to my contact controller which is also allowed to be accessed without authentication.  I set up my contact controller and wanted to allow the index method to be displayed.  In the contact controller you call the parent beforeFilter() and then allow any additional methods like so:</p>
<pre class="brush: php">
function beforeFilter() {
        parent::beforeFilter();
        $this-&gt;Auth-&gt;allow(&#039;index&#039;);
}
</pre>
<p>But for some reason, this kept sending me to the login form to authenticate.  After banging my head on the wall I soon remembered my view was calling an element that used a method from another controller.  Long story short, Auth was not allowing this method without authentication.  Unfortunately, even with debug mode turned on, there was no clue to lead me in the right direction.</p>
<p>So, if you run into a similar problem, make sure you allow any related controller functions to run without authentication if you are using elements and requestActions. </p>
]]></content:encoded>
			<wfw:commentRss>http://jcombs.net/cakephp/cakephp-auth-component/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

