Skip to content

Commit

Permalink
Gizra#242 Improvements for og_access port. (Gizra#2)
Browse files Browse the repository at this point in the history
* Gizra#242 Remove og_ui dependency, replace defines with OgAccess class and its consts, also in the whole module.

* Gizra#242 Grammar issue is fixed.

* Gizra#242 Introduce service injection and replace t() with the injected one.

* Gizra#242 PHPCS fixes.
  • Loading branch information
tatarbj authored and gheydon committed Oct 26, 2020
1 parent 9cb2c72 commit 479f8ed
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 54 deletions.
1 change: 0 additions & 1 deletion og_access/og_access.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ type: module

dependencies:
- og
- og_ui
60 changes: 18 additions & 42 deletions og_access/og_access.module
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,7 @@ use Drupal\field\Entity\FieldConfig;
use Drupal\node\NodeInterface;
use Drupal\og\Og;
use Drupal\og_access\OgAccessBundleFormAlter;

/**
* The access realm of group member.
*/
define('OG_ACCESS_REALM', 'og_access');

/**
* Group public access field.
*/
define('OG_ACCESS_FIELD', 'group_access');

/**
* Group public access field.
*/
define('OG_ACCESS_CONTENT_FIELD', 'group_content_access');

/**
* Public group/group content access.
*/
define('OG_ACCESS_PUBLIC', 0);

/**
* Private group/group content access.
*/
define('OG_ACCESS_PRIVATE', 1);
use Drupal\og_access\OgAccess;

