<?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>Internet Strategy Guide &#187; theory crafting</title>
	<atom:link href="http://phpprotip.com/category/php/theory-crafting/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpprotip.com</link>
	<description>Together we can defeat the internet</description>
	<lastBuildDate>Sat, 11 Feb 2012 01:39:47 +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>Auth/ACL implementation strategies</title>
		<link>http://phpprotip.com/2010/08/authacl-implementation-strategies/</link>
		<comments>http://phpprotip.com/2010/08/authacl-implementation-strategies/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 18:52:21 +0000</pubDate>
		<dc:creator>chance</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[theory crafting]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[Access control list]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[Authentication]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://phpprotip.com/?p=384</guid>
		<description><![CDATA[I'm going to talk more about ACLs than Auth. Auth is simple, it's the ACL that will trip you up.  Since both concepts are coupled together when you're making a login system, I feel it's appropriate to at least touch on Auth. What I want to cover is the ways we can create the ACL [...]]]></description>
			<content:encoded><![CDATA[<p>I'm going to talk more about <a class=\"zem_slink freebase/en/access_control_list\" title=\"Access control list\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9BY2Nlc3NfY29udHJvbF9saXN0">ACLs</a> than Auth. Auth is simple, it's the ACL that will trip you up.  Since both concepts are coupled together when you're making a login system, I feel it's appropriate to at least touch on Auth. What I want to cover is the ways we can create the ACL object to suit needs based on the scale of the project. I'm going to assume that readers have a passing familiarity with using the Auth and Acl objects and may have even implemented them into projects.</p>
<h2><span id="more-384"></span>Zend_Auth</h2>
<p>The reason I say Auth is simple is because <a class=\"zem_slink freebase/en/zend_framework\" title=\"Zend Framework\" rel=\"homepage\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2ZyYW1ld29yay56ZW5kLmNvbS8=">Zend Framework</a> makes it simple with their <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2ZyYW1ld29yay56ZW5kLmNvbS9tYW51YWwvZW4vemVuZC5hdXRoLmh0bWw=">Zend_Auth</a> class. You can pick an auth style, implement and then go from there. For the purpose of this discussion, I'll be talking using Database authentication. So after one has set up a login page that uses Zend_Auth (there are already a lot of articles that cover this so i'll move on), the major bear you have to tackle is using the Zend_Auth_Result to determine proper access. Sounds simple, that's what Zend_Acl is for.</p>
<h2>Zend_Acl</h2>
<p>So how do we create our Acl? Again, ZF's reference guide gives us some handy-dandy examples of using their object but how you utilize the object depends on your scale. Other articles give you a way to bind the two together, usually via a controller plugin of some sort. The thing that can be tricky is how you want the Acl and Auth to interact with each other within that plugin. I utilize a controller plugin that fires off an Auth/Acl check in the dispatchLoopStartup() method. If no Zend_Auth_Result object exists, it assigns a guest role and if one does exist, it searches the Zend_Auth_Storage for a role value (assigning guest if one doesn't exist somehow) and checks that value against the Acl that was created in my Bootstrap. My biggest conundrum has always been translating Requests into Resources. I'll talk more on that later.</p>
<h2>Scaling</h2>
<p>I tend to put my scaling into one of the following categories: small,decent or ZOMG. Small is something like a personal site or a proof-of-concept/self-tutorial project where my users will probably be a 1-5 roles and 1-5 resources at best. Decent is most collaborative niche projects with 50-100 resources. ZOMG is where the number of resources I'm dealing with is &gt;100, unknown or potentially large. I define my ACLs in my Bootstrap in one of three ways:</p>
<ol>
<li><a class=\"zem_slink freebase/en/hard_code\" title=\"Hard coding\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9IYXJkX2NvZGluZw==">Hardcoded</a></li>
<li>As part of my Navigation Object properties</li>
<li>Pulled from a <a class=\"zem_slink freebase/en/database\" title=\"Database\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9EYXRhYmFzZQ==">database</a></li>
</ol>
<h3>Hardcoded</h3>
<p>This one is easy to implement since if you follow the ZF reference or the <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy56aW11ZWwuaXQvYmxvZy8/cD04Ng==">numerous</a> guides/posts you find when you <a class=\"zem_slink freebase/en/google\" title=\"Google\" rel=\"homepage\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2dvb2dsZS5jb20=">google</a> for auth/acl systems, you'll be able to hardcode your ACL. I find this ideal for small projects where the number of resources is relatively low and the growth of adding additional resources is non-existent. This is the simplest way to do things.</p>
<h3>Navigation-based</h3>
<p>This method makes use of the Zend_Navigation object. <a class=\"zem_slink\" title=\"Brandon Savage\" rel=\"blog\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5icmFuZG9uc2F2YWdlLm5ldC8=">Brandon Savage</a> wrote up a nice <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5icmFuZG9uc2F2YWdlLm5ldC9jb250cm9sbGluZy1hY2Nlc3MtemVuZF9uYXZpZ2F0aW9uLWFuZC16ZW5kX2FjbC8=">guide to integrating ACLs into you r Navigation</a>. I usually instantiate my Navigation objects in the Bootstrap from a <a class=\"zem_slink freebase/en/extensible_markup_language\" title=\"XML\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9YTUw=">XML</a> file. The only tricky thing I found with this is finding the appropriate Navigation_Page (that tells us our resource/permissions) from the Request object. I tend to utilize this method when the growth of resources occurs at a slow pace.</p>
<h3>Database-driven</h3>
<p>This particular method is what spurred me to write about Auth and Acl. I see this solution as ideal for a high amount of resources or where the growth of resources to have fast spurts.  This solution can allow for ease of maintenance and is <a class=\"zem_slink freebase/en/scalability\" title=\"Scalability\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9TY2FsYWJpbGl0eQ==">scalable</a>. Even though it is a scalable solution, I kind of see it as overkill for smaller, low-growth projects.</p>
<h2>Multiple project ACLs</h2>
<p>Since I've began working with Zend Framework, I have used it to create both internal apps in addition to running my company's main site. To date, I have a total of eleven internal applications, some of which require Auth/Acl and some of which do not. Those that do not require them will soon be requiring them due to future features we wish to implement for them. At first I was working on a <a class=\"zem_slink freebase/en/drag-and-drop\" title=\"Drag-and-drop\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9EcmFnLWFuZC1kcm9w">drag and drop</a> solution for setting up an Auth/ACL system for any given project. I was going to work it a stand-alone module where all the developer has to do is call the Module Bootstrap within the Application Bootstrap. The drag and drop solution was going to rely on the project's Navigation object for determining the ACL of a requested page. Then it occurred to me that even if I could make this 'simple' drag and drop solution, we'd be faced with reapplying it to ten more projects and have subsequent setup processes for each additional project. This seemed highly inefficient and a pain in my ass.</p>
<h3>Control Panel</h3>
<p>My next realization was that there was multiple overlap on the users accessing various internal applications and way too many vhosts to make for each project. It seemed like it would be better to do the ultimate refactor and create a centralized control panel for people to authenticate and access internal applications.</p>
<p>I was still hung up on wanting to reuse my Navigation based code when I figured out that maintaining the navigation XML would be another nightmare. I had attempted a database solution for one project and it became a pain to maintain the ACL when I introduced Routing to the project. It was a bitter and hateful experience to get it working and since both the Auth/ACL and Routing parts of the project were done before I had embraced <a class=\"zem_slink freebase/en/unit_test\" title=\"Unit testing\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9Vbml0X3Rlc3Rpbmc=">unit testing</a>, sorting through my crazy <a class=\"zem_slink freebase/en/monkey-patch\" title=\"Monkey patch\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9Nb25rZXlfcGF0Y2g=">monkey patching</a> logic would only intensify my rage. I also recalled that the biggest issue with that proto-solution was translating the Request into a Resource.</p>
<p><a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3BocHByb3RpcC5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTAvMDgvZGFzaGJvYXJkX3RhYmxlcy5wbmc="><img class="alignright size-medium wp-image-442" title="jank_acl_schema" src="http://phpprotip.com/wp-content/uploads/2010/08/dashboard_tables-300x176.png" alt="" width="300" height="176" /></a>In the schema shown on the right, the Resource.Name corresponded to the Request's Controller and Privilege.Name corresponded to Request's Action. When the ACL was created in the Bootstrap, it would create Module.Name_Resource.Name as the resource then associate privileges to that resource. Routing and Controller forwarding made this whole thing hard to mentally track. I ended up having a lot of monkey patch entries to make it work in the end.</p>
<p>So here I was, just generally damned when I thought up a way around the issues I had with the previous schema. I would need to have the Resource.Name independent of the Request <a class=\"zem_slink freebase/en/uniform_resource_identifier\" title=\"Uniform Resource Identifier\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9Vbmlmb3JtX1Jlc291cmNlX0lkZW50aWZpZXI=">URI</a> in order to avoid routing issues. By associating the Resource to a URI, I don't have to account for if Routing exists or not. The URI will always be available and the core ACL system will be independent of any Routes that do/don't/might exist. Privileges bound to action seemed like a lot of overkill since all privileges come down to <a class=\"zem_slink freebase/en/crud\" title=\"Create, read, update and delete\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9DcmVhdGUlMkNfcmVhZCUyQ191cGRhdGVfYW5kX2RlbGV0ZQ==">CRUD</a> (though I always like the BREAD acronym better). In addition, privileges would be off in their own world and reused when necessary (the last schema had too many redundant privileges associated to different resources).</p>
<p><a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3BocHByb3RpcC5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTAvMDgvY29udHJvbF9wYW5lbF9zY2hlbWEucG5n"><img class="alignright size-medium wp-image-440" title="control_panel_schema" src="http://phpprotip.com/wp-content/uploads/2010/08/control_panel_schema-272x300.png" alt="" width="272" height="300" /></a>As I stated earlier, the goal is a unified login for internal apps (there are currently eleven and this number will grow). The user table consists of a username, password and control panel role. This role allows for future interfaces to be built that can manage the control panel such as adding a project or adding users. If you're wondering why I have the password column as able to be null, it's to support some legacy items. Next major table is the project table which lets me have a project_user table  that is also has a role associated with it. I made role a separate table since a lot of the projects overlap on roles such as guest and admin. Resources can be specifically named or generalized as necessary. All my links (URIs) can share a resource name if necessary. Finally there is the privilege table along with a resource_privilege table that allows me to maintain those relationships. While I have a separate control_panel_role, I decided that the control panel resource/privilege system doesn't need to be separate. I only wanted to make sure that the control panel system was accessible even if there was no projects available for selection. I'm planning on making the past projects into modules under this system and all future internal apps accessible/controlled from here. I lack any co-workers that I'm able to debate the merits of this idea with and given that this will be a major refactor of my work thus far, I am asking for feedback on the idea. Am I missing any other viable ways of defining the ACL that would be easier/better? I'm not a DBA so I'm wondering if my tables are actually optimized well enough. Any other issues or thoughts I haven't considered?</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class=\"zemanta-pixie-a\" title=\"Enhanced by Zemanta\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy56ZW1hbnRhLmNvbS8="><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_c.png?x-id=c137486e-8012-4ac3-8089-0392bb540ad6" alt="Enhanced by Zemanta" /></a><span class="zem-script more-related more-info pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
 <img src="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=384" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://phpprotip.com/2010/08/authacl-implementation-strategies/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>visibility and inheritance.</title>
		<link>http://phpprotip.com/2009/08/visibility-and-inheritance/</link>
		<comments>http://phpprotip.com/2009/08/visibility-and-inheritance/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 15:16:40 +0000</pubDate>
		<dc:creator>chance</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[theory crafting]]></category>
		<category><![CDATA[unittesting]]></category>
		<category><![CDATA[web dev]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[visibility]]></category>

		<guid isPermaLink="false">http://phpprotip.com/?p=164</guid>
		<description><![CDATA[An interesting topic came up in #phpc today. It revolved around some issues I've been encountering in my latest code designs/structures. It also leads into some side topics that I will attempt to explore. From my point of view, the discussion centered around what is the best 'default' visibility to use for methods. Another thing [...]]]></description>
			<content:encoded><![CDATA[<p>An interesting topic came up in #phpc today. It revolved around some issues I've been encountering in my latest code designs/structures. It also leads into some side topics that I will attempt to explore.</p>
<p>From my point of view, the discussion centered around what is the best 'default' visibility to use for methods. Another thing touched upon is the <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9PcGVuL2Nsb3NlZF9wcmluY2lwbGU=">Open/Closed principle</a>, which I think I subscribe to or may subscribe to(this depends on my ability to determine what half of the words in the entry mean).</p>
<p>Out of the whole discussion, here is the points I got (aka understood) out of it. Please correct me in the comments if I'm off base in any way.</p>
<p><strong>Methods should only be public when necessary. This is to help reduce the amount of side-effects that can occur because of method overrides.</strong></p>
<p>K, I can accept that and in thinking about my past code, I use to use protected more than private. I used public very sparingly.<br />
Now I find, since I started unit testing, that I have a large amount of public functions than I've had in the past. That is because I can't figure out how to test private methods. One way that I can think of is to create public methods that allow you to test the private ones. Unfortunately, this makes me wonder why the method isn't public to begin with since it seems redundant and wasteful to have these public methods to access private methods.<br />
Please note the key word methods, property accessors are a different story.<br />
The best solution (I can think of) to testing private methods is Mock Objects. Unfortunately, even though I've started to use mocks/doubles more, I'm unsure if my implementation is correct. Until I'm confident in my understanding of mocks/doubles, I worry about having false positives in test results.</p>
<p><strong>Another assertion that was made in the discussion was that private methods allow you to preserve the class' core functionality.</strong><br />
Unfortunately, no matter what the visibility of the method is, you're able to override it (and potentially mess with the core functionality you were trying to preserve).<br />
Example:</p>
<pre>
class foo{
    private function foobar() {
        echo "foo\n";
        return "foo foo\n";
    }

    public function bar() {
        echo "w00t ";
        return $this->foobar();
    }
}

class bar extends foo{
    private function foobar() {
        return "bar\n";
    }

    public function baz() {
        echo $this->bar();
    }

    public function wut() {
        echo $this->foobar();
    }
}

$f=new bar();
$f->baz();
$f->wut();
</pre>
<p>By running the above code, you get:</p>
<pre>
w00t foo
foo foo
bar
</pre>
<p>If preservation of core functionality is your main concern, then you're better off using <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzMi5waHAubmV0L21hbnVhbC9lbi9sYW5ndWFnZS5vb3A1LmZpbmFsLnBocA==">final</a>.</p>
<p>So after all that rambling, you're probably wondering what I'm trying to get to. It still comes down to visibility's effect on inheritance. The way I see it, unless you declare the method as final, you can't lock down the parent functionality because private methods can still be overridden. Trying to figure out what level of visibility for a method is a situational call. There is no correct 'default' visibility. Sure private is safer because of <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2h0dHA6Ly9lbi53aWtpcGVkaWEub3JnL3dpa2kvUHJpbmNpcGxlX29mX2xlYXN0X3ByaXZpbGVnZQ==">least privilege</a> but it makes testing a bear (or at least a bear at my current skill level in testing). Public potentially opens you up for abuse or misuse.</p>
<p>What I would still like to know is, how do you do class method visibility? How does that affect your testing methodology? Can someone give me an example (that isn't a singleton) where private is a better choice over protected? I like protected because it seems less limiting to me and my current coding style appreciates that degree of flexibility.</p>
 <img src="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=164" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://phpprotip.com/2009/08/visibility-and-inheritance/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

