You are here

function flag_lists_flag_action_access in Flag Lists 8

Same name and namespace in other branches
  1. 4.0.x flag_lists.module \flag_lists_flag_action_access()

Implements hook_flag_action_access().

File

./flag_lists.module, line 255
Contains flag_lists.module.

Code

function flag_lists_flag_action_access($action, FlagInterface $flag, AccountInterface $account, EntityInterface $flaggable = NULL) {

  // If this is a flag list and we have flag list access,
  // also provide access to the flag.
  // Allow both users flag and the corresponding template.
  // Currently only the users flag lists are accepted.
  // This needs to corrected to allow also global lists.
  $flagListsService = Drupal::service('flaglists');
  $flagLists = $flagListsService
    ->getAllFlaggingCollections();
  foreach ($flagLists as $flagList) {
    if ($flag
      ->id() == $flagList
      ->getRelatedFlag()
      ->id()) {
      if ($flagList
        ->getBaseFlag()
        ->isGlobal()) {

        // The flag list is global.
        return AccessResult::allowedIfHasPermission($account, 'access global flag lists');
      }
      elseif ($flagList
        ->getOwnerId() == $account
        ->id()) {

        // The flag list is owned by current user.
        return AccessResult::allowedIfHasPermission($account, 'view own flag lists');
      }
    }
  }

  // Check for access for the used template as well.
  $flagTemplates = $flagListsService
    ->getAllFlagForList();
  foreach ($flagTemplates as $flagTemplate) {
    if ($flag
      ->id() == $flagTemplate
      ->id()) {

      // This is one of the template flags.
      // But is it global?
      $baseFlag = Drupal::service('flag')
        ->getFlagById($flagTemplate
        ->getBaseFlag());
      if ($baseFlag
        ->isGlobal()) {
        return AccessResult::allowedIfHasPermission($account, 'access global flag lists');
      }
      else {
        return AccessResult::allowedIfHasPermission($account, 'view own flag lists');
      }
    }
  }
  return AccessResult::neutral();
}