/**
* Implements hook_node_grants().
Expand All @@ -58,7 +34,7 @@ function og_access_node_grants(AccountInterface $account, $op) {
foreach ($groups as $group_type => $entity_groups) {
/** @var \Drupal\core\Entity\EntityInterface $group */
foreach ($entity_groups as $group) {
$realm = OG_ACCESS_REALM . ':' . $group_type;
$realm = OgAccess::OG_ACCESS_REALM . ':' . $group_type;
$grants[$realm][] = $group->id();
}
}
Expand All @@ -72,30 +48,30 @@ function og_access_node_grants(AccountInterface $account, $op) {
*/
function og_access_node_access_records(NodeInterface $node) {
if (!$node->isPublished()) {
// Node is unpublished, so we don't allow every group member to see it.
// Node is unpublished, so we don't allow any group member to see it.
return [];
}

// The group IDs, that in case access is granted, will be recorded.
$gids = [];

if (Og::isGroup('node', $node->getType()) &&
$node->hasField(OG_ACCESS_FIELD) &&
!empty($node->{OG_ACCESS_FIELD}) && $node->{OG_ACCESS_FIELD}->value) {
$node->hasField(OgAccess::OG_ACCESS_FIELD) &&
!empty($node->{OgAccess::OG_ACCESS_FIELD}) && $node->{OgAccess::OG_ACCESS_FIELD}->value) {
// Private group.
$gids['node'][] = $node->id();
}

if ($node->hasField(OG_ACCESS_CONTENT_FIELD) &&
!empty($node->get(OG_ACCESS_CONTENT_FIELD))) {
$content_access = $node->get(OG_ACCESS_CONTENT_FIELD)->value;
if ($node->hasField(OgAccess::OG_ACCESS_CONTENT_FIELD) &&
!empty($node->get(OgAccess::OG_ACCESS_CONTENT_FIELD))) {
$content_access = $node->get(OgAccess::OG_ACCESS_CONTENT_FIELD)->value;
}
else {
$content_access = OG_ACCESS_PUBLIC;
$content_access = OgAccess::OG_ACCESS_PUBLIC;
}

switch ($content_access) {
case OG_ACCESS_PUBLIC:
case OgAccess::OG_ACCESS_PUBLIC:
// Skip non-group content nodes.
if (!Og::isGroupContent('node', $node->getType())) {
break;
Expand All @@ -114,8 +90,8 @@ function og_access_node_access_records(NodeInterface $node) {
continue;
}

if ($group->hasField(OG_ACCESS_FIELD) && !empty($group->get(OG_ACCESS_FIELD)) &&
$group->get(OG_ACCESS_FIELD)->value) {
if ($group->hasField(OgAccess::OG_ACCESS_FIELD) && !empty($group->get(OgAccess::OG_ACCESS_FIELD)) &&
$group->get(OgAccess::OG_ACCESS_FIELD)->value) {
$has_private = TRUE;
}
}
Expand All @@ -125,7 +101,7 @@ function og_access_node_access_records(NodeInterface $node) {
}
break;

case OG_ACCESS_PRIVATE:
case OgAccess::OG_ACCESS_PRIVATE:
$list_gids = [];
/** @var \Drupal\og\OgGroupAudienceHelper $audience_helper */
$audience_helper = \Drupal::service('og.group_audience_helper');
Expand All @@ -142,7 +118,7 @@ function og_access_node_access_records(NodeInterface $node) {
foreach ($gids as $group_type => $values) {
foreach ($values as $gid) {
$grants[] = [
'realm' => OG_ACCESS_REALM . ':' . $group_type,
'realm' => OgAccess::OG_ACCESS_REALM . ':' . $group_type,
'gid' => $gid,
'grant_view' => 1,
'grant_update' => 0,
Expand Down Expand Up @@ -198,9 +174,9 @@ function og_access_entity_type_save(EntityInterface $entity) {
// Add/remove on the group itself.
$is_group = Og::isGroup($entity_type_id, $bundle);
if ($entity->og_is_group || $is_group) {
$field = FieldConfig::loadByName($entity_type_id, $bundle, OG_ACCESS_FIELD);
$field = FieldConfig::loadByName($entity_type_id, $bundle, OgAccess::OG_ACCESS_FIELD);
if (!$field && $enable_og_access) {
Og::createField(OG_ACCESS_FIELD, $entity_type_id, $bundle);
Og::createField(OgAccess::OG_ACCESS_FIELD, $entity_type_id, $bundle);
}
elseif ($field) {
if (!$enable_og_access || $is_group && !$entity->og_is_group) {
Expand All @@ -212,10 +188,10 @@ function og_access_entity_type_save(EntityInterface $entity) {
// Add remove the relevant field to the group content bundle.
$is_group_content = Og::isGroupContent($entity_type_id, $bundle);
if ($entity->og_group_content_bundle || $is_group_content) {
$field = FieldConfig::loadByName($entity_type_id, $bundle, OG_ACCESS_CONTENT_FIELD);
$field = FieldConfig::loadByName($entity_type_id, $bundle, OgAccess::OG_ACCESS_CONTENT_FIELD);

if (!$field && $enable_og_access) {
Og::createField(OG_ACCESS_CONTENT_FIELD, $entity_type_id, $bundle);
Og::createField(OgAccess::OG_ACCESS_CONTENT_FIELD, $entity_type_id, $bundle);
}
elseif ($field) {
if (!$enable_og_access || $is_group_content && !$entity->og_group_content_bundle) {
Expand Down
35 changes: 35 additions & 0 deletions og_access/src/OgAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Drupal\og_access;

/**
* Helper class for constants.
*/
class OgAccess {

/**
* The access realm of group member.
*/
const OG_ACCESS_REALM = 'og_access';

/**
* Group public access field.
*/
const OG_ACCESS_FIELD = 'group_access';

/**
* Group public access field.
*/
const OG_ACCESS_CONTENT_FIELD = 'group_content_access';

/**
* Public group/group content access.
*/
const OG_ACCESS_PUBLIC = 0;

/**
* Private group/group content access.
*/
const OG_ACCESS_PRIVATE = 1;

}
17 changes: 11 additions & 6 deletions og_access/src/OgAccessBundleFormAlter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\og\Og;
use Drupal\Core\StringTranslation\TranslationInterface;

/**
* Helper for og_access_form_alter().
*/
class OgAccessBundleFormAlter {
use StringTranslationTrait;

/**
* The entity bundle.
Expand Down Expand Up @@ -37,9 +39,12 @@ class OgAccessBundleFormAlter {
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
* @param Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation object.
*/
public function __construct(EntityInterface $entity) {
public function __construct(EntityInterface $entity, TranslationInterface $string_translation) {
$this->entity = $entity;
$this->stringTranslation = $string_translation;
}

/**
Expand All @@ -59,8 +64,8 @@ public function formAlter(array &$form, FormStateInterface $form_state) {

$form['og']['og_enable_access'] = [
'#type' => 'checkbox',
'#title' => t('Restrict access to group members'),
'#description' => t('Enable OG access control. Provides a new field that determines the group/group content visibility. Public groups can have member-only content. Any public group content belonging to a private group will be restricted to the members of that group only.'),
'#title' => $this->t('Restrict access to group members'),
'#description' => $this->t('Enable OG access control. Provides a new field that determines the group/group content visibility. Public groups can have member-only content. Any public group content belonging to a private group will be restricted to the members of that group only.'),
'#default_value' => $this->bundle ? $this->hasAccessControl() : FALSE,
'#states' => [
'visible' => [
Expand All @@ -75,7 +80,7 @@ public function formAlter(array &$form, FormStateInterface $form_state) {
* Checks whether the existing bundle has OG access control enabled.
*
* @return bool
* True if the group bundle has the OG_ACCESS_FIELD field -OR-
* True if the group bundle has the OgAccess::OG_ACCESS_FIELD field -OR-
* if the group content bundle has the OG_CONTENT_ACCESS_FIELD field.
* False otherwise.
*/
Expand All @@ -84,11 +89,11 @@ protected function hasAccessControl() {
->getFieldDefinitions($this->entityTypeId, $this->bundle);

if (Og::isGroup($this->entityTypeId, $this->bundle)) {
return isset($field_definitions[OG_ACCESS_FIELD]);
return isset($field_definitions[OgAccess::OG_ACCESS_FIELD]);
}

if (Og::isGroupContent($this->entityTypeId, $this->bundle)) {
return isset($field_definitions[OG_ACCESS_CONTENT_FIELD]);
return isset($field_definitions[OgAccess::OG_ACCESS_CONTENT_FIELD]);
}

return FALSE;
Expand Down
4 changes: 2 additions & 2 deletions og_access/src/Plugin/OgFields/OgAccessField.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function getFieldStorageBaseDefinition(array $values = []) {
'cardinality' => 1,
'settings' => [
'allowed_values' => [
0 => 'Public - accessible to all site users',
1 => 'Private - accessible only to group members',
0 => $this->t('Public - accessible to all site users'),
1 => $this->t('Private - accessible only to group members'),
],
'allowed_values_function' => '',
],
Expand Down
6 changes: 3 additions & 3 deletions og_access/src/Plugin/OgFields/OgContentAccessField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Determine the group content visibility.
*
* @OgFields(
* id = OG_ACCESS_CONTENT_FIELD,
* id = OgAccess::OG_ACCESS_CONTENT_FIELD,
* type = "node",
* description = @Translation("Determine the group content visibility.")
* )
Expand All @@ -24,8 +24,8 @@ public function getFieldStorageBaseDefinition(array $values = []) {
'cardinality' => 1,
'settings' => [
'allowed_values' => [
0 => 'Public - accessible to all site users',
1 => 'Private - accessible only to group members',
0 => $this->t('Public - accessible to all site users'),
1 => $this->t('Private - accessible only to group members'),
],
'allowed_values_function' => '',
],
Expand Down

0 comments on commit 479f8ed

Please sign in to comment.