public function testGroupItemVisibility()
 {
     $original_user = _elgg_services()->session->getLoggedInUser();
     _elgg_services()->session->setLoggedInUser($this->user);
     $group_guid = $this->group->guid;
     // unrestricted: pass non-members
     $this->group->setContentAccessMode(ElggGroup::CONTENT_ACCESS_MODE_UNRESTRICTED);
     $vis = Elgg_GroupItemVisibility::factory($group_guid, false);
     $this->assertFalse($vis->shouldHideItems);
     // membersonly: non-members fail
     $this->group->setContentAccessMode(ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY);
     $vis = Elgg_GroupItemVisibility::factory($group_guid, false);
     $this->assertTrue($vis->shouldHideItems);
     // members succeed
     $this->group->join($this->user);
     $vis = Elgg_GroupItemVisibility::factory($group_guid, false);
     $this->assertFalse($vis->shouldHideItems);
     // non-member admins succeed - assumes admin logged in
     _elgg_services()->session->setLoggedInUser($original_user);
     $vis = Elgg_GroupItemVisibility::factory($group_guid, false);
     $this->assertFalse($vis->shouldHideItems);
 }
/**
 * May the current user access item(s) on this page? If the page owner is a group,
 * membership, visibility, and logged in status are taken into account.
 *
 * @param bool $forward    If set to true (default), will forward the page;
 *                         if set to false, will return true or false.
 *
 * @param int  $group_guid The group that owns the page. If not set, this
 *                         will be pulled from elgg_get_page_owner_guid().
 *
 * @return bool Will return if $forward is set to false.
 * @since 1.9.0
 */
function elgg_group_gatekeeper($forward = true, $group_guid = null)
{
    if (null === $group_guid) {
        $group_guid = elgg_get_page_owner_guid();
    }
    if (!$group_guid) {
        return true;
    }
    // this handles non-groups and invisible groups
    $visibility = Elgg_GroupItemVisibility::factory($group_guid);
    if (!$visibility->shouldHideItems) {
        return true;
    }
    if ($forward) {
        // only forward to group if user can see it
        $group = get_entity($group_guid);
        $forward_url = $group ? $group->getURL() : '';
        if (!elgg_is_logged_in()) {
            _elgg_services()->session->set('last_forward_from', current_page_url());
            $forward_reason = 'login';
        } else {
            $forward_reason = 'member';
        }
        $msg_keys = array('non_member' => 'membershiprequired', 'logged_out' => 'loggedinrequired', 'no_access' => 'noaccess');
        register_error(elgg_echo($msg_keys[$visibility->reasonHidden]));
        forward($forward_url, $forward_reason);
    }
    return false;
}