Skip to content

Commit

Permalink
Merge pull request #26 from Gizra/25-flag-action-access
Browse files Browse the repository at this point in the history
Check email flag access
  • Loading branch information
jhedstrom authored Sep 22, 2016
2 parents 9f1a82f + 31da4ef commit 7a7f05b
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 26 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ before_script:
- travis_retry git clone --branch 8.x-1.x --depth 1 https://github.com/Gizra/message_notify.git
- cd ..

# Patch flag.
# @todo Remove this once https://www.drupal.org/node/2802653 lands.
- cd modules/flag
- curl https://www.drupal.org/files/issues/2802653-10.patch > patch.txt
- git apply patch.txt
- cd -

# Install Composer dependencies on 8.1.x and above.
- test ${DRUPAL_CORE} == "8.0.x" || composer self-update && composer install

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ display:
plugin_id: flag_link
filters:
status:
value: true
value: '1'
table: node_field_data
field: status
plugin_id: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ display:
plugin_id: flag_link
filters:
status:
value: true
value: '1'
table: users_field_data
field: status
plugin_id: boolean
Expand Down
35 changes: 17 additions & 18 deletions message_subscribe_email/message_subscribe_email.module
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,32 @@
* Code for the message subscribe email feature.
*/

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\flag\FlagInterface;

/**
* Implements hook_flag_access().
*
* @todo Flag access is broken. See https://www.drupal.org/node/2584647.
* Implements hook_flag_action_access().
*/
function message_subscribe_email_flag_access(FlagInterface $flag, $action, AccountInterface $account) {
return;
if (strpos($flag->name, 'email_') === 0) {

$entity_type = FLAG_API_VERSION == 3 ? $flag->entity_type : $flag->content_type;
function message_subscribe_email_flag_action_access($action, FlagInterface $flag, AccountInterface $account, EntityInterface $flaggable = NULL) {
// The 'unflag' action is always allowed here, so only check 'flag'.
if ($action === 'flag' && $flaggable && strpos($flag->id(), 'email_') === 0) {

// Get the other flags on that same content.
$user_flags = flag_get_user_flags($entity_type, $content_id, $account->uid);
/** @var \Drupal\flag\FlaggingInterface[] $user_flaggings */
$user_flaggings = \Drupal::service('flag')->getAllEntityFlaggings($flaggable, $account);
$user_flags = [];
foreach ($user_flaggings as $flagging) {
$user_flags[$flagging->getFlagId()] = $flagging;
}

$name = str_replace('email_', '', $flag->name);
// @FIXME
// // @FIXME
// // This looks like another module's variable. You'll need to rewrite this call
// // to ensure that it uses the correct configuration object.
// $prefix = variable_get('flag_prefix', 'subscribe') . '_';
// Find the corresponding subscribe_* flag.
$name = str_replace('email_', '', $flag->id());
$prefix = \Drupal::config('message_subscribe.settings')->get('flag_prefix') . '_';

// Disable access to the flag when the subscribe flag is unflagged, but
// allow unflagging.
return $action == 'unflag' || !empty($user_flags[$prefix . $name]);
// Disable access to the flag when the subscribe flag is unflagged.
return AccessResult::forbiddenIf(empty($user_flags[$prefix . $name]));
}
}

Expand Down
2 changes: 1 addition & 1 deletion message_subscribe_email/src/EventSubscriber/FlagEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FlagEvents implements EventSubscriberInterface {
/**
* The flag service.
*
* @var \Drupal\flag\FlagServiceInterface $flag_service
* @var \Drupal\flag\FlagServiceInterface
*/
protected $flagService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,27 @@ public function testEmailNotifications() {
$this->assertEquals($expected_uids, $uids, 'All expected subscribers were fetched.');
}

/**
* Verify flag action access for the email_* flags.
*/
public function testFlagActionAccess() {
$node = $this->nodes[1];
$user = $this->users[1];
$email_flag = $this->flagService->getFlagById('email_node');
$subscribe_flag = $this->flagService->getFlagById('subscribe_node');

// When the item is flagged, flag and unflag access should be available.
$access = $email_flag->actionAccess('flag', $user, $node);
$this->assertTrue($access->isAllowed());
$access = $email_flag->actionAccess('unflag', $user);
$this->assertTrue($access->isAllowed());

// Unflag the entity, and now only the unflag action should be available.
$this->flagService->unflag($subscribe_flag, $node, $user);
$access = $email_flag->actionAccess('flag', $user, $node);
$this->assertFalse($access->isAllowed());
$access = $email_flag->actionAccess('unflag', $user, $node);
$this->assertTrue($access->isAllowed());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ display:
plugin_id: flag_link
filters:
status:
value: true
value: '1'
table: node_field_data
field: status
plugin_id: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ display:
plugin_id: flag_link
filters:
status:
value: true
value: '1'
table: users_field_data
field: status
plugin_id: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function tabAccess(AccountInterface $user, FlagInterface $flag = NULL) {
return AccessResult::allowed();
}

if (!$flag->hasActionAccess('unflag', $user) || $user->id() != $this->currentUser->id()) {
if (!$flag->actionAccess('unflag', $user) || $user->id() != $this->currentUser->id()) {
return AccessResult::forbidden();
}

Expand Down Expand Up @@ -139,7 +139,7 @@ public function tabTitle(FlagInterface $flag) {
* @param \Drupal\flag\FlagInterface $flag
* The flag to display subscriptions for.
*
* @return array A render array.
* @return array
* A render array.
*/
public function tab(UserInterface $user, FlagInterface $flag = NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/SubscribersInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function getBasicContext(EntityInterface $entity, $skip_detailed_context
* (optional) The user account to filter available flags. If not set, all
* flags for the given entity and bundle will be returned.
*
* @return \Drupal\flag\FlagInterface[] $flags
* @return \Drupal\flag\FlagInterface[]
* An array of the structure [fid] = flag_object.
*
* @see \Drupal\flag\FlagServiceInterface::getFlags()
Expand Down

0 comments on commit 7a7f05b

Please sign in to comment.