You are here

function gnode_group_node_create_access in Group 7

Determines whether a user could create a node in a Group context.

Parameters

string $type: The node type the user is attempting to create.

object $account: (optional) The account of the user.

Return value

bool Whether the user has creation rights.

1 call to gnode_group_node_create_access()
gnode_node_access in modules/gnode/gnode.node_access.inc
Implements hook_node_access().
1 string reference to 'gnode_group_node_create_access'
gnode_global_node_create_access in modules/gnode/gnode.module
Determines whether a user could create a node in a site-wide context.

File

modules/gnode/gnode.module, line 149
Contains Group's implementation of the Node module hooks and forms.

Code

function gnode_group_node_create_access($type, $account = NULL) {

  // This flag can be manipulated to not have Group Node interfere with the
  // results of hook_node_access(). This can be useful if you need to figure
  // out whether a user had node creation rights before Group Node decided
  // to allow such action.
  $skip =& drupal_static(__FUNCTION__, FALSE);
  if ($skip) {
    return FALSE;
  }
  global $user;
  if (!isset($account)) {
    $account = $user;
  }

  // If the user can bypass group access, he is allowed access.
  if (user_access('bypass group access', $account)) {
    return TRUE;
  }

  // Determine whether the account could be 'anonymous' or 'outsider'.
  $account_exists = (bool) $account->uid;

  // Check for all group types if a non-member can create nodes of the given
  // node type in them.
  foreach (group_types() as $group_type) {

    // Retrieve the permissions to check for creation rights.
    $check_permissions = $account_exists ? $group_type->outsider_permissions : $group_type->anonymous_permissions;
    $has_access = in_array('administer group', $check_permissions);
    $has_access = $has_access || in_array("create {$type} node", $check_permissions);

    // If the group type allows access to non-members and the provided user
    // has as at least one group of this type he is not a member of, we allow
    // access.
    if ($has_access && group_non_member_gids($account->uid, $group_type->name)) {
      return TRUE;
    }
  }

  // Check the user's groups for creation rights.
  foreach (group_load_by_member($account->uid) as $group) {
    $has_access = group_access('administer group', $group, $account);
    $has_access = $has_access || group_access("create {$type} node", $group, $account);
    if ($has_access) {
      return TRUE;
    }
  }
  return FALSE;
}