<!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>[BuddyPress][11806] trunk: Activity: add function to check if a user has access to a single activity.</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" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/11806">11806</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/11806","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>djpaul</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-01-10 18:59:00 +0000 (Wed, 10 Jan 2018)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Activity: add function to check if a user has access to a single activity.
This change extracts the existing logic from `bp_activity_screen_single_activity_permalink()` into a new function, allowing it to be used in multiple places, such as the REST API, or a WP-CLI extension.
Fixes <a href="http://buddypress.trac.wordpress.org/ticket/7048">#7048</a>
Props espellcaste</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpactivitybpactivityfunctionsphp">trunk/src/bp-activity/bp-activity-functions.php</a></li>
<li><a href="#trunksrcbpactivitybpactivityscreensphp">trunk/src/bp-activity/bp-activity-screens.php</a></li>
<li><a href="#trunktestsphpunittestcasesactivityfunctionsphp">trunk/tests/phpunit/testcases/activity/functions.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpactivitybpactivityfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-activity/bp-activity-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-activity/bp-activity-functions.php 2018-01-10 12:49:30 UTC (rev 11805)
+++ trunk/src/bp-activity/bp-activity-functions.php 2018-01-10 18:59:00 UTC (rev 11806)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3081,6 +3081,55 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Can a user see a particular activity item?
+ *
+ * @since 3.0.0
+ *
+ * @param BP_Activity_Activity $activity Activity object.
+ * @param integer $user_id User ID.
+ * @return boolean True on success, false on failure.
+ */
+function bp_activity_user_can_read( $activity, $user_id = 0 ) {
+ $retval = false;
+
+ // Fallback.
+ if ( empty( $user_id ) ) {
+ $user_id = bp_loggedin_user_id();
+ }
+
+ // Admins and moderators can see everything.
+ if ( bp_current_user_can( 'bp_moderate' ) ) {
+ $retval = true;
+ }
+
+ // If activity author match user, allow access as well.
+ if ( $user_id === $activity->user_id ) {
+ $retval = true;
+ }
+
+ // If activity is from a group, do an extra cap check.
+ if ( ! $retval && bp_is_active( 'groups' ) && $activity->component === buddypress()->groups->id ) {
+
+ // Check to see if the user has access to the activity's parent group.
+ $group = groups_get_group( $activity->item_id );
+ if ( $group ) {
+ $retval = $group->user_has_access;
+ }
+ }
+
+ /**
+ * Filters whether the current user has access to an activity item.
+ *
+ * @since 3.0.0
+ *
+ * @param bool $retval Return value.
+ * @param int $user_id Current user ID.
+ * @param BP_Activity_Activity $activity Activity object.
+ */
+ return apply_filters( 'bp_activity_user_can_read', $retval, $user_id, $activity );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Hide a user's activity.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 1.2.0
</span></span></pre></div>
<a id="trunksrcbpactivitybpactivityscreensphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-activity/bp-activity-screens.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-activity/bp-activity-screens.php 2018-01-10 12:49:30 UTC (rev 11805)
+++ trunk/src/bp-activity/bp-activity-screens.php 2018-01-10 18:59:00 UTC (rev 11806)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -195,19 +195,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 1.2.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return bool|string Boolean on false or the template for a single activity item on success.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function bp_activity_screen_single_activity_permalink() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $bp = buddypress();
-
</del><span class="cx" style="display: block; padding: 0 10px"> // No displayed user or not viewing activity component.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( !bp_is_activity_component() )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! bp_is_activity_component() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! bp_current_action() || !is_numeric( bp_current_action() ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $action = bp_current_action();
+ if ( ! $action || ! is_numeric( $action ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Get the activity details.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $activity = bp_activity_get_specific( array( 'activity_ids' => bp_current_action(), 'show_hidden' => true, 'spam' => 'ham_only', ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $activity = bp_activity_get_specific( array(
+ 'activity_ids' => $action,
+ 'show_hidden' => true,
+ 'spam' => 'ham_only',
+ ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // 404 if activity does not exist
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $activity['activities'][0] ) || bp_action_variables() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -218,39 +224,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $activity = $activity['activities'][0];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Default access is true.
- $has_access = true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user_id = bp_displayed_user_id();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // If activity is from a group, do an extra cap check.
- if ( isset( $bp->groups->id ) && $activity->component == $bp->groups->id ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Check user access to the activity item.
+ *
+ * @since 3.0.0
+ */
+ $has_access = bp_activity_user_can_read( $activity, $user_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Activity is from a group, but groups is currently disabled.
- if ( !bp_is_active( 'groups') ) {
- bp_do_404();
- return;
- }
-
- // Check to see if the user has access to to the activity's parent group.
- if ( $group = groups_get_group( $activity->item_id ) ) {
- $has_access = $group->user_has_access;
- }
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> // If activity author does not match displayed user, block access.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( true === $has_access && bp_displayed_user_id() !== $activity->user_id ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // More info:https://buddypress.trac.wordpress.org/ticket/7048#comment:28
+ if ( true === $has_access && $user_id !== $activity->user_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $has_access = false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Filters the access permission for a single activity view.
- *
- * @since 1.2.0
- *
- * @param array $access Array holding the current $has_access value and current activity item instance.
- */
- $has_access = apply_filters_ref_array( 'bp_activity_permalink_access', array( $has_access, &$activity ) );
-
- /**
</del><span class="cx" style="display: block; padding: 0 10px"> * Fires before the loading of a single activity template file.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 1.2.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -273,7 +262,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $url = sprintf(
</span><span class="cx" style="display: block; padding: 0 10px"> wp_login_url( 'wp-login.php?redirect_to=%s' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- esc_url_raw( bp_activity_get_permalink( bp_current_action() ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ esc_url_raw( bp_activity_get_permalink( $action ) )
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -287,7 +276,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $template Path to the activity template to load.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- bp_core_load_template( apply_filters( 'bp_activity_template_profile_activity_permalink', 'members/single/activity/permalink' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template = apply_filters( 'bp_activity_template_profile_activity_permalink', 'members/single/activity/permalink' );
+
+ // Load the template.
+ bp_core_load_template( $template );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'bp_screens', 'bp_activity_screen_single_activity_permalink' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestcasesactivityfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/activity/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/activity/functions.php 2018-01-10 12:49:30 UTC (rev 11805)
+++ trunk/tests/phpunit/testcases/activity/functions.php 2018-01-10 18:59:00 UTC (rev 11806)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1475,6 +1475,133 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( array(), $found['activities'] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @group bp_activity_user_can_read
+ */
+ public function test_user_can_access_their_own_activity() {
+ $u = self::factory()->user->create();
+
+ $a = self::factory()->activity->create( array(
+ 'user_id' => $u,
+ ) );
+
+ $o = self::factory()->activity->get_object_by_id( $a );
+
+ $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
+ }
+
+ /**
+ * @group bp_activity_user_can_read
+ */
+ public function test_user_cannot_access_someone_elses_activity() {
+ $u = self::factory()->user->create();
+ $u2 = self::factory()->user->create();
+
+ $a = self::factory()->activity->create( array(
+ 'user_id' => $u2,
+ ) );
+
+ $o = self::factory()->activity->get_object_by_id( $a );
+
+ $this->assertFalse( bp_activity_user_can_read( $o, $u ) );
+ $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
+ }
+
+ /**
+ * @group bp_activity_user_can_read
+ */
+ public function test_admin_can_access_someone_elses_activity() {
+ $u = self::factory()->user->create();
+ $u2 = self::factory()->user->create( array( 'role' => 'administrator' ) );
+
+ $a = self::factory()->activity->create( array(
+ 'user_id' => $u,
+ ) );
+
+ $o = self::factory()->activity->get_object_by_id( $a );
+
+ $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
+
+ $this->set_current_user( $u2 );
+ $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
+ }
+
+ /**
+ * @group bp_activity_user_can_read
+ */
+ public function test_group_admin_access_someone_elses_activity_in_a_grou() {
+ $u = self::factory()->user->create();
+ $u2 = self::factory()->user->create();
+
+ $g = self::factory()->group->create();
+
+ $a = self::factory()->activity->create( array(
+ 'component' => buddypress()->groups->id,
+ 'user_id' => $u,
+ 'item_id' => $g,
+ ) );
+
+ $o = self::factory()->activity->get_object_by_id( $a );
+
+ $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
+
+ self::add_user_to_group( $u2, $g );
+
+ $m1 = new BP_Groups_Member( $u2, $g );
+ $m1->promote( 'admin' );
+
+ $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
+ }
+
+ /**
+ * @group bp_activity_user_can_read
+ */
+ public function test_non_member_can_access_to_someone_elses_activity_in_a_group() {
+ $u = self::factory()->user->create();
+ $u2 = self::factory()->user->create();
+
+ $g = self::factory()->group->create();
+
+ self::add_user_to_group( $u, $g );
+
+ $a = self::factory()->activity->create( array(
+ 'component' => buddypress()->groups->id,
+ 'user_id' => $u,
+ 'item_id' => $g,
+ ) );
+
+ $o = self::factory()->activity->get_object_by_id( $a );
+
+ $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
+ $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
+ }
+
+ /**
+ * @group bp_activity_user_can_read
+ */
+ public function test_user_access_to_his_activity_in_disabled_group() {
+ $u = self::factory()->user->create();
+ $g = self::factory()->group->create();
+
+ self::add_user_to_group( $u, $g );
+
+ $a = self::factory()->activity->create( array(
+ 'component' => buddypress()->groups->id,
+ 'user_id' => $u,
+ 'item_id' => $g,
+ ) );
+
+ $o = self::factory()->activity->get_object_by_id( $a );
+
+ groups_edit_group_settings( $g, 0, 'hidden' );
+
+ $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
+
+ groups_edit_group_settings( $g, 0, 'private' );
+
+ $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function check_activity_caches() {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $this->acaches as $k => $v ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->acaches[ $k ] = wp_cache_get( $k, 'bp_activity' );
</span></span></pre>
</div>
</div>
</body>
</html>