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
  • 1
    You can add your own hook_permission (api.drupal.org/api/drupal/modules%21system%21system.api.php/…) , where input parameter will be id of group(gid) and user id (uid), then check if user (uid) is group administer (gid) and return true if yes
    – Gaydabura
    Commented Jan 14, 2013 at 11:11
  • Gaydabura thanks for the hint. I have found that there is a coresponding hook in the OG module hook_og_permission. I will still have to write some code but I can use api.drupalize.me/api/drupal/function/… as an example so it should be simple enough.
    – Pawel G
    Commented Jan 14, 2013 at 11:24
  • it is even better. good luck
    – Gaydabura
    Commented Jan 14, 2013 at 11:24

1 Answer 1

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;
}
1
  • 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. Commented Mar 21, 2016 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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