<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Dynamic Assertions for Zend_Acl in ZF</title>
	<atom:link href="http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/feed" rel="self" type="application/rss+xml" />
	<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf</link>
	<description>Ralph Schindler</description>
	<lastBuildDate>Mon, 17 May 2010 17:30:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Cameron</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-1556</link>
		<dc:creator>Cameron</dc:creator>
		<pubDate>Wed, 02 Dec 2009 07:23:03 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-1556</guid>
		<description>This is literally the best webpage I have ever seen. Thanks, Ralph.</description>
		<content:encoded><![CDATA[<p>This is literally the best webpage I have ever seen. Thanks, Ralph.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hector Virgen</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-930</link>
		<dc:creator>Hector Virgen</dc:creator>
		<pubDate>Tue, 13 Oct 2009 21:20:33 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-930</guid>
		<description>Hi Ralph,

In your class UserCanModifyBlogPostAssertion, you are throwing an InvalidArgumentException if the role is not a User or the resource is not a BlogPost. I&#039;ve found that this can cause a problem if you query the ACL with strings instead of objects:

$acl-&gt;isAllowed(&#039;user&#039;, $blogPost, &#039;modify&#039;); // throws exception

Although the query doesn&#039;t make a whole lot of sense, this is still correct usage of Zend_Acl. It may be better to return false in your assertion if you can&#039;t work with the passed in objects.</description>
		<content:encoded><![CDATA[<p>Hi Ralph,</p>
<p>In your class UserCanModifyBlogPostAssertion, you are throwing an InvalidArgumentException if the role is not a User or the resource is not a BlogPost. I&#8217;ve found that this can cause a problem if you query the ACL with strings instead of objects:</p>
<p>$acl-&gt;isAllowed(&#8216;user&#8217;, $blogPost, &#8216;modify&#8217;); // throws exception</p>
<p>Although the query doesn&#8217;t make a whole lot of sense, this is still correct usage of Zend_Acl. It may be better to return false in your assertion if you can&#8217;t work with the passed in objects.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hector Virgen</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-866</link>
		<dc:creator>Hector Virgen</dc:creator>
		<pubDate>Thu, 08 Oct 2009 22:20:52 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-866</guid>
		<description>These changes are a huge improvement. Well done! It used to take me days to build a dynamic ACL system from scratch -- now I can do it in a few minutes.</description>
		<content:encoded><![CDATA[<p>These changes are a huge improvement. Well done! It used to take me days to build a dynamic ACL system from scratch &#8212; now I can do it in a few minutes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Mintz</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-865</link>
		<dc:creator>David Mintz</dc:creator>
		<pubDate>Thu, 08 Oct 2009 19:38:54 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-865</guid>
		<description>&quot;In plain English, what developers want to be able to do is be able to design assertions that can accept application models that implement the Resource or Role interface, and be able to apply some dynamic or custom logic to assess whether or not the given role has access to the given resource.&quot;

That&#039;s actually pretty funny, believe it or not. (-:</description>
		<content:encoded><![CDATA[<p>&#8220;In plain English, what developers want to be able to do is be able to design assertions that can accept application models that implement the Resource or Role interface, and be able to apply some dynamic or custom logic to assess whether or not the given role has access to the given resource.&#8221;</p>
<p>That&#8217;s actually pretty funny, believe it or not. (-:</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-233</link>
		<dc:creator>Jonathan</dc:creator>
		<pubDate>Thu, 20 Aug 2009 10:04:48 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-233</guid>
		<description>Great Article! Thanks!</description>
		<content:encoded><![CDATA[<p>Great Article! Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Toxygene</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-226</link>
		<dc:creator>Toxygene</dc:creator>
		<pubDate>Wed, 19 Aug 2009 18:42:57 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-226</guid>
		<description>class User implements Zend_Acl_Role_Interface {
  public $id;
  public function __construct($id, $role) {
    $this-&gt;id = $id;
    $this-&gt;role = $role;
  }
  public function getRoleId()
  {
    return &quot;{$this-&gt;role}-{$this-&gt;id}&quot;;
  }
}

class Post implements Zend_Acl_Resource_Interface {
  public $id;
  public $authorId;
  public function __construct($id = null, $authorId = null) {
    $this-&gt;id = $id;
    $this-&gt;authorId = $authorId;
  }
  public function isAuthor(User $user) {
    return $user-&gt;id == $this-&gt;authorId;
  }
  public function getResourceId() {
    if ($this-&gt;id) {
      return &quot;posts-{$this-&gt;id}&quot;;
    } else {
      return &quot;posts&quot;;
    }
  }
}

class IsAuthorOfPost implements Zend_Acl_Assert_Interface {
  public function assert(Zend_Acl $acl, Zend_Acl_Role_Interface $role = null, Zend_Acl_Resource_Interface $resource = null, $privilege = null) {
    // type checks
    return $resource-&gt;isAuthor($role);
  }
}

$acl = new Zend_Acl();
$acl-&gt;addRole(&quot;users&quot;)
    -&gt;addRole(&quot;authors&quot;, array(&quot;users&quot;))
    -&gt;addRole(&quot;admins&quot;)
    -&gt;addResource(&quot;posts&quot;)
    -&gt;allow(&quot;admins&quot;, &quot;posts&quot;)
    -&gt;allow(&quot;authors&quot;, &quot;posts&quot;, &quot;create&quot;)
    -&gt;allow(&quot;authors&quot;, &quot;posts&quot;, array(&quot;delete&quot;, &quot;edit&quot;), new IsAuthorOfPost());

$allan = new User(1, &quot;users&quot;);
$acl-&gt;addRole($allan, $allan-&gt;role);

$mary = new User(2, &quot;authors&quot;);
$acl-&gt;addRole($mary, $mary-&gt;role);

$joe  = new User(3, &quot;authors&quot;);
$acl-&gt;addRole($joe, $joe-&gt;role);

$mike = new User(4, &quot;admins&quot;);
$acl-&gt;addRole($mike, $mike-&gt;role);

var_dump($acl-&gt;isAllowed($allan, new Post(), &quot;create&quot;)); // false, users cannot create posts
var_dump($acl-&gt;isAllowed($mary, new Post(), &quot;create&quot;)); // true, authors can create posts
var_dump($acl-&gt;isAllowed($joe, new Post(), &quot;create&quot;)); // true, authors can create posts
var_dump($acl-&gt;isAllowed($mike, new Post(), &quot;create&quot;)); // true, admins can do anything with a post

$joesPost = new Post(1, $joe-&gt;id);
$acl-&gt;addResource($joesPost, &quot;posts&quot;);

var_dump($acl-&gt;isAllowed($allan, $joesPost, &quot;edit&quot;)); // false, Allan is not an author
var_dump($acl-&gt;isAllowed($mary, $joesPost, &quot;edit&quot;)); // false, Marry is not the author of the post
var_dump($acl-&gt;isAllowed($joe, $joesPost, &quot;edit&quot;)); // true, Joe is the author of the post
var_dump($acl-&gt;isAllowed($mike, $joesPost, &quot;edit&quot;)); // true, admins can do anything with posts</description>
		<content:encoded><![CDATA[<p>class User implements Zend_Acl_Role_Interface {<br />
  public $id;<br />
  public function __construct($id, $role) {<br />
    $this-&gt;id = $id;<br />
    $this-&gt;role = $role;<br />
  }<br />
  public function getRoleId()<br />
  {<br />
    return &#8220;{$this-&gt;role}-{$this-&gt;id}&#8221;;<br />
  }<br />
}</p>
<p>class Post implements Zend_Acl_Resource_Interface {<br />
  public $id;<br />
  public $authorId;<br />
  public function __construct($id = null, $authorId = null) {<br />
    $this-&gt;id = $id;<br />
    $this-&gt;authorId = $authorId;<br />
  }<br />
  public function isAuthor(User $user) {<br />
    return $user-&gt;id == $this-&gt;authorId;<br />
  }<br />
  public function getResourceId() {<br />
    if ($this-&gt;id) {<br />
      return &#8220;posts-{$this-&gt;id}&#8221;;<br />
    } else {<br />
      return &#8220;posts&#8221;;<br />
    }<br />
  }<br />
}</p>
<p>class IsAuthorOfPost implements Zend_Acl_Assert_Interface {<br />
  public function assert(Zend_Acl $acl, Zend_Acl_Role_Interface $role = null, Zend_Acl_Resource_Interface $resource = null, $privilege = null) {<br />
    // type checks<br />
    return $resource-&gt;isAuthor($role);<br />
  }<br />
}</p>
<p>$acl = new Zend_Acl();<br />
$acl-&gt;addRole(&#8220;users&#8221;)<br />
    -&gt;addRole(&#8220;authors&#8221;, array(&#8220;users&#8221;))<br />
    -&gt;addRole(&#8220;admins&#8221;)<br />
    -&gt;addResource(&#8220;posts&#8221;)<br />
    -&gt;allow(&#8220;admins&#8221;, &#8220;posts&#8221;)<br />
    -&gt;allow(&#8220;authors&#8221;, &#8220;posts&#8221;, &#8220;create&#8221;)<br />
    -&gt;allow(&#8220;authors&#8221;, &#8220;posts&#8221;, array(&#8220;delete&#8221;, &#8220;edit&#8221;), new IsAuthorOfPost());</p>
<p>$allan = new User(1, &#8220;users&#8221;);<br />
$acl-&gt;addRole($allan, $allan-&gt;role);</p>
<p>$mary = new User(2, &#8220;authors&#8221;);<br />
$acl-&gt;addRole($mary, $mary-&gt;role);</p>
<p>$joe  = new User(3, &#8220;authors&#8221;);<br />
$acl-&gt;addRole($joe, $joe-&gt;role);</p>
<p>$mike = new User(4, &#8220;admins&#8221;);<br />
$acl-&gt;addRole($mike, $mike-&gt;role);</p>
<p>var_dump($acl-&gt;isAllowed($allan, new Post(), &#8220;create&#8221;)); // false, users cannot create posts<br />
var_dump($acl-&gt;isAllowed($mary, new Post(), &#8220;create&#8221;)); // true, authors can create posts<br />
var_dump($acl-&gt;isAllowed($joe, new Post(), &#8220;create&#8221;)); // true, authors can create posts<br />
var_dump($acl-&gt;isAllowed($mike, new Post(), &#8220;create&#8221;)); // true, admins can do anything with a post</p>
<p>$joesPost = new Post(1, $joe-&gt;id);<br />
$acl-&gt;addResource($joesPost, &#8220;posts&#8221;);</p>
<p>var_dump($acl-&gt;isAllowed($allan, $joesPost, &#8220;edit&#8221;)); // false, Allan is not an author<br />
var_dump($acl-&gt;isAllowed($mary, $joesPost, &#8220;edit&#8221;)); // false, Marry is not the author of the post<br />
var_dump($acl-&gt;isAllowed($joe, $joesPost, &#8220;edit&#8221;)); // true, Joe is the author of the post<br />
var_dump($acl-&gt;isAllowed($mike, $joesPost, &#8220;edit&#8221;)); // true, admins can do anything with posts</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Konr Ness</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-224</link>
		<dc:creator>Konr Ness</dc:creator>
		<pubDate>Wed, 19 Aug 2009 17:42:17 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-224</guid>
		<description>Why do you have ACL permission for the publisher role in the UserCanModifyBlogPostAssertion class?

// if role is publisher, he can always modify a post
if ($user-&gt;getRoleId() == &#039;publisher&#039;) {
    return true;
}

Shouldn&#039;t this instead be handled at the ACL level:
$acl-&gt;allow(&#039;publisher&#039;, &#039;blogPost&#039;, &#039;modify&#039;);

Is it because publisher inherits from contributor? Would my example work if this permission was removed from UserCanModifyBlogPostAssertion?</description>
		<content:encoded><![CDATA[<p>Why do you have ACL permission for the publisher role in the UserCanModifyBlogPostAssertion class?</p>
<p>// if role is publisher, he can always modify a post<br />
if ($user-&gt;getRoleId() == &#8216;publisher&#8217;) {<br />
    return true;<br />
}</p>
<p>Shouldn&#8217;t this instead be handled at the ACL level:<br />
$acl-&gt;allow(&#8216;publisher&#8217;, &#8216;blogPost&#8217;, &#8216;modify&#8217;);</p>
<p>Is it because publisher inherits from contributor? Would my example work if this permission was removed from UserCanModifyBlogPostAssertion?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-192</link>
		<dc:creator>Greg</dc:creator>
		<pubDate>Mon, 17 Aug 2009 20:17:36 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-192</guid>
		<description>I am interested in knowing how to make this multi-role aware. Example being if the user can be a publisher and a reviewer. roleId no longer accurately describes this. Would you create a new dynamic role (perhaps based on their username) and then attach both the pub and review roles? Something like:

&lt;code&gt;
$roles = $identity[&#039;ROLES&#039;];   //array of string roles are associated with the user perhaps from db
$acl-&gt;addRole(new Zend_Acl_Role(&#039;user-&#039;.$name),$roles);  //make sure ACL knows about this new &quot;role&quot;
$user-&gt;setRoleId(&#039;user-&#039;.$name);
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>I am interested in knowing how to make this multi-role aware. Example being if the user can be a publisher and a reviewer. roleId no longer accurately describes this. Would you create a new dynamic role (perhaps based on their username) and then attach both the pub and review roles? Something like:</p>
<p><code><br />
$roles = $identity['ROLES'];   //array of string roles are associated with the user perhaps from db<br />
$acl->addRole(new Zend_Acl_Role('user-'.$name),$roles);  //make sure ACL knows about this new "role"<br />
$user->setRoleId('user-'.$name);<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-185</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Mon, 17 Aug 2009 03:57:30 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-185</guid>
		<description>Thanks for this!

Just wondering - how would you go about spitting out a list of blog posts that a user is able to edit?</description>
		<content:encoded><![CDATA[<p>Thanks for this!</p>
<p>Just wondering &#8211; how would you go about spitting out a list of blog posts that a user is able to edit?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Exception e</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-176</link>
		<dc:creator>Exception e</dc:creator>
		<pubDate>Fri, 14 Aug 2009 15:28:30 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-176</guid>
		<description>Hi, thanks for the link. It is a well written article. I can understand where your reasoning comes from
But suppose I&#039;ve written a module with a class Model_User and I need to integrate a 3rd-party module that also contribute a model for User, which use the same convention and is thus called Model_User too. Now we can more easily get into problems I think.

Some might suggest that models are module-specific but I dare to question that. It is quite normal that different modules from you application use the same model. Modules are not a means to separate the model, but they contribute behaviour to an application. I can agree what you say
 &quot;A Module is a collection of code that solves a more specific atomic problem of the larger business problem.&quot; But I miss the notion of «general/reusable/application agnostic behaviour».

Maybe we could define it as &quot;A Module is a pluggable and reusable stand-alone collection of code that solves a (more or less) concrete larger business problem.&quot;

Maybe I went off-topic. Maybe I am nit-picking. :D</description>
		<content:encoded><![CDATA[<p>Hi, thanks for the link. It is a well written article. I can understand where your reasoning comes from<br />
But suppose I&#8217;ve written a module with a class Model_User and I need to integrate a 3rd-party module that also contribute a model for User, which use the same convention and is thus called Model_User too. Now we can more easily get into problems I think.</p>
<p>Some might suggest that models are module-specific but I dare to question that. It is quite normal that different modules from you application use the same model. Modules are not a means to separate the model, but they contribute behaviour to an application. I can agree what you say<br />
 &#8220;A Module is a collection of code that solves a more specific atomic problem of the larger business problem.&#8221; But I miss the notion of «general/reusable/application agnostic behaviour».</p>
<p>Maybe we could define it as &#8220;A Module is a pluggable and reusable stand-alone collection of code that solves a (more or less) concrete larger business problem.&#8221;</p>
<p>Maybe I went off-topic. Maybe I am nit-picking. <img src='http://ralphschindler.com/wordpress/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
