<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[GlotPress] [456] trunk:
  Recurse project|locale|set-slug permissions and make translation set approval permisions use these transparently</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd>456</dd>
<dt>Author</dt> <dd>nbachiyski</dd>
<dt>Date</dt> <dd>2010-04-05 12:05:51 +0000 (Mon, 05 Apr 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Recurse project|locale|set-slug permissions and make translation set approval permisions use these transparently</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkgpincludesadvancedpermissionsphp">trunk/gp-includes/advanced-permissions.php</a></li>
<li><a href="#trunkgpincludesthingsuserphp">trunk/gp-includes/things/user.php</a></li>
<li><a href="#trunkttest_permissionsphp">trunk/t/test_permissions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkgpincludesadvancedpermissionsphp"></a>
<div class="modfile"><h4>Modified: trunk/gp-includes/advanced-permissions.php (455 => 456)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/gp-includes/advanced-permissions.php        2010-04-05 12:01:10 UTC (rev 455)
+++ trunk/gp-includes/advanced-permissions.php        2010-04-05 12:05:51 UTC (rev 456)
</span><span class="lines">@@ -4,8 +4,10 @@
</span><span class="cx">  * Filter for can_user, which tries if the user
</span><span class="cx">  * has permissions on project parents
</span><span class="cx">  */
</span><del>-function gp_recurse_project_permissions( $verdict, $args ) {        
-        if ( $verdict || $args['object_type'] != 'project' || !$args['object_id'] || !$args['user'] ) return $verdict;
</del><ins>+function gp_recurse_project_permissions( $verdict, $args ) {
+        if ( !( !$verdict &amp;&amp; $args['object_type'] == 'project' &amp;&amp; $args['object_id'] &amp;&amp; $args['user'] ) ) {
+                return $verdict;
+        }
</ins><span class="cx">         $project = GP::$project-&gt;get( $args['object_id'] );
</span><span class="cx">         if ( $project-&gt;parent_project_id ) {
</span><span class="cx">                 return $args['user']-&gt;can( $args['action'], 'project', $project-&gt;parent_project_id );
</span><span class="lines">@@ -13,4 +15,32 @@
</span><span class="cx">         return false;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+function gp_recurse_project_locale_set_slug_permissions( $verdict, $args ) {
+        if ( !( !$verdict &amp;&amp; $args['object_type'] == 'project|locale|set-slug' &amp;&amp; $args['object_id'] &amp;&amp; $args['user'] ) ) {
+                return $verdict;
+        }
+        list( $project_id, $locale_slug, $set_slug ) = explode( '|', $args['object_id'] );
+        $project = GP::$project-&gt;get( $project_id );
+        if ( $project-&gt;parent_project_id ) {
+                return $args['user']-&gt;can( $args['action'], $args['object_type'], $project-&gt;parent_project_id.'|'.$locale_slug.'|'.$set_slug );
+        }
+        return false;
+}
+
+
+function gp_route_translation_set_permissions_to_parent_project( $verdict, $args ) {
+        if ( !( $verdict == 'no-verdict' &amp;&amp; $args['action'] == 'approve' &amp;&amp; $args['object_type'] == 'translation-set'
+                        &amp;&amp; $args['object_id'] &amp;&amp; $args['user'] ) ) {
+                return $verdict;
+        }
+        if ( isset( $args['extra']['set'] ) &amp;&amp; $args['extra']['set'] &amp;&amp; $args['extra']['set']-&gt;id == $args['object_id'] )
+                $set = $args['extra']['set'];
+        else
+                $set = GP::$translation_set-&gt;get( $args['object_id'] );
+        return $args['user']-&gt;can( 'approve', 'project|locale|set-slug', $set-&gt;project_id.'|'.$set-&gt;locale.'|'.$set-&gt;slug );
+        
+}
+
</ins><span class="cx"> add_filter( 'can_user', 'gp_recurse_project_permissions', 10, 2 );
</span><ins>+add_filter( 'can_user', 'gp_recurse_project_locale_set_slug_permissions', 10, 2 );
+add_filter( 'pre_can_user', 'gp_route_translation_set_permissions_to_parent_project', 10, 2 );
</ins></span></pre></div>
<a id="trunkgpincludesthingsuserphp"></a>
<div class="modfile"><h4>Modified: trunk/gp-includes/things/user.php (455 => 456)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/gp-includes/things/user.php        2010-04-05 12:01:10 UTC (rev 455)
+++ trunk/gp-includes/things/user.php        2010-04-05 12:05:51 UTC (rev 456)
</span><span class="lines">@@ -89,7 +89,7 @@
</span><span class="cx">          * 
</span><span class="cx">          * Example: $user-&gt;can( 'read', 'translation-set', 11 );
</span><span class="cx">          */
</span><del>-        function can( $action, $object_type = null, $object_id = null) {
</del><ins>+        function can( $action, $object_type = null, $object_id = null, $extra = null ) {
</ins><span class="cx">                 $user = null;
</span><span class="cx">                 if ( isset( $this ) &amp;&amp; $this-&gt;id )
</span><span class="cx">                         $user = $this;
</span><span class="lines">@@ -98,6 +98,7 @@
</span><span class="cx">                 $user_id = $user? $user-&gt;id : null;
</span><span class="cx">                 $args = $filter_args = compact( 'user_id', 'action', 'object_type', 'object_id' );
</span><span class="cx">                 $filter_args['user'] = $user;
</span><ins>+                $filter_args['extra'] = $extra;
</ins><span class="cx">                 $preliminary = apply_filters( 'pre_can_user', 'no-verdict', $filter_args );
</span><span class="cx">                 if ( is_bool( $preliminary ) ) {
</span><span class="cx">                         return $preliminary;
</span></span></pre></div>
<a id="trunkttest_permissionsphp"></a>
<div class="modfile"><h4>Modified: trunk/t/test_permissions.php (455 => 456)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/t/test_permissions.php        2010-04-05 12:01:10 UTC (rev 455)
+++ trunk/t/test_permissions.php        2010-04-05 12:05:51 UTC (rev 456)
</span><span class="lines">@@ -34,11 +34,51 @@
</span><span class="cx">                 $this-&gt;assertTrue( (bool)$user-&gt;can( 'write', 'project', $sub-&gt;id ) );
</span><span class="cx">                 $this-&gt;assertFalse( (bool)$user-&gt;can( 'write', 'project', $other-&gt;id ) );
</span><span class="cx">         }
</span><ins>+
+        function test_recursive_project_locale_set_slug_permissions() {
+                $object_type = 'project|locale|set-slug';
+                $action = 'whetever';
+                $user = GP::$user-&gt;create( array( 'user_login' =&gt; 'gugu', 'user_email' =&gt; 'gugu@gugu.net' ) );
+                
+                $other = GP::$project-&gt;create( array( 'name' =&gt; 'Other', 'slug' =&gt; 'other', 'path' =&gt; 'other') );
+                $root = GP::$project-&gt;create( array( 'name' =&gt; 'Root', 'slug' =&gt; 'root', 'path' =&gt; 'root') );
+                $sub = GP::$project-&gt;create( array( 'name' =&gt; 'Sub', 'slug' =&gt; 'sub', 'parent_project_id' =&gt; $root-&gt;id, 'path' =&gt; 'root/sub' ) );
+                
+                GP::$permission-&gt;create( array( 'user_id' =&gt; $user-&gt;id, 'action' =&gt; 'whatever',
+                        'object_type' =&gt; $object_type, 'object_id' =&gt; $root-&gt;id.'|bg|default' ) );
+                $this-&gt;assertTrue( (bool)$user-&gt;can( 'whatever', $object_type, $root-&gt;id.'|bg|default' ) );
+                $this-&gt;assertTrue( (bool)$user-&gt;can( 'whatever', $object_type, $sub-&gt;id.'|bg|default' ) );
+                $this-&gt;assertFalse( (bool)$user-&gt;can( 'other', $object_type, $sub-&gt;id.'|bg|default' ) );
+                $this-&gt;assertFalse( (bool)$user-&gt;can( 'whatever', $object_type, $sub-&gt;id.'|en|default' ) );
+                $this-&gt;assertFalse( (bool)$user-&gt;can( 'whatever', $object_type, $sub-&gt;id.'|bg|slug' ) );
+        }
+
+
+        function test_approve_translation_set_permissions() {
+                $user = GP::$user-&gt;create( array( 'user_login' =&gt; 'gugu', 'user_email' =&gt; 'gugu@gugu.net' ) );
+                
+                $other = GP::$project-&gt;create( array( 'name' =&gt; 'Other', 'slug' =&gt; 'other', 'path' =&gt; 'other') );
+                $root = GP::$project-&gt;create( array( 'name' =&gt; 'Root', 'slug' =&gt; 'root', 'path' =&gt; 'root') );
+                $sub = GP::$project-&gt;create( array( 'name' =&gt; 'Sub', 'slug' =&gt; 'sub', 'parent_project_id' =&gt; $root-&gt;id, 'path' =&gt; 'root/sub' ) );
+
+                GP::$permission-&gt;create( array( 'user_id' =&gt; $user-&gt;id, 'action' =&gt; 'approve',
+                        'object_type' =&gt; 'project|locale|set-slug', 'object_id' =&gt; $root-&gt;id.'|bg|default' ) );
+                
+                $set_root_bg = GP::$translation_set-&gt;create( array( 'name' =&gt; 'Set', 'slug' =&gt; 'default', 'project_id' =&gt; $root-&gt;id, 'locale' =&gt; 'bg') );
+                $set_sub_bg = GP::$translation_set-&gt;create( array( 'name' =&gt; 'Set', 'slug' =&gt; 'default', 'project_id' =&gt; $sub-&gt;id, 'locale' =&gt; 'bg') );
+                $set_root_en = GP::$translation_set-&gt;create( array( 'name' =&gt; 'Set', 'slug' =&gt; 'default', 'project_id' =&gt; $root-&gt;id, 'locale' =&gt; 'en') );
+                $set_root_bg_slug = GP::$translation_set-&gt;create( array( 'name' =&gt; 'Set', 'slug' =&gt; 'baba', 'project_id' =&gt; $root-&gt;id, 'locale' =&gt; 'bg') );
+                $set_other_bg = GP::$translation_set-&gt;create( array( 'name' =&gt; 'Set', 'slug' =&gt; 'default', 'project_id' =&gt; $other-&gt;id, 'locale' =&gt; 'bg') );
+                $this-&gt;assertTrue( (bool)$user-&gt;can( 'approve', 'translation-set', $set_root_bg-&gt;id ) );
+                $this-&gt;assertTrue( (bool)$user-&gt;can( 'approve', 'translation-set', $set_sub_bg-&gt;id ) );
+                $this-&gt;assertTrue( (bool)$user-&gt;can( 'approve', 'translation-set', $set_root_bg-&gt;id, array('set' =&gt; $set_root_bg) ) );
+                $this-&gt;assertFalse( (bool)$user-&gt;can( 'approve', 'translation-set', $set_root_en-&gt;id ) );
+                $this-&gt;assertFalse( (bool)$user-&gt;can( 'approve', 'translation-set', $set_root_bg_slug-&gt;id ) );
+                $this-&gt;assertFalse( (bool)$user-&gt;can( 'approve', 'translation-set', $set_other_bg-&gt;id ) );
+        }
</ins><span class="cx">         
</span><span class="cx">         function assertEqualPermissions( $expected, $actual ) {
</span><span class="cx">                 $fields = $actual-&gt;fields();
</span><span class="cx">                 unset($fields['id']);
</span><del>-        }
-        
-        
</del><ins>+        }        
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>