<?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>HirdWeb &#187; ACL</title>
	<atom:link href="http://www.hirdweb.com/tag/acl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hirdweb.com</link>
	<description>Another Blog clogging up the already crowded internet</description>
	<lastBuildDate>Wed, 18 Jan 2012 20:54:49 +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>Central ACL Check</title>
		<link>http://www.hirdweb.com/2008/11/17/central-acl-check/</link>
		<comments>http://www.hirdweb.com/2008/11/17/central-acl-check/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 12:33:31 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[cakePHP]]></category>

		<guid isPermaLink="false">http://www.hirdweb.com/?p=142</guid>
		<description><![CDATA[With checking ACL&#8217;s, the code I use is as follows: $info = $this->Member->read(null, $id); // Check for permissions to edit this account if ( !$this->Acl->check(array('model' => 'Member', 'foreign_key' => $this->Auth->user('member_id')), $info['Member']['username'], 'update') ) { $this->Session->setFlash(__('You are not allowed to edit this user. -- ' . $this->Auth->user('member_id'), true)); $this->redirect(array('action'=>'index')); } While this works and is not [...]]]></description>
			<content:encoded><![CDATA[<p>With checking ACL&#8217;s, the code I use is as follows:</p>
<pre>
$info = $this->Member->read(null, $id);

// Check for permissions to edit this account
if ( !$this->Acl->check(array('model' => 'Member', 'foreign_key' => $this->Auth->user('member_id')), $info['Member']['username'], 'update') ) {
	$this->Session->setFlash(__('You are not allowed to edit this user. -- ' . $this->Auth->user('member_id'), true));
	$this->redirect(array('action'=>'index'));
}
</pre>
<p>While this works and is not that bad of an idea at all, there is a way to centralize this check and put it in the main app_controller.php file.<br />
<span id="more-142"></span></p>
<p>Depending on the scope of the project, or the business needs of the application, this check can be done and centralized so that there is not the need to put the ACL check in its form above in every action in every controller that needs to have this done. So for the Member controller, you may need to copy/paste the code above to check the ACL for permissions to view, edit, delete, add, etc. That is just one controller, what if there were others as well, like Post, Event, Pictures, etc. That is going to be a lot of code to write to check the ACL. </p>
<p>Now even though the <a href='http://book.cakephp.org/view/471/Checking-Permissions-The-ACL-Component' target='_new' title='5.1.3.4 Checking Permissions: The ACL Component'>Cookbook on checking ACLs</a> shows a simple one line approach:</p>
<pre>
$this->Acl->check(array('model' => 'User', 'foreign_key' => 2356), 'Weapons');
</pre>
<p>it is not just a simple one line approach. There are other things to consider, like what do you want to do if it fails. Is there a specific error message, does it need to redirect to a certain page, and what about if the person needs to be logged in and not need any special ACL permissions.This can get to be a few lines in length, and have a few more things that need to be done. This would be per function/action in the controller. </p>
<p>What I have done, is put the check in the app_controller. This way I need to check it with one line of code, instead of a few. I know, this may seem trivial, but this is something that could get really drawn out in heavier applications. </p>
<p>In the app_controller.php file, I created two functions:<br />
_enforceAccess()<br />
_checkAccess()</p>
<p>First I will explain the _enforceAccess() function. Since I use the model method of ACL, the function is looking for the following:<br />
model<br />
user&#8217;s ID<br />
Object being acted on<br />
Action for the object<br />
Redirect Page</p>
<p>I also do a check for logged in status as well, to make sure that if an action is needed after login, like viewing, then I can check that here as well, instead of per function in the controller. I set all of the parameters to null, in case there is no needed permission check and I need to check logged in status. </p>
<p>So the way this function looks after everything is added:</p>
<pre>
function _enforceAccess($model = null, $id = null,$object = null, $action = null, $redirect = array("/")) {
	if (!$this->loggedin) {
		$this->Session->setFlash('You do not have permissions to carry out the requested action.');
    	$this->redirect('/members/login');
    	exit();
    }
	if ($this->_checkAccess($model,$id, $object, $action)) {
		return true;
	}
	$this->Session->setFlash('You do not have permissions to carry out the requested action.');
	$this->redirect($redirect);
	exit();
}
</pre>
<p>This function first checks for logged in status, if the person is logged in, then I go to the next step, if not, then I would redirect to the login page to get the user to log in to perform this action. As a side note to this: <i>If you are using the Auth component, after the person logs in, the Auth component will redirect back to the prior page where this check was made.</i> </p>
<p>After checking logged in status, it makes a call to the _checkAccess() function and passes the parameters needed to check the ACL. If everything works out ok, and the person has the permission to do the requested action on the object, then the code sends it back to the controller to do any other scripts. The person is authorized, and so it lets them through. </p>
<p>If the person does not have the permission, then they are redirected to the page that is set as they redirect. If no page is given, then the default is just the top level page, or home page of the application. If you specify a redirect page. Either way, the error message is printed out for the end user. </p>
<p>In the _checkAccess() function, it requires the parameters to check the ACL:<br />
$model<br />
$id<br />
$object<br />
$action<br />
This function does the straightforward work. </p>
<pre>
function _checkAccess($model = null,$id = null, $object = null, $action = null) {
	// safety check just in case
	if ( !$this->loggedin ) {
		return false;
    }
    // Just checking for logged in status in this case
	if ( is_null($object) &#038;&#038; is_null($action) ) {
		return true;
	}
	// Check access to a specific record
	if ( !is_null($object) &#038;&#038; !is_null($action) ) {
		if ( $this->Acl->check(array('model' => $model, 'foreign_key' => $id), $object, $action) ) {
			return true;
		}
	}
	// Everything failed, so return false
	return false;
}
</pre>
<p>The first thing the function checks is a safety check. Just to make sure, we check to see if they are logged in. If they are not, return false. This is not really a necessary step, but for this application, one of the requirements was paranoid-like logged in checking. The next feature is to make sure logged in users are authorized. If there is no real check for permissions, and they just want to make sure the person is logged in, then this is one method I use to check this. Your application may be different, and that is ok. The final feature is to check a real object and action. We just put the ACL check here, and return true if it is allowed. If all of the IF statements do not get run, then it returns false. So when if you use this method, make sure that the needed checks are done, and the parameters that are passed have a real value and are not just NULL. </p>
<p>So this accounts for the checks I do now. I take out the calls in the controllers and replace them with this call. To see this in action, lets look at a few examples. </p>
<p>A member is logged in and would like to edit their friend&#8217;s profile:</p>
<pre>
function edit($id = null) {
    $info = $this->Member->read(null, $id);
    $this->_enforceAccess("Member", $this->Auth->user('member_id'),  $info['Member']['username'], 'update', array('controller' => "members"))
    // The rest of the action code goes here
}
</pre>
<p>This will do all the checks for you, and redirect to the members index page if the person does not have permission to edit other user&#8217;s account information. </p>
<p>Another example: a person who is logged in wants to view all posts. Posts are visible to only logged in accounts.</p>
<pre>
function view() {
    $this->_enforceAccess();
    // The rest of the action code goes here
}
</pre>
<p>Since this is open to everyone who is logged in, we do not need to send any parameters over, we just need to make sure they are logged in. Since the _enforceAccess() function already checks for this, we do not need to do any further checks in this function. </p>
<p>Another example: an admin wants to delete an account, and this account is the admin account, (yes this has happened before where an admin was &#8220;three sheets to the wind&#8221; and trying to perform an emergency delete of an employee who was terminated, and tried to delete their own account):</p>
<pre>
function delete($id = null) {
    $info = $this->Member->read(null, $id);
    $this->_enforceAccess("Member", $this->Auth->user('member_id'),  $info['Member']['username'], 'delete', array('controller' => "members"))
    // The rest of the action code goes here
}
</pre>
<p>Last example: A member is logged in and trying to view all events for the group they do not belong to:</p>
<pre>
function viewEvents($group_id = null) {
    $info = $this->Events->read(null, $group_id);
    $this->_enforceAccess("Event", $this->Auth->user('member_id'),  $info['Events']['group_id'], 'view', array('controller' => "events", 'action' => "comingup"))
    // The rest of the action code goes here
}
</pre>
<p>In this check, the code pulls in the groups events based on the foreign key of group_id in the Events table. We are enforcing the the access to view these events, based on the person&#8217;s ID. If the person&#8217;s ID does not have permission to view the events for that group, it will redirect the user to the page events/comingup. Which does it&#8217;s own little thing. Each example here will spit out the error message in the _enforceAccess() function. </p>
<p>Now, even though these two functions added some more lines of code, it has reduced what is needed to be done in each controller. Instead of doing a check in each function in each controller, with its accompanying lines of code, you have just one check to do, usually in one line of code. </p>
<p>What works so well for me in this, is that the _enforceAccess() method can be extended. If you want a generic error message, and then do special errors on certain cases, add a new parameter for the error message, and a check to see if it is filled out. If it is use the special error message, if not, then use the default. If you need additional permission checks, this method also provides a central point to do those. </p>
<p>Remember that in any application, it is all what is required by the business/client/needs. This may work in some instances, and not in others.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hirdweb.com/2008/11/17/central-acl-check/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ACL Implementation</title>
		<link>http://www.hirdweb.com/2008/11/05/acl-implementation/</link>
		<comments>http://www.hirdweb.com/2008/11/05/acl-implementation/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 13:32:38 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[cakePHP]]></category>

		<guid isPermaLink="false">http://www.hirdweb.com/?p=134</guid>
		<description><![CDATA[After doing a few posts on Access Control Lists (ACLs), the need to look further into the implementation of ACLs in a CakePHP project could be helpful. If there are questions on setting up the ACL tri-table in the database, you can review the previous postings, or check out the CakePHP documentation. But now that [...]]]></description>
			<content:encoded><![CDATA[<p>After doing a few posts on Access Control Lists (ACLs), the need to look further into the implementation of ACLs in a CakePHP project could be helpful. If there are questions on setting up the ACL tri-table in the database, you can review the <a href='http://www.hirdweb.com/2008/08/25/access-control-lists-acls/' title='Access Control Lists'>previous postings</a>, or check out the <a href='http://book.cakephp.org/view/465/Understanding-How-ACL-Works' target='_blank' title='CakePHP Documentation'>CakePHP documentation</a>. But now that you have the ACL tables set up, how does it actually work?</p>
<p>First, the ACL happens after authentication. So whether or not you are using the Auth component, you will still need to authenticate the user some how, some way. Then once the user is authenticated and logged in, that user will have permissions to do different thing. Let&#8217;s say one of those things is to edit accounts. If it is a regular user, he should be able to edit his own and no one else. If the user was a &#8220;site admin&#8221; he should be able to edit his own and any account that is not a &#8220;super-admin&#8221;. If he is a super admin then he should edit everyone&#8217;s account. However, the first part of this is setting up the initial ACL permissions. </p>
<p><span id="more-134"></span></p>
<p>This step happens right on the account creation action. After the user has been created, and we grab the last inserted ID, we can update the ACL tables. </p>
<pre>
$usr = $this->User->getLastInsertID();

// Set up the ARO data for this user, assign as a member only at this point.
$aro = new Aro();
// Create the info for this new user
$user = array(
	'alias' => $this->data['User']['username'],
	'parent_id' => 4, // member ARO
	'model' => 'User',
	'foreign_key' => $usr
);
// Create the new ARO for the user
$aro->create();
$aro->save($user);
/**********************************************************************/
// Users are a little crazy because they are also ACOs
$aco = new Aco();
$aco_usr = array(
	'alias' => $this->data['User']['username'],
	'parent_id' => 2, // User ACO id
	'model' => 'User',
	'foreign_key' => $usr
);
// Create the new ACO for the user
$aco->create();
$aco->save($aco_usr);

// A User may update itself but not delete itself.
$this->Acl->allow( array('model' => 'User', 'foreign_key' => $usr), $this->data['User']['username'], 'update' );
$this->Acl->deny( array('model' => 'User', 'foreign_key' => $usr), 'Users', 'delete' );

$this->Session->setFlash(__('The User has been saved', true));
$this->redirect(array('action'=>'index'));
</pre>
<p>This will provide for the base permissions for the user. </p>
<p>Remember at this point, we are looking at ACLs from a user model only. If we wanted to extend this to groups, posts, calendars, etc we would add this in here as well. Say we also had a &#8220;posts&#8221; feature in our application, and we wanted to let the user create their own posts, edit their own posts, but no one elses:</p>
<pre>
$usr = $this->Auth->user('user_id');
$aco_posts = array(
	'alias' => $this->data['Post']['post_id'],
	'parent_id' => 4, // Posts ACO id
	'model' => 'Post',
	'foreign_key' => $usr
);
// Create the new ACO for the user to post
$aco->create();
$aco->save($aco_posts);

// A User may add, edit or delete their own posts, but not other user's posts
$this->Acl->allow( array('model' => 'Post', 'foreign_key' => $usr), $this->data['Post']['post_id'], 'update' );
$this->Acl->allow( array('model' => 'Post', 'foreign_key' => $usr), $this->data['Post']['post_id'], 'delete' );
</pre>
<p>So for anything that you want to limit the user access to, this is your chance. But remember the old saying &#8220;Keep it simple stupid&#8221;. Only do something if it makes sense and the project calls for it. I usually only do a User ACL in the users controller. </p>
<p>Now when you a user wants to go to an edit action after they are logged in, we need to check to make sure they can do this action:</p>
<pre>
$info = $this->User->read(null, $id);

// Check for permissions to edit this account
if ( !$this->Acl->check(array('model' => 'User', 'foreign_key' => $this->Auth->user('user_id')), $info['User']['username'], 'update') ) {
	$this->Session->setFlash(__('You are not allowed to edit this user. -- ' . $this->Auth->user('user_id'), true));
	$this->redirect(array('action'=>'index'));
}
</pre>
<p>The above code, broken out:</p>
<ul>
<li />$info gets all of the data about the id to be edited. It grabs the data from the table and stores it in the $info array
<li />Does a check to verify that they have permission, always checking for a NOT to be on the safe side
<li />The Acl->check is looking at the User model, since that is the object that is going to be edited (ACO)
<li />The Auth->user(&#8216;user_id&#8217;) is the object requesting permission to do an edit (ARO)
<li />$info['User']['username'] is the exact ACO that will be updated
<li />&#8216;update&#8217; is the action that will be taken on the exact ACO
</ul>
<p>If the test does not pass, and the currently logged in user does not have permission to update the user_id, then they will be redirected to a the index page for the controller. </p>
<p>Let&#8217;s also take the example above with the Posts, and lets say that a user is wanting to edit a post. At the very top of the function, we would put in a very similar check:</p>
<pre>
$info = $this->Post->read(null, $id);

// Check for permissions to edit this account
if ( !$this->Acl->check(array('model' => 'Post', 'foreign_key' => $this->Auth->user('user_id')), $info['Post']['post_id'], 'update') ) {
	$this->Session->setFlash(__('You are not allowed to edit this post. -- ' . $this->Auth->user('user_id'), true));
	$this->redirect(array('action'=>'index'));
}
</pre>
<p>In this example, we just replace User with Post and the info array elements. The same type of check could be done for delete, create and read actions in the controller. As you can see, this check could be done in a function in the app_controller.php file for a central location, which I will get into the next post. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hirdweb.com/2008/11/05/acl-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Access Control Lists (ACLs) Part 3</title>
		<link>http://www.hirdweb.com/2008/08/27/access-control-lists-acls-part-3/</link>
		<comments>http://www.hirdweb.com/2008/08/27/access-control-lists-acls-part-3/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 16:58:51 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[cakePHP]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.hirdweb.com/?p=72</guid>
		<description><![CDATA[In the first part, the idea and theory behind an ACL was discussed. In part 2, the set up of AROs, ACOs, and ACLs via the command line was shown. Now in part three, we look at why this is so important. Because an interactive site with memberships should never be static, what happens when [...]]]></description>
			<content:encoded><![CDATA[<p>In the first part, the idea and theory behind an ACL was discussed. In part 2, the set up of AROs, ACOs, and ACLs via the command line was shown. Now in part three, we look at why this is so important. Because an interactive site with memberships should never be static, what happens when a new member signs up? What happens when a member is promoted to an &#8220;admin&#8221; level? And what happens when users change? This can all be happened via ACLs. </p>
<p>In part 2, existing member were set up as AROs. And with user accounts, we also have to set those up as ACOs. Then those AROs (people) need to have permissions set for the CRUD actions. (Create, Read, Update, Delete). These actions are specific to the ACO, or object they are trying to manipulate. So if a user wants to edit their own account, do they have permission? If a user wants to delete another person&#8217;s account, do they have permissions to? With setting up ACLs, this can be checked. But what do we do when a new person signs up for an account? We need to create the code to do this. </p>
<p>In the Users Controller, we need to make sure we use the ACL component is included. So include this in the controller:</p>
<pre>
class UsersController extends AppController {
	var $name = 'Users';
	var $components = array('Acl');
</pre>
<p>Also remember that the Auth and Security components are also very powerful components and should be included as well, but the above only shows where to include the components. Now with this in place, we can no address the add (or register) function of the controller. </p>
<p><span id="more-72"></span><br />
When a new user registers an account on the site, we want to make sure to give them only access to their own account, and be able to read the other user profiles. The first thing is to create an &#8220;add&#8221; (or register, I actually prefer that because it just seems to be more logical to me, but I will use the &#8220;add&#8221; function for this example).</p>
<pre>
function add() {

}
</pre>
<p>Now we need to create the basics, or even better, better Bake up the views for the User controller. This helps set the base actions needed to add an account. But here is what some of the included actions should be:</p>
<pre>
function add() {
	if (!empty($this->data)) {
		// Sanitize the stuff
		$clean = new Sanitize();
		$clean->clean($this->data);

		// Set the data to the model
		$this->User->set($this->data);

		// Clean all of the elements in the data array, Attendee, Organization, and Bill information areas
		$this->data['User']['username']	  = $clean->paranoid($this->data['User']['username'], array('.', '-', '\''));
		// Make sure you clean all the user input values to help clean the input from the user, this is just an example line

		// Set any defaults for the user table, these would be items that are not on the form, ie avatar default, last_login date or ip, just to make sure there is no XSS on those fields

		$this->User->create();

		if ($this->User->save($this->data)) {
			$this->Session->setFlash(__('The User has been saved', true));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
		}
	}
}
</pre>
<p>This is just some extensions of the Baked output from the Users controller. I always suggest to sanitze the data, and always set defaults if there are more fields in the table that do not have corresponding inputs. This just helps to cut down on XSS (cross site scripting) and helps to maintain some order of data expected to go in the tables. The next step is to add the ARO and ACO creations for this user. </p>
<p>Every user is an ARO, but they are also ACOs as they may have other users requesting to take action on their accounts. So we need to create the AROs and ACOs for the user right after the create() method:</p>
<pre>
. . .
$this->User->create();
if ($this->User->save($this->data)) {
	$usr = $this->User->getLastInsertID();
</pre>
<p>First we need to get the last inserted ID, because this is the user&#8217;s new user_id. We need to use that here. Then we need to instantiate the ARO object</p>
<pre>
$aro = new Aro();
</pre>
<p>With the ARO object ready to use, we need to create some date to send to the ARO. </p>
<pre>
$user = array(
	'alias' => $this->data['User']['username'],
	'parent_id' => 4, // member ARO
	'model' => 'User',
	'foreign_key' => $usr
);
</pre>
<p>We are creating the information we are to put in to the ARO, the alias (which is the username), the parent ID, in this case, the &#8220;Members&#8221; ARO is aro_id 4, (your parent_id may be different depending on how many AROs you have), the model, which is User in this example, and the foreign key which is the new user id that was just created. Now the Cookbook will say you do not need to put in the alias and the model/foreign_key. But I do because it makes the table easier to read, especially if you are looking at the table doing troubleshooting. It is easier for me, but feel free to find a good method for your own application. </p>
<p>Now take the data, create and ARO and save the data to it. </p>
<pre>
$aro->create();
$aro->save($user);
</pre>
<p>Creating the ACO is similar to the ARO, and this is how it would look. </p>
<pre>
$aco = new Aco();
$aco_usr = array(
	'alias' => $this->data['User']['username'],
	'parent_id' => 2, // User ACO id
	'model' => 'User',
	'foreign_key' => $usr
);

$aco->create();
$aco->save($aco_usr);
</pre>
<p>Now we have an ARO created, and an ACO, we need to make sure the ARO has certain permissions to the ACO (and its parent). Remember that a few rules for all new accounts is that they can only edit their own account, they can not delete any account, and they can view any account. So we can set these permissions after the ARO and ACO creations. </p>
<p>First we want to set it that they can only edit their own account. We are going to implicitly allow access to only this account. </p>
<pre>
$this->Acl->allow( array('model' => 'User', 'foreign_key' => $usr), $this->data['User']['username'], 'update' );
</pre>
<p>By setting the permission this way, it is only granting access to their own account in order to edit or update. If this ARO tries to update another account, it will not have the permissions needed to do so. Now we need to deny the delete action for all User ACOs. </p>
<pre>
$this->Acl->deny( array('model' => 'User', 'foreign_key' => $usr), 'Users', 'delete' );
</pre>
<p>This will deny all delete actions for this ARO. By specifying the &#8220;Users&#8221; ACO, we are safeguarding the delete action. This not only denies the delete action from happening on the &#8220;User&#8221; ACO itself, but all of its child nodes, or in other words, any ACO that has the &#8220;Users&#8221; ACO as it parent. Thus, this newly registered user can not delete any account in the system. </p>
<p>Here is it in its entirety:</p>
<pre>
function add() {
	if (!empty($this->data)) {
		// Sanitize the stuff
		$clean = new Sanitize();
		$clean->clean($this->data);

		// Set the data to the model
		$this->User->set($this->data);

		// Clean all of the elements in the data array, Attendee, Organization, and Bill information areas
		$this->data['User']['username']	  = $clean->paranoid($this->data['User']['username'], array('.', '-', '\''));
		// Make sure you clean all the user input values to help clean the input from the user, this is just an example line

		// Set any defaults for the user table, these would be items that are not on the form, ie avatar default, last_login date or ip, just to make sure there is no XSS on those fields

		$this->User->create();
		if ($this->User->save($this->data)) {
			$usr = $this->User->getLastInsertID();

			$aro = new Aro();
			$user = array(
				'alias' => $this->data['User']['username'],
				'parent_id' => 4, // member ARO
				'model' => 'User',
				'foreign_key' => $usr
			);

			$aro->create();
			$aro->save($user);

			/*****************************/
			$aco = new Aco();
			$aco_usr = array(
				'alias' => $this->data['User']['username'],
				'parent_id' => 2, // User ACO id
				'model' => 'User',
				'foreign_key' => $usr
			);

			$aco->create();
			$aco->save($aco_usr);

			// Set the permissions
			$this->Acl->allow( array('model' => 'User', 'foreign_key' => $usr), $this->data['User']['username'], 'update' );
			$this->Acl->deny( array('model' => 'User', 'foreign_key' => $usr), 'Users', 'delete' );

			$this->Session->setFlash(__('The User has been saved', true));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
		}
	}
}
</pre>
<p>So that will set a new ARO and ACO for a new user. You would follow the same path for an event, or group, but you would only set a new ACO, and the permissions as needed, based on requirements. But this is not the only thing to do. We still need to check these permissions. As it stands right now, any user can edit or delete any other user, because we have not included the ACL check yet. So to demonstrate this check, we will use the Edit function as an example. </p>
<p>In the edit function, there is an ID passed to the function. This is the ID of the account to be edited. We need to check if the currently logged in user has the permissions to edit this account. We do that by doing an ACL check. </p>
<pre>
$this->Acl->check(array('model' => 'model_name', 'foreign_key' => "id of logged in person" "alias of ACO", "action requested");
</pre>
<p>So for this example, it is the User ACO we are wanting, more specifically the user account requested to be edited, and the action is (of course) update. So the first thing is to get the alias of the user to be edited</p>
<pre>
function edit($id = null) {
	$info = $this->User->read(null, $id);
}
</pre>
<p>This will pull the id into an array called &#8220;info&#8221; and we are looking for <i>$info['User']['username']</i>. Remember that your version may vary based upon the table info you have. Now we can do a check based on the currently logged in user, and in this example I am using the Auth component to get that. </p>
<pre>
if ( $this->Acl->check(array('model' => 'User', 'foreign_key' => $this->Auth->user('user_id')), $info['User']['username'], 'update') ) {
	// Do the edit stuff here
} else {
	$this->Session->setFlash(__('You are not allowed to edit this user.', true));
	$this->redirect(array('action'=>'index'));
}
</pre>
<p>This will check the currently logged in user to see if they have update permissions on the specified user_id&#8217;s ACO. If they do have permission, then it will go through the edit actions, checks, etc. If they do not, it bypasses that altogether and kicks them back to the index view with a message. </p>
<p>The same can be done for the delete action</p>
<pre>
function delete($id = null) {
	$info = $this->User->read(null, $id);

	if ( !$this->Acl->check(array('model' => 'User', 'foreign_key' => $this->Auth->user('user_id')), $info['User']['username'], 'delete') ) {
		// Do the delete stuff here
	} else {
		$this->Session->setFlash(__('You are not allowed to DELETE this user.', true));
		$this->redirect(array('action'=>'index'));
	}
}
</pre>
<p>Now this is just a simple example, and this same type of idea can be done for events, editing events, viewing events, etc. The same goes for groups and any other type of application where a permission check is needed. </p>
<p>Now, is my soap-box time. Why use ACLs at all? Wouldn&#8217;t a simple &#8220;level&#8221; table be enough to create this same type of effect? Yes and no. Remember that the right program for the job is determinant upon the job requirements. If an application is going to be used for a small purpose, and maybe you only will have 50 people at the most in a calendar application, then an ACL may be going a little overboard. However, if you have 50 people who are part of different groups, and based on group membership can do different things on the application, and the tasks may change from person to person based on work load, then an ACL may take some of the stress of of doing this type of thing. </p>
<p>Remember that an ACL is a great way to keep permissions in check with little human overhead involved. But sometimes the job requires that human overhead is needed, sometimes not. ACLs are great, and I use them frequently on big jobs. For the smaller jobs, I do not. But after this, hopefully you know better about what an ACL is, and how to use one in an application. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hirdweb.com/2008/08/27/access-control-lists-acls-part-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Access Control Lists (ACLs) Part 2</title>
		<link>http://www.hirdweb.com/2008/08/26/access-control-lists-acls-part-2/</link>
		<comments>http://www.hirdweb.com/2008/08/26/access-control-lists-acls-part-2/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 01:34:56 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[cakePHP]]></category>

		<guid isPermaLink="false">http://www.hirdweb.com/?p=65</guid>
		<description><![CDATA[In my last post, I covered a little bit about what an Access Control List (ACL) was. The Cookbook provides a great more detail. To go along with the idea of the last post, the application has a few different areas: Users who are members of groups, Groups which have users as members, and Events [...]]]></description>
			<content:encoded><![CDATA[<p>In my last post, I covered a little bit about what an Access Control List (ACL) was. The <a href="http://book.cakephp.org/view/171/access-control-lists" target="_blank">Cookbook </a>provides a great more detail. </p>
<p>To go along with the idea of the last post, the application has a few different areas: Users who are members of groups, Groups which have users as members, and Events that belong to either the user or the group. Since the creation of ACOs and AROs are basically the same for each area (Users, Groups, Events), I will detail some code for the Users area making use of ACLs. </p>
<p>The first thing we need to do is create an ARO grouping and an ACO grouping. Remember that AROs are the requester of an object. In this example, we can think of them as people. And people have different types of roles, which is what we need to create for the people. In this example application, there will be site admins (Admins), group leaders (Leaders) and regular members (Members). So we need to create this type of ARO. We can do this in a controller, and a page, or we can do this via the command line. </p>
<p><span id="more-65"></span><br />
If you create the AROs and ACOs via a controller, it would be done this way:</p>
<pre>
$aro = new Aro();
$groups = array(
	0 => array('alias' => 'Admins'),
	1 => array('alias' => 'Leaders'),
	2 => array('alias' => 'Members'),
);

foreach($groups as $data)
{
	$aro->create();
	$aro->save($data);
}
</pre>
<p>Instantiate the new ARO object, then set up information for the ARO. Then iterate through the array in a foreach loop.  You can do the same for the ACO parents that need to be created. The ACOs in this example are similar to our models. There are events, users, and groups. To create those, follow the same pattern as above, only make sure to use the &#8220;Aco()&#8221; object. However, an easier way (at least I think so), is to use the command line interface (cli) to create these. </p>
<p>To use the cli for this, you will need to be logged in via &#8220;SSH&#8221; to the server, or connected via shell, or however else you choose to connect. The command to create the ACOs and AROs is:</p>
<pre>
# cake acl create aro (parent) (node)
# cake acl create aco (parent) (node)
</pre>
<p>So if I was going to create the groups for the parent AROs, I would do the following:</p>
<pre>
# cake acl create aro Members admin
</pre>
<p><i>NOTE:</i> If you have not set up the <a href="http://book.cakephp.org/view/108/the-cakephp-console" target="_new">CakePHP console paths according the Cookbook specifications</a>, then you may have to use this command line a little different call. I always suggest to run the console from the App folder. From there, you can run the following:</p>
<pre>
# php ../cake/console/cake.php acl create aro Members admin
</pre>
<p>I do suggest you set up the console to work without this, but if for some reason you are unable to, this can work as well. </p>
<p>So this will set up each parent ARO and ACO. Now we need to address the users. If there are already users in the system, then we can use the cli to update them and add them to the ARO table. However, users a different bunch, and this is why I choose to use the example on them instead of events or groups. Events and groups are both objects which are requested by users. But users, they are different. For example, if a user with the username of &#8220;test1&#8243; exists, and logs in, and then sees another account, let&#8217;s say that account username is &#8220;answer1&#8243;, we do not want &#8220;test1&#8243; to be able to edit this account. However, we would want an administrator to have this ability. So we need to lock down edit permissions from &#8220;test1&#8243; so that he can only update his own account, and we want to be able to give administrators access to edit any User account. So now users are not only an ARO, but they become an ACO as well. And we need to give (or deny) permissions to any user based on who they are. </p>
<p>So lets say we have 3 users already in the system. &#8220;BigMan1&#8243;, and administrator, &#8220;Newbie&#8221; and &#8220;Lilie&#8221; both regular members. We want to be able to create an ARO for each of them, and then create an ACO for each of their accounts, and then give them permissions. To create the ARO and ACO for each account (with showing the return from the console):</p>
<pre>
$ cake acl create aro Members BigMan1
New Aro 'BigMan1' created.

X-Powered-By: PHP/5.2.6
Content-type: text/html
</pre>
<pre>
$ cake acl create aco Users BigMan1
New Aco 'BigMan1' created.

X-Powered-By: PHP/5.2.6
Content-type: text/html
</pre>
<p>Now we can follow this for the other two and they would be created. Now we need to give them permissions. The admin should have access to edit any User account, delete any user account except for their own, and be able to view any User account. To grant permissions, the command is:</p>
<pre>
$ cake acl grant admin aro aco  action
</pre>
<p>The action can be one of the four CRUD members or &#8220;all&#8221;.  We are going to start with the admin user, because they will get access to all Users. </p>
<pre>
$ cake acl grant BigMan1 Users update
Permission granted.
X-Powered-By: PHP/5.2.6
Content-type: text/html

$ cake acl grant BigMan1 Users delete
Permission granted.
X-Powered-By: PHP/5.2.6
Content-type: text/html

$ cake acl grant BigMan1 Users read
Permission granted.
X-Powered-By: PHP/5.2.6
Content-type: text/html
</pre>
<p>Now there is a problem here. The Admin can delete his own account right now, because we have given him permissions to delete and User account (parent ACO = Users). We do not want him to delete his own account, so we need to deny him permission. This is done very similar, only instead of &#8220;grant&#8221;, it is &#8220;deny&#8221;.</p>
<pre>
$ cake acl deny BigMan1 BigMan1 delete
Permission granted.
X-Powered-By: PHP/5.2.6
Content-type: text/html
</pre>
<p>In the above example, we are doing an acl command, denying permissions for ARO BigMan1 on ACL BigMan1 for the delete action. In the previous example where we granted him permissions, we set the ACO on the parent, so that any child nodes would inherit the same permissions. But on this deny, we want to deny only for a child node. </p>
<p>Now, when we grant access to other users who are not admins, we would do this on the child nodes. So lets say that users can read any User account, only edit their own, and never delete their own account. So we would do that as:</p>
<pre>
$ cake acl grant Newbie Newbie update
Permission granted.
X-Powered-By: PHP/5.2.6
Content-type: text/html

$ cake acl deny Newbie Users delete
Permission denied.
X-Powered-By: PHP/5.2.6
Content-type: text/html

$ cake acl grant Newbie Users read
Permission granted.
X-Powered-By: PHP/5.2.6
Content-type: text/html
</pre>
<p>This will grant &#8220;Newbie&#8221; access to read any User account, update their own account, and denied being able to delete any account. </p>
<p>So that does it for the existing users. But now what about any new user that registers on the site? That will be covered in part 3, and will show some of the true power of the ACL when used in applications. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hirdweb.com/2008/08/26/access-control-lists-acls-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Access Control Lists (ACLs)</title>
		<link>http://www.hirdweb.com/2008/08/25/access-control-lists-acls/</link>
		<comments>http://www.hirdweb.com/2008/08/25/access-control-lists-acls/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 01:20:49 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[cakePHP]]></category>

		<guid isPermaLink="false">http://www.hirdweb.com/?p=42</guid>
		<description><![CDATA[Access Control Lists, or ACLs, provide a good level of access control on any site. Code bases and platforms may use a different method of instituting ACLs and I am going to go through how CakePHP 1.2.x is handling them. First is to understand what an ACL really is. The Cookbook has a good page [...]]]></description>
			<content:encoded><![CDATA[<p>Access Control Lists, or ACLs, provide a good level of access control on any site. Code bases and platforms may use a different method of instituting ACLs and I am going to go through how CakePHP 1.2.x is handling them. </p>
<p>First is to understand what an ACL really is. The <a href="http://book.cakephp.org/view/171/access-control-lists" target="_new">Cookbook has a good page explaining this type of concept</a>. I highly recommend reading through this page. The whole concept behind this ACL can be divided in three parts:</p>
<ul>
<li>ACO &#8211; Access Control Object, object that is being requested</li>
<li>ARO &#8211; Access Request Object, object that is putting in the request</li>
<li>ACL &#8211; Access Control List, determines if an ARO can access an ACO.</li>
</ul>
<p><em>In the Cookbook, they have a very good call out about the ACL, it is not authentication. No matter what code base, or platform you are on, never mistake this. The ACL verification only happens after the person logs in. They can be very powerful together, but authentication must happen first. </em></p>
<p>The next thing to understand is the way an ACL would look in a matrix. Again, the Cookbook provides a great example of this. The one thing that I would rather prefer, but understand why they do this, is the use of the example. Sure, we all like movies, and the Lord of the Rings is a great way to really explain different things, but it may be hard to switch that over to the real world of coding. So for this entry, I am going to use as an example, and Event Calendar. </p>
<p><span id="more-42"></span><br />
In this Event Calendar, here is the breakdown: there are events, there are groups, there are users. Each user is going to be a member of a group. A group will have a &#8220;leader&#8221; who can add, edit or delete group events. A group will also have an &#8220;editor&#8221; who will be able to add or edit group events. And the default group membership is one who can only view group events. </p>
<ul>
<li><strong>Groups</strong></li>
<li> &#8211; <em>Leader</em></li>
<li> &#8211; - Add, edit, delete group events</li>
<li> &#8211; - View all group events</li>
<li> &#8211; <em>Editor</em></li>
<li> &#8211; - Add, or edit group events</li>
<li> &#8211; - View all group events</li>
<li> &#8211; <em>Member</em></li>
<li> &#8211; - View all group events</li>
</ul>
<p>Users will be anyone who creates a login to the application. Of course there will be a &#8220;site admin&#8221; who can add, edit, delete to any user account except their own. They can assign a user to a group, or remove memberships. The group leader can add or remove memberships to their own group only. They can not add, edit or delete user accounts. The default user can edit their own account, and request for the account to be deleted (they can not delete their own account). </p>
<ul>
<li><strong>Users</strong></li>
<li> &#8211; <em>Site Admin</em></li>
<li> &#8211; - Add, edit, delete all user accounts (except the site admin account)</li>
<li> &#8211; - Add, or remove users from any group</li>
<li> &#8211; <em>Leader</em></li>
<li> &#8211; - Add or remove users from their own group only</li>
<li> &#8211; <em>Users</em></li>
<li> &#8211; Sign up for an account, edit only their profile</li>
<li> &#8211; request to be a member of a group</li>
</ul>
<p>Events can be either a group event, or an individual event. Group members can see group events, and an individual event is only available to be viewed by the user who entered the event. </p>
<ul>
<li><strong>Events</strong></li>
<li> &#8211; <em>Group Events</em></li>
<li> &#8211; - Viewable by members of the group</li>
<li> &#8211; <em>Individual Events</em></li>
<li> &#8211; - Viewable only by the user who entered the event</li>
</ul>
<p>To put this in a &#8220;use case&#8221; scenario, here are four examples:</p>
<p>Case 1: Username &#8220;MotoCrash&#8221; is a registered user of the site. He is also a group leader of the &#8220;ProFootball&#8221; group. He is able to update membership of the group, and is able to edit the different events for the group calendar. There are some events he has had to delete. He often updates his own account with a new avatar image. He can view all of the group events to determine if any of them need to be edited.<br />
ARO = &#8220;MotoCrash&#8221;<br />
ACOs = Groups, Events, Users</p>
<p>Case 2: Username &#8220;FootballNut&#8221; is a registered user of the site. He is a member of two (2) groups. He is a regular member of the group named &#8220;ProFootball&#8221; and a group leader of &#8220;PopWarnerBall&#8221;. He is able to see events in both groups, but is only able to add, edit, or delete events in the &#8220;PopWarnerBall&#8221; group. He adds his own events for personal uses that only he is able to see.<br />
ARO = &#8220;FootballNut&#8221;<br />
ACOs = Groups (2 checks), Events</p>
<p>Case 3: Username &#8220;Angel&#8221; is a regular member. She is not part of a group as of yet, but posts events to her own personal calendar. She is able to edit her account, adding a new signature line every month. She is able to view her own events. She is an avid football fan, but is unable to see events for the ProFootball group.<br />
ARO = &#8220;Angel&#8221;<br />
ACOs = Groups, Users, Events</p>
<p>Case 4: Username &#8220;Admin&#8221; is a the site admin. They are able to do all actions in any group, and able to update any memberships, or add, edit or delete accounts. They are not able to delete their own account.<br />
ARO = &#8220;Admin&#8221;<br />
ACOs = Groups, Events, Users, Administation</p>
<table width='90%' cellpadding='2' cellspacing='2' border='1'>
<tr>
<td>&nbsp;</td>
<td colspan='4'>Users</td>
</tr>
<tr>
<td>Area</td>
<td>MotoCrash</td>
<td>FootballNut</td>
<td>Angel</td>
<td>Admin</td>
</tr>
<tr>
<td><b>Group ACO</b>: ProFootball</td>
<td colspan='4'>&nbsp;</td>
</tr>
<tr>
<td><i>Add Members</i></td>
<td align='center'><b>X</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><i>Remove Members</i></td>
<td align='center'><b>X</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><b>Group ACO</b>: PopWarnerBall</td>
<td colspan='4'>&nbsp;</td>
</tr>
<tr>
<td><i>Add Members</i></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><i>Remove Members</i></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><b>Event ACO</b>: ProFootball</td>
<td colspan='4'>&nbsp;</td>
</tr>
<tr>
<td><i>View Events</i></td>
<td align='center'><b>X</b></td>
<td align='center'><b>X</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><i>Add, Edit or Delete Events</i></td>
<td align='center'><b>X</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><b>Event ACO</b>: PopWarnerBall</td>
<td colspan='4'>&nbsp;</td>
</tr>
<tr>
<td><i>View Events</i></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><i>Add, Edit or Delete Events</i></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><b>Individual Event ACO</b>: MotoCrash</td>
<td colspan='4'>&nbsp;</td>
</tr>
<tr>
<td><i>View, Add, Edit, Delete  Events</i></td>
<td align='center'><b>X</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><b>Individual Event ACO</b>: FootballNut</td>
<td colspan='4'>&nbsp;</td>
</tr>
<tr>
<td><i>View, Add, Edit, Delete  Events</i></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><b>Individual Event ACO</b>: Angel</td>
<td colspan='4'>&nbsp;</td>
</tr>
<tr>
<td><i>View, Add, Edit, Delete  Events</i></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><b>User ACO</b></td>
<td colspan='4'>&nbsp;</td>
</tr>
<tr>
<td><i>Update Own Account</i></td>
<td align='center'><b>X</b></td>
<td align='center'><b>X</b></td>
<td align='center'><b>X</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><i>Update Other Account</i></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
<tr>
<td><i>Delete Own Account</i></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
</tr>
<tr>
<td><i>Delete Other Account</i></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>-</b></td>
<td align='center'><b>X</b></td>
</tr>
</table>
<p>I hope this has been helping to better understand ACLs. In order to really delve into this topic, and separate the different areas, I am finishing this post here. I am going to post another one that gets into the code of setting up the tables (acos, aros, aros_acos), and then the coding portion of the ACLs, like when a new user is created, group membership changes, or how to validate on the ACL based on the logged in user. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hirdweb.com/2008/08/25/access-control-lists-acls/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

