<!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 && $args['object_type'] == 'project' && $args['object_id'] && $args['user'] ) ) {
+                return $verdict;
+        }
</ins><span class="cx">         $project = GP::$project->get( $args['object_id'] );
</span><span class="cx">         if ( $project->parent_project_id ) {
</span><span class="cx">                 return $args['user']->can( $args['action'], 'project', $project->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 && $args['object_type'] == 'project|locale|set-slug' && $args['object_id'] && $args['user'] ) ) {
+                return $verdict;
+        }
+        list( $project_id, $locale_slug, $set_slug ) = explode( '|', $args['object_id'] );
+        $project = GP::$project->get( $project_id );
+        if ( $project->parent_project_id ) {
+                return $args['user']->can( $args['action'], $args['object_type'], $project->parent_project_id.'|'.$locale_slug.'|'.$set_slug );
+        }
+        return false;
+}
+
+
+function gp_route_translation_set_permissions_to_parent_project( $verdict, $args ) {
+        if ( !( $verdict == 'no-verdict' && $args['action'] == 'approve' && $args['object_type'] == 'translation-set'
+                        && $args['object_id'] && $args['user'] ) ) {
+                return $verdict;
+        }
+        if ( isset( $args['extra']['set'] ) && $args['extra']['set'] && $args['extra']['set']->id == $args['object_id'] )
+                $set = $args['extra']['set'];
+        else
+                $set = GP::$translation_set->get( $args['object_id'] );
+        return $args['user']->can( 'approve', 'project|locale|set-slug', $set->project_id.'|'.$set->locale.'|'.$set->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->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 ) && $this->id )
</span><span class="cx">                         $user = $this;
</span><span class="lines">@@ -98,6 +98,7 @@
</span><span class="cx">                 $user_id = $user? $user->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->assertTrue( (bool)$user->can( 'write', 'project', $sub->id ) );
</span><span class="cx">                 $this->assertFalse( (bool)$user->can( 'write', 'project', $other->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->create( array( 'user_login' => 'gugu', 'user_email' => 'gugu@gugu.net' ) );
+                
+                $other = GP::$project->create( array( 'name' => 'Other', 'slug' => 'other', 'path' => 'other') );
+                $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root', 'path' => 'root') );
+                $sub = GP::$project->create( array( 'name' => 'Sub', 'slug' => 'sub', 'parent_project_id' => $root->id, 'path' => 'root/sub' ) );
+                
+                GP::$permission->create( array( 'user_id' => $user->id, 'action' => 'whatever',
+                        'object_type' => $object_type, 'object_id' => $root->id.'|bg|default' ) );
+                $this->assertTrue( (bool)$user->can( 'whatever', $object_type, $root->id.'|bg|default' ) );
+                $this->assertTrue( (bool)$user->can( 'whatever', $object_type, $sub->id.'|bg|default' ) );
+                $this->assertFalse( (bool)$user->can( 'other', $object_type, $sub->id.'|bg|default' ) );
+                $this->assertFalse( (bool)$user->can( 'whatever', $object_type, $sub->id.'|en|default' ) );
+                $this->assertFalse( (bool)$user->can( 'whatever', $object_type, $sub->id.'|bg|slug' ) );
+        }
+
+
+        function test_approve_translation_set_permissions() {
+                $user = GP::$user->create( array( 'user_login' => 'gugu', 'user_email' => 'gugu@gugu.net' ) );
+                
+                $other = GP::$project->create( array( 'name' => 'Other', 'slug' => 'other', 'path' => 'other') );
+                $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root', 'path' => 'root') );
+                $sub = GP::$project->create( array( 'name' => 'Sub', 'slug' => 'sub', 'parent_project_id' => $root->id, 'path' => 'root/sub' ) );
+
+                GP::$permission->create( array( 'user_id' => $user->id, 'action' => 'approve',
+                        'object_type' => 'project|locale|set-slug', 'object_id' => $root->id.'|bg|default' ) );
+                
+                $set_root_bg = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $root->id, 'locale' => 'bg') );
+                $set_sub_bg = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $sub->id, 'locale' => 'bg') );
+                $set_root_en = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $root->id, 'locale' => 'en') );
+                $set_root_bg_slug = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'baba', 'project_id' => $root->id, 'locale' => 'bg') );
+                $set_other_bg = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $other->id, 'locale' => 'bg') );
+                $this->assertTrue( (bool)$user->can( 'approve', 'translation-set', $set_root_bg->id ) );
+                $this->assertTrue( (bool)$user->can( 'approve', 'translation-set', $set_sub_bg->id ) );
+                $this->assertTrue( (bool)$user->can( 'approve', 'translation-set', $set_root_bg->id, array('set' => $set_root_bg) ) );
+                $this->assertFalse( (bool)$user->can( 'approve', 'translation-set', $set_root_en->id ) );
+                $this->assertFalse( (bool)$user->can( 'approve', 'translation-set', $set_root_bg_slug->id ) );
+                $this->assertFalse( (bool)$user->can( 'approve', 'translation-set', $set_other_bg->id ) );
+        }
</ins><span class="cx">         
</span><span class="cx">         function assertEqualPermissions( $expected, $actual ) {
</span><span class="cx">                 $fields = $actual->fields();
</span><span class="cx">                 unset($fields['id']);
</span><del>-        }
-        
-        
</del><ins>+        }        
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>
</body>
</html>