function social_group_block_access in Open Social 8.5
Same name and namespace in other branches
- 8.9 modules/social_features/social_group/social_group.module \social_group_block_access()
- 8 modules/social_features/social_group/social_group.module \social_group_block_access()
- 8.2 modules/social_features/social_group/social_group.module \social_group_block_access()
- 8.3 modules/social_features/social_group/social_group.module \social_group_block_access()
- 8.4 modules/social_features/social_group/social_group.module \social_group_block_access()
- 8.6 modules/social_features/social_group/social_group.module \social_group_block_access()
- 8.7 modules/social_features/social_group/social_group.module \social_group_block_access()
- 8.8 modules/social_features/social_group/social_group.module \social_group_block_access()
- 10.3.x modules/social_features/social_group/social_group.module \social_group_block_access()
- 10.0.x modules/social_features/social_group/social_group.module \social_group_block_access()
- 10.1.x modules/social_features/social_group/social_group.module \social_group_block_access()
- 10.2.x modules/social_features/social_group/social_group.module \social_group_block_access()
Implements hook_block_access().
Check if the user is viewing a closed_group, check if the user is a member. If the user is not a member, the user has no access to view this block.
File
- modules/
social_features/ social_group/ social_group.module, line 1113 - The Social group module.
Code
function social_group_block_access(Block $block, $operation, AccountInterface $account) {
$block_id = $block
->getPluginId();
// This is a list of the blocks that this function cares about, if we're being
// called for a different block we exit early.
$managed_blocks = [
'views_exposed_filter_block:newest_groups-page_all_groups',
'views_block:groups-block_user_groups',
'views_block:upcoming_events-upcoming_events_group',
'views_block:latest_topics-group_topics_block',
];
if (!in_array($block_id, $managed_blocks)) {
return AccessResult::neutral();
}
if ($block
->getPluginId() == 'views_exposed_filter_block:newest_groups-page_all_groups' && $operation == 'view' && $account
->isAnonymous()) {
$group_types = 0;
/** @var \Drupal\group\Entity\GroupTypeInterface $group_type */
foreach (GroupType::loadMultiple() as $group_type) {
$group_types += (int) $group_type
->getAnonymousRole()
->hasPermission('view group');
}
if ($group_types < 2) {
return AccessResult::forbidden();
}
}
$group = _social_group_get_current_group();
$user = Drupal::currentUser();
// Check if there is a group set and if its an closed group.
if ($group && $group
->getGroupType()
->id() == 'closed_group' && $account
->id() != 1) {
if ($account
->hasPermission('manage all groups')) {
return AccessResult::neutral();
}
elseif (!$group
->getMember($user)) {
// If it is closed and the current user is not an member of this group,
// then he is not allowed to see these blocks.
$forbidden_blocks = [
'views_block:upcoming_events-upcoming_events_group',
'views_block:latest_topics-group_topics_block',
];
foreach ($forbidden_blocks as $forbidden_block) {
if ($operation == 'view' && $block
->getPluginId() == $forbidden_block) {
return AccessResult::forbidden();
}
}
}
}
// Check if we're alloweed to view the joined groups block.
if ($operation === 'view' && $block
->getPluginId() === 'views_block:groups-block_user_groups') {
// Here we're going to assume by default access is not granted.
$groupController = SocialGroupController::create(\Drupal::getContainer());
$access = $groupController
->myGroupAccess($account);
// If the 'myGroupAccess' returns 'AccessResultNeutral', we have to assume
// that access must be denied.
if ($access instanceof AccessResultNeutral) {
// Return forbidden, since access was not explicitly granted.
return AccessResult::forbidden();
}
return $access;
}
return AccessResult::neutral();
}