<?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, 26 Mar 2012 03:30:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: gogink</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-28274</link>
		<dc:creator>gogink</dc:creator>
		<pubDate>Tue, 05 Apr 2011 23:20:31 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-28274</guid>
		<description>What if resources is dynamic? If resorces are items with itemId and userId (user that create item)?How to dynamicly add resorces by userID, and then assert those same resources?</description>
		<content:encoded><![CDATA[<p>What if resources is dynamic? If resorces are items with itemId and userId (user that create item)?How to dynamicly add resorces by userID, and then assert those same resources?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Weltkind</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-27077</link>
		<dc:creator>Weltkind</dc:creator>
		<pubDate>Thu, 24 Mar 2011 07:46:53 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-27077</guid>
		<description>Great thanks for this article! It is that I need so much!</description>
		<content:encoded><![CDATA[<p>Great thanks for this article! It is that I need so much!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wil Moore III</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-22293</link>
		<dc:creator>Wil Moore III</dc:creator>
		<pubDate>Wed, 16 Feb 2011 08:27:23 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-22293</guid>
		<description>Nice article Ralph. I sort of wish I had read this before implementing the same; however, doing so allowed me to dig pretty deep.

Konr mentioned this quite a while ago, but I thought I&#039;d bring it up here in case anyone else stumbles across this article.

Instead of this:

// if role is publisher, he can always modify a post
        if ($user-&gt;getRoleId() == &#039;publisher&#039;) {
            return true;
        }

I do this:

$acl-&gt;allow(&#039;publisher&#039;, &#039;blogPost&#039;, array(&#039;publish&#039;,&#039;modify&#039;));

Just to be clear; Is there any reason (besides demonstration purposes) to check the publisher&#039;s roleId via the assertion class vs. int he allow definition?</description>
		<content:encoded><![CDATA[<p>Nice article Ralph. I sort of wish I had read this before implementing the same; however, doing so allowed me to dig pretty deep.</p>
<p>Konr mentioned this quite a while ago, but I thought I&#8217;d bring it up here in case anyone else stumbles across this article.</p>
<p>Instead of this:</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>I do this:</p>
<p>$acl-&gt;allow(&#8216;publisher&#8217;, &#8216;blogPost&#8217;, array(&#8216;publish&#8217;,'modify&#8217;));</p>
<p>Just to be clear; Is there any reason (besides demonstration purposes) to check the publisher&#8217;s roleId via the assertion class vs. int he allow definition?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dynamic Assertions for Zend_Acl in ZF &#124; Zend Framework University</title>
		<link>http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf/comment-page-1#comment-7729</link>
		<dc:creator>Dynamic Assertions for Zend_Acl in ZF &#124; Zend Framework University</dc:creator>
		<pubDate>Fri, 27 Aug 2010 23:39:26 +0000</pubDate>
		<guid isPermaLink="false">http://ralphschindler.com/?p=34#comment-7729</guid>
		<description>[...] Zend_Acl can now be used to make concise, dynamic and expressive ACL systems. The assertion system that is in place in Zend_Acl can be leveraged in ways never seen before out of the box. While the User/BlogPost example is on the simple side, you can use this article to start thinking about the different ways such a system can be leveraged in your own projects where dynamic assertions would simplify controller or model code that is already in place.  Author: Ralph Schindler Source: Ralph Schindler » Zend Framework [...]</description>
		<content:encoded><![CDATA[<p>[...] Zend_Acl can now be used to make concise, dynamic and expressive ACL systems. The assertion system that is in place in Zend_Acl can be leveraged in ways never seen before out of the box. While the User/BlogPost example is on the simple side, you can use this article to start thinking about the different ways such a system can be leveraged in your own projects where dynamic assertions would simplify controller or model code that is already in place.  Author: Ralph Schindler Source: Ralph Schindler » Zend Framework [...]</p>
]]></content:encoded>
	</item>
	<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>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: ralphschindler.com @ 2012-05-17 15:54:30 -->
