<!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" />
<title>[27390] trunk: Don't default to current user for capability checks when dealing with a post without an author (post_author = 0).</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { 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 #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#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>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/27390">27390</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2014-03-04 03:08:54 +0000 (Tue, 04 Mar 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Don't default to current user for capability checks when dealing with a post without an author (post_author = 0).

Undoes <a href="http://core.trac.wordpress.org/changeset/12053">[12053]</a>. While it risks breakage, this is a far safer and saner default for these situations.

props danielbachhuber.
fixes <a href="http://core.trac.wordpress.org/ticket/27020">#27020</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescapabilitiesphp">trunk/src/wp-includes/capabilities.php</a></li>
<li><a href="#trunktestsphpunittestsusercapabilitiesphp">trunk/tests/phpunit/tests/user/capabilities.php</a></li>
<li><a href="#trunktestsphpunittestsusermapMetaCapphp">trunk/tests/phpunit/tests/user/mapMetaCap.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescapabilitiesphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/capabilities.php (27389 => 27390)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/capabilities.php   2014-03-04 02:19:41 UTC (rev 27389)
+++ trunk/src/wp-includes/capabilities.php      2014-03-04 03:08:54 UTC (rev 27390)
</span><span class="lines">@@ -1071,20 +1071,15 @@
</span><span class="cx">                  break;
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               $post_author_id = $post->post_author;
-
-               // If no author set yet, default to current user for cap checks.
-               if ( ! $post_author_id )
-                       $post_author_id = $user_id;
-
-               // If the user is the author...
-               if ( $user_id == $post_author_id ) {
</del><ins>+                // If the post author is set and the user is the author...
+               if ( $post->post_author && $user_id == $post->post_author ) {
</ins><span class="cx">                   // If the post is published...
</span><span class="cx">                  if ( 'publish' == $post->post_status ) {
</span><span class="cx">                          $caps[] = $post_type->cap->delete_published_posts;
</span><span class="cx">                  } elseif ( 'trash' == $post->post_status ) {
</span><del>-                               if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
</del><ins>+                                if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
</ins><span class="cx">                                   $caps[] = $post_type->cap->delete_published_posts;
</span><ins>+                               }
</ins><span class="cx">                   } else {
</span><span class="cx">                          // If the post is draft...
</span><span class="cx">                          $caps[] = $post_type->cap->delete_posts;
</span><span class="lines">@@ -1093,10 +1088,11 @@
</span><span class="cx">                  // The user is trying to edit someone else's post.
</span><span class="cx">                  $caps[] = $post_type->cap->delete_others_posts;
</span><span class="cx">                  // The post is published, extra cap required.
</span><del>-                       if ( 'publish' == $post->post_status )
</del><ins>+                        if ( 'publish' == $post->post_status ) {
</ins><span class="cx">                           $caps[] = $post_type->cap->delete_published_posts;
</span><del>-                       elseif ( 'private' == $post->post_status )
</del><ins>+                        } elseif ( 'private' == $post->post_status ) {
</ins><span class="cx">                           $caps[] = $post_type->cap->delete_private_posts;
</span><ins>+                       }
</ins><span class="cx">           }
</span><span class="cx">          break;
</span><span class="cx">          // edit_post breaks down to edit_posts, edit_published_posts, or
</span><span class="lines">@@ -1121,20 +1117,15 @@
</span><span class="cx">                  break;
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               $post_author_id = $post->post_author;
-
-               // If no author set yet, default to current user for cap checks.
-               if ( ! $post_author_id )
-                       $post_author_id = $user_id;
-
-               // If the user is the author...
-               if ( $user_id == $post_author_id ) {
</del><ins>+                // If the post author is set and the user is the author...
+               if ( $post->post_author && $user_id == $post->post_author ) {
</ins><span class="cx">                   // If the post is published...
</span><span class="cx">                  if ( 'publish' == $post->post_status ) {
</span><span class="cx">                          $caps[] = $post_type->cap->edit_published_posts;
</span><span class="cx">                  } elseif ( 'trash' == $post->post_status ) {
</span><del>-                               if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
</del><ins>+                                if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
</ins><span class="cx">                                   $caps[] = $post_type->cap->edit_published_posts;
</span><ins>+                               }
</ins><span class="cx">                   } else {
</span><span class="cx">                          // If the post is draft...
</span><span class="cx">                          $caps[] = $post_type->cap->edit_posts;
</span><span class="lines">@@ -1143,10 +1134,11 @@
</span><span class="cx">                  // The user is trying to edit someone else's post.
</span><span class="cx">                  $caps[] = $post_type->cap->edit_others_posts;
</span><span class="cx">                  // The post is published, extra cap required.
</span><del>-                       if ( 'publish' == $post->post_status )
</del><ins>+                        if ( 'publish' == $post->post_status ) {
</ins><span class="cx">                           $caps[] = $post_type->cap->edit_published_posts;
</span><del>-                       elseif ( 'private' == $post->post_status )
</del><ins>+                        } elseif ( 'private' == $post->post_status ) {
</ins><span class="cx">                           $caps[] = $post_type->cap->edit_private_posts;
</span><ins>+                       }
</ins><span class="cx">           }
</span><span class="cx">          break;
</span><span class="cx">  case 'read_post':
</span><span class="lines">@@ -1173,18 +1165,13 @@
</span><span class="cx">                  break;
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               $post_author_id = $post->post_author;
-
-               // If no author set yet, default to current user for cap checks.
-               if ( ! $post_author_id )
-                       $post_author_id = $user_id;
-
-               if ( $user_id == $post_author_id )
</del><ins>+                if ( $post->post_author && $user_id == $post->post_author ) {
</ins><span class="cx">                   $caps[] = $post_type->cap->read;
</span><del>-               elseif ( $status_obj->private )
</del><ins>+                } elseif ( $status_obj->private ) {
</ins><span class="cx">                   $caps[] = $post_type->cap->read_private_posts;
</span><del>-               else
</del><ins>+                } else {
</ins><span class="cx">                   $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
</span><ins>+               }
</ins><span class="cx">           break;
</span><span class="cx">  case 'publish_post':
</span><span class="cx">          $post = get_post( $args[0] );
</span></span></pre></div>
<a id="trunktestsphpunittestsusercapabilitiesphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/user/capabilities.php (27389 => 27390)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/user/capabilities.php  2014-03-04 02:19:41 UTC (rev 27389)
+++ trunk/tests/phpunit/tests/user/capabilities.php     2014-03-04 03:08:54 UTC (rev 27390)
</span><span class="lines">@@ -524,7 +524,34 @@
</span><span class="cx">          }
</span><span class="cx">  }
</span><span class="cx"> 
</span><ins>+       function authorless_post_statuses() {
+               return array( array( 'draft' ), array( 'private' ), array( 'publish' ) );
+       }
+
</ins><span class="cx">   /**
</span><ins>+        * @ticket 27020
+        * @dataProvider authorless_post_statuses
+        */
+       function test_authorless_post( $status ) {
+               // Make a post without an author
+               $post = $this->factory->post->create( array( 'post_author' => 0, 'post_type' => 'post', 'post_status' => $status ) );
+
+               // Add an editor and contributor
+               $editor = $this->factory->user->create_and_get( array( 'role' => 'editor' ) );
+               $contributor = $this->factory->user->create_and_get( array( 'role' => 'contributor' ) );
+
+               // editor can edit, view, and trash
+               $this->assertTrue( $editor->has_cap( 'edit_post', $post ) );
+               $this->assertTrue( $editor->has_cap( 'delete_post', $post ) );
+               $this->assertTrue( $editor->has_cap( 'read_post', $post ) );
+
+               // a contributor cannot (except read a published post)
+               $this->assertFalse( $contributor->has_cap( 'edit_post', $post ) );
+               $this->assertFalse( $contributor->has_cap( 'delete_post', $post ) );
+               $this->assertEquals( $status === 'publish', $contributor->has_cap( 'read_post', $post ) );
+       }
+
+       /**
</ins><span class="cx">    * @ticket 16714
</span><span class="cx">   */
</span><span class="cx">  function test_create_posts_caps() {
</span></span></pre></div>
<a id="trunktestsphpunittestsusermapMetaCapphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/user/mapMetaCap.php (27389 => 27390)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/user/mapMetaCap.php    2014-03-04 02:19:41 UTC (rev 27389)
+++ trunk/tests/phpunit/tests/user/mapMetaCap.php       2014-03-04 03:08:54 UTC (rev 27390)
</span><span class="lines">@@ -232,4 +232,18 @@
</span><span class="cx">          $this->assertEquals( array( 'update_core' ), map_meta_cap( 'update_core', $this->user_id ) );
</span><span class="cx">          $this->assertEquals( array( 'edit_plugins' ), map_meta_cap( 'edit_plugins', $this->user_id ) );
</span><span class="cx">  }
</span><ins>+
+       /**
+        * Test a post without an author.
+        *
+        * @ticket 27020
+        */
+       function test_authorless_posts_capabilties() {
+               $post_id = $this->factory->post->create( array( 'post_author' => 0, 'post_type' => 'post', 'post_status' => 'publish' ) );
+               $editor = $this->factory->user->create( array( 'role' => 'editor' ) );
+
+               $this->assertEquals( array( 'edit_others_posts', 'edit_published_posts' ), map_meta_cap( 'edit_post', $editor, $post_id ) );
+               $this->assertEquals( array( 'delete_others_posts', 'delete_published_posts' ), map_meta_cap( 'delete_post', $editor, $post_id ) );
+
+       }
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>