1

I am using organic groups and I want the owner/reviewer of the group to be able to access an unpublished group content created by members of the group.

I know I can give "View any unpublished content" rights to a global user role, but this will be to strong (I have different reviewers for different groups and single reviewer should not have access to unpublished content of all the groups).

I want to give that right to the specific role in the group and "Organic groups global permissions" simply does not have that option.

I am familiar with hooks system but I am not sure how to approach the issue. Any ideas?

3

Have a look at this example implementation which creates a new OG Permission View unpublished content and uses Node Grants to decide who can view unpublished nodes. You now can configure which OG Role in your groups are allowed to view unpublished nodes.

<?php
define('MY_MODULE_REALM_VIEW_UNPUBLISHED', 'my_module_view_unpublished');

/**
 * Implements hook_og_permission().
 */
function my_module_og_permission() {
  // Add OG permission to view unpublished nodes.
  return array(
    'view unpublished' => array(
      'title' => t('View unpublished content'),
      'description' => t("Allow user to see unpublished content within the group."),
      // Determine to which roles the permissions will be enabled by default.
      'default role' => array(OG_ADMINISTRATOR_ROLE),
    ),
  );
}

/**
 * Implements hook_node_grants().
 */
function my_module_node_grants($account, $op) {
  if ($op != 'view') {
    return;
  }

  $grants = array();

  // Handle access to unpublished nodes.
  // Get all OG Group IDs the user is allowed to view unpublished nodes in.
  $entities = og_get_entity_groups();
  foreach ($entities['node'] as $entity_id) {
    if (og_user_access('node', $entity_id, 'view unpublished')) {
      $grants[MY_MODULE_REALM_VIEW_UNPUBLISHED][] = $entity_id;
    }
  }

  return $grants;
}

/**
 * Implements hook_node_access_records().
 */
function my_module_node_access_records($node) {
  // Node is unpublished and belongs to a OG group.
  if (!(empty($node->status) && $groups = og_get_entity_groups('node', $node))) {
    return array();
  }

  foreach ($groups['node'] as $nid) {
    $grants[] = array(
      'realm' => MY_MODULE_REALM_VIEW_UNPUBLISHED,
      'gid' => $nid,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => 10,
    );
  }
  return $grants;
}
| improve this answer | |
  • To add this no longer works very well. I have implemented this and found that when the user goes to log into the site, it times out because they are not added to the primary/default group. Once I add them, then they can log in but this isn't ideal as we want them to only be added to their specific group. – Travis Johnston Mar 21 '16 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.