<?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>Random Tech Articles &#187; ldap</title>
	<atom:link href="http://www.analogrithems.com/rant/category/ldap/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.analogrithems.com/rant</link>
	<description>If I have seen a little further it is by standing on the shoulders of Giants. - Newton</description>
	<lastBuildDate>Tue, 10 Aug 2010 20:51:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Find the Schema Path In LDAP</title>
		<link>http://www.analogrithems.com/rant/2010/03/29/find-the-schema-path-in-ldap/</link>
		<comments>http://www.analogrithems.com/rant/2010/03/29/find-the-schema-path-in-ldap/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 03:35:49 +0000</pubDate>
		<dc:creator>analogrithems</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[ldap]]></category>

		<guid isPermaLink="false">http://www.analogrithems.com/rant/?p=161</guid>
		<description><![CDATA[So I&#8217;ve been trying to find a good way to discover the schema path in LDAP that would work across Netscape style ldap servers as well as Active Directory.  See the problem is each one hides their schema path in a different place.  Netscap style servers (Redhat-DS, Project 389, iPlanet, Sun One, etc) use &#8216;cn=schema&#8217;, [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been trying to find a good way to discover the schema path in LDAP that would work across Netscape style ldap servers as well as Active Directory.  See the problem is each one hides their schema path in a different place.  Netscap style servers (Redhat-DS, Project 389, iPlanet, Sun One, etc) use &#8216;cn=schema&#8217;, while openldap has been known to use &#8216;cn=subschema&#8217; and Active Directory uses  <tt>'CN=Schema</tt>, <tt>CN= Configuration</tt>, <tt>DC=<em>forest_root_domain'</em></tt> this makes it really difficult to create a universal tool that can find any schema path.  In the past I would lift code from the PhpLDAPAdmin tool, but even they changed their methods.  Usually you get stuck using several case statements.  Recently I discovered a way to do a query to the Root DSE and have it just tell you where it has stored it&#8217;s schema data.   Use the <strong>subschemaSubentry </strong>to tell you exactly where the schema path is.</p>
<p>Here is an example of how to get it in PHP</p>
<p>$checkDN = @ldap_read($this-&gt;database, &#8221;, &#8216;objectClass=*&#8217;);<br />
$schemaEntry = ldap_get_entries($this-&gt;database, $checkDN);<br />
$SchemaDN = $schemaEntry[0]['subschemaSubentry'];</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.analogrithems.com/rant/2010/03/29/find-the-schema-path-in-ldap/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.analogrithems.com/rant/2010/03/29/find-the-schema-path-in-ldap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LdapAuth component for CakePHP</title>
		<link>http://www.analogrithems.com/rant/2009/06/13/ldapauth-component-for-cakephp/</link>
		<comments>http://www.analogrithems.com/rant/2009/06/13/ldapauth-component-for-cakephp/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 22:08:52 +0000</pubDate>
		<dc:creator>analogrithems</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[centralized authentication]]></category>

		<guid isPermaLink="false">http://www.analogrithems.com/rant/?p=40</guid>
		<description><![CDATA[So I was looking for a way to authenticate against LDAP with cake but I&#8217;ve found that it doesn&#8217;t support it by default. I found one that checks the auth against ldap then creates a local mysql account. This also didn&#8217;t use a actual ldap data sourc e either, it just handeled it&#8217;s own ldap [...]]]></description>
			<content:encoded><![CDATA[<p>So I was looking for a way to authenticate against LDAP with cake but I&#8217;ve found that it doesn&#8217;t support it by default.  I found one that checks the auth against ldap then creates a local mysql account.  This also didn&#8217;t use a actual ldap data sourc e either, it just handeled it&#8217;s own ldap connection.  After I read through the standard cakphp auth component I saw that it wouldn&#8217;t be that hard to write an LDAP based auth component.</p>
<p>First you need to download this file <a href="http://www.analogrithems.com/rant/wp-content/uploads/2009/06/ldap_auth.phps">ldap_auth.php</a> to your <strong>&#8216;app/controllers/components/&#8217;</strong> directory.</p>
<p>Then just like the original auth component you add that component to a model.  I followed the usual auth example and created a model called Users.php like so</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> User <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'User'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$useDbConfig</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ldap'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$primaryKey</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'uid'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$useTable</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ou=people'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If some of these options are odd looking please have a look at my <a href="http://www.analogrithems.com/rant/?p=3">ldap data source </a> post.  </p>
<p>Next you are going to need your controller.  Here is the one I used  It defines the required functions like login, logout &#038; authorize.</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> UsersController <span style="color: #000000; font-weight: bold;">extends</span> AppController <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Users'</span><span style="color: #339933;">;</span>    
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$components</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'RequestHandler'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'LdapAuth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Form'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Html'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Javascript'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Ajax'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">LdapAuth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logout</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>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> isAuthorized<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: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>7</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.analogrithems.com/rant/2009/06/13/ldapauth-component-for-cakephp/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.analogrithems.com/rant/2009/06/13/ldapauth-component-for-cakephp/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>CakePHP: LDAP with full CRUD, a living example!</title>
		<link>http://www.analogrithems.com/rant/2009/06/12/cakephp-with-full-crud-a-living-example/</link>
		<comments>http://www.analogrithems.com/rant/2009/06/12/cakephp-with-full-crud-a-living-example/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 22:14:35 +0000</pubDate>
		<dc:creator>analogrithems</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[ldap]]></category>

		<guid isPermaLink="false">http://www.analogrithems.com/rant/?p=13</guid>
		<description><![CDATA[I&#8217;ve been using CakePHP for a while now and I&#8217;ve been thinking for a while it was time to see if I could give something back. As an IT leader I&#8217;m in love with LDAP. It makes life so simple for me and my team. The big downside to LDAP is it&#8217;s not very easy [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using CakePHP for a while now and I&#8217;ve been thinking for a while it was time to see if I could give something back.  As an IT leader I&#8217;m in love with LDAP.  It makes life so simple for me and my team.  The big downside to LDAP is it&#8217;s not very easy to learn how all the objectClasses and attributes work with various applications.  Microsoft has eliminated this with the <em>Microsoft Management Console</em> (<em>MMC</em>).   It amazes me that no open source project has developed a tools such as this before.  I&#8217;ve worked on a open source tool in the past that was a web interface wrapper around ldap to do account management, so I&#8217;m familiar with the requirements such an application should have. Being that CakePHP is so powerful, I wanted to see if I could do this with that.</p>
<p>When I first started, I realized that CakePHP didn&#8217;t have an LDAP data source officially supported yet.  I did find two articles about some good attempts.  One by <a href="http://bakery.cakephp.org/articles/view/ldap-datasource-for-cakephp" target="_blank"><span>euphrate</span></a>, unfortunately this one was only for reading from ldap.  The second one was by <a href="http://memdump.wordpress.com/2008/04/26/ldap-data-source-now-with-full-crud/">Gservat</a>, this one was a bit more complete, but was not really working for me and  as i read from his comments many others.  I think we wrote his for CakePHP 1.1.  Since I wanted to use Current cake 1.2.8xxx  I set out to use this as my start and fix/extend it.</p>
<p>Before we get started I want to state the environment I was using to do my work was Redhat Enterprise 5.2 &amp; Fedora 10 (Work requirement) with redhat directory server 8.1 and Fedora directory server 1.2.  Now while LDAP is a standard protocol, some of the driver may have become centric to those platforms, so if this is the case, please leave me a comment and I will try to correct the ldap data source I&#8217;m working on.  My hope is to get ldap as an offical CakePHP data source.  With that said the reason i call this a living example is because I&#8217;ve continued to upgrade and improve this data source as well as this article.  Some of the next features I want to implement is data associations.  Basically has and belongs to many relations.  This way when you look up an user account it also shows you all the groups that user is in.  This will take some time but I&#8217;ll get there.  This work is all being done in the hopes that I can use this data source and CakePHP to build a really user friendly web interface for managing enterprise LDAP infrastructures without a whole lot of LDAP knowledge.</p>
<p><strong>4/15/2010 &#8211; Made several changes to the code to hopefully play nicely with active directory.  Tested it with CakePHP-1.3 everything looks good.  Please test and leave feedback.  The new datasource requires you to add a new field to the database.cfg ldap entry.  See the config below<br />
</strong></p>
<p><strong>8/20/2009 &#8211; New Home for the source.  I&#8217;ve got this datasource in my  github tree now <a href="http://github.com/analogrithems/idbroker/tree/master/models/datasources">http://github.com/analogrithems/idbroker/tree/master/models/datasources</a> enjoy, and feel free to submit bugs or request there.</strong></p>
<p><strong>7/13/2009 &#8211; updated ldap_source.php to make better use of the debug describe code.  Also fixed the way things update.  Only update what has changed instead of whole record.  This will help with LDAP aci rules when logging in as non-admin users and trying to do things like update your userpassword or email.</strong></p>
<p><strong>6/20/2009 &#8211; Updated ldap_source.php to work with OpenLDAP 2.3 schema system.  First it will try &#8216;cn=schema&#8217;, if that doesn&#8217;t return any results then it looks for schemas in &#8216;cn=subschema&#8217;  this make sure the code will work with OpenLDAP as well as the Netscape based versions like iPlanet, Redhat Directory Server, Fedora Directory Server etc.</strong></p>
<p>First things first, here is my ldap data source for CakePHP.  You will need to download this <a href="http://www.analogrithems.com/rant/wp-content/uploads/2009/07/ldap_source.phps" target="_blank">ldap_source.php</a> to your &#8216;app/models/datasources/&#8217; directory.</p>
<p>So lets dive right in below is the database config we will use.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> ldaps<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//hostname and 'port' =&amp;gt; 636</span>
        <span style="color: #666666; font-style: italic;">// If using tls set 'tls' =&amp;gt; true and 'port' =&amp;gt; 389</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$ldap</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'datasource'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'ldap'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'host'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'port'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">389</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'basedn'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'dc=examnple,dc=com'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'login'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'database'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'tls'</span>         <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Netscape'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//Available types are 'OpenLDAP', 'ActiveDirectory', 'Netscape'</span>
		<span style="color: #0000ff;">'version'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">3</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>You notice that the variables database, login and password are blank.  Keep at least database this way.  You can populate login and password if don&#8217;t want your ldap connections to be anonymous.  I keep mine blank because I have written my own auth component that uses ldap,   So once I&#8217;m authed that gets passed to the datasource instead.  This is a ugly hack that I&#8217;ve written another <a href="http://www.analogrithems.com/rant/2009/06/13/ldapauth-component-for-cakephp/">post about</a>.</p>
<p>Please note that if you are using Redhat directory server, iPlanet or Fedora Project 389 then the <strong>type</strong> is Netscape as these all evolved off the Netscape LDAP server code base.  The other available types are &#8216;OpenLDAP&#8217; or &#8216;ActiveDirectory&#8217;  (case matters).</p>
<p>Here is our people model for accessing the users in your LDAP tree.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'alphaNumeric'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'custom'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/^[a-zA-Z]*$/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'on'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Only Letters and Numbers can be used for Display Name.'</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'between'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'between'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'on'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Between 5 to 15 characters'</span>
			<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'sn'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'custom'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/^[a-zA-Z]*$/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'on'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Only Letters and Numbers can be used for Last Name.'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'userpassword'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'minLength'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Mimimum 8 characters long.'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'email'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'on'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Must Contain a Valid Email Address.'</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'uid'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'alphaNumeric'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'on'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Only Letters and Numbers can be used for Username.'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>Here is a very basic controller to accompany our people model.  It demonstrates the important core functions and should get you started on using this data source with your own application.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">data<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;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'objectclass'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'top'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'organizationalperson'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'inetorgperson'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'person'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'posixaccount'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'shadowaccount'</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;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password_confirm'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userpassword'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password_confirm'</span><span style="color: #009900;">&#93;</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: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'homedirectory'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uid'</span><span style="color: #009900;">&#93;</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;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'homedirectory'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/home/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uid'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>save<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<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;">-&amp;</span>gt<span style="color: #339933;">;</span>Session<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setFlash<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'People Was Successfully Created.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>id<span style="color: #339933;">;</span>
					<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>redirect<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'id'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$id</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: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Session<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setFlash<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;People couldn't be created.&quot;</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: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Session<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setFlash<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Passwords don't match.&quot;</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: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>layout <span style="color: #339933;">=</span> <span style="color: #0000ff;">'people'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> view<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$id</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: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>primaryKey<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$people</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>find<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'first'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$filter</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;">-&amp;</span>gt<span style="color: #339933;">;</span>set<span style="color: #009900;">&#40;</span><span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'people'</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: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>layout <span style="color: #339933;">=</span> <span style="color: #0000ff;">'people'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>id <span style="color: #339933;">=</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>del<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>So lets talk about somethings here,  in our model we define <strong>$primaryKey</strong> &amp; <strong>$useTable</strong> variables.  The <strong>$useTable</strong> is the branch of the ldap server.  For this models purpose we define our table as <strong>&#8216;ou=people&#8217;</strong>.  This makes sure that objects we create (I.E. Users/people)  will be added under the organization unit people.  It also makes sure that when you pass something like &#8216;jdoe&#8217; to the delete action it will search that branch for the user object to delete.  The <strong>$primaryKey</strong> also helps in the creation and deleting of users.  It makes sure that the dn is created as uid, this is helpful to make sure that a user doesn&#8217;t already have that user name.  Also since ldap is case insensitive you don&#8217;t have to worry about the possible variations of the object names when checking the existence.</p>
<p>Now your model isn&#8217;t limited to one branch or object type.  If you wanted to create a browser for example your could define a model like the following.</p>
<p>You&#8217;ll notice here we set our <strong>$useTable</strong> to nothing (important, you get errors about no db defined from CakePHP if this missing).  The really interesting part here is that we set <strong>$primaryKey</strong> to dn.  This is the ultimate primary key for our type or data source.  The difference here is that when we create/delete an object we have to pass it the full dn.</p>
<p>Our new data source also adds some new options to the find function.<br />
<strong>$options['targetDN'] :</strong> This is more like the point in the tree we want to start our search.  If you don&#8217;t define it it defaults to the $useTable.$config[$useDbConfig]['basedn']  if your $useTable variable is empty it defaults to the basedn configured in your database config.</p>
<p><strong>$options['scope'] :</strong> If you&#8217;ve worked with ldap before then you are familiar with the concept of search scopes.  You have three search scopes, &#8216;sub&#8217;, &#8216;one, &amp; &#8216;base&#8217;.  Basically <strong>sub</strong> means search from this point down the tree.  <strong>one</strong> means search one level below this point and <strong>base</strong> means search just this point.  For example if you wanted to see if a user already existed you could set the targetDn to uid=jdoe,ou=people,dc=example,dc=com and it will check if this object already exists.   The default scope is <strong>sub</strong></p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.analogrithems.com/rant/2009/06/12/cakephp-with-full-crud-a-living-example/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.analogrithems.com/rant/2009/06/12/cakephp-with-full-crud-a-living-example/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
	</channel>
</rss>
