Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions src/GroupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
namespace Drupal\og;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\og\Entity\OgRole;
use Drupal\og\Event\PermissionEvent;
use Drupal\og\Event\PermissionEventInterface;
use Drupal\og\Exception\OgRoleException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
Expand Down Expand Up @@ -282,17 +284,31 @@ public function refresh() {
}

/**
* Creates default roles for the given group type.
* Creates per bundle or per group OG roles.
*
* @param string $entity_type_id
* The entity type ID of the group for which to create default roles.
* @param string $bundle_id
* The bundle ID of the group for which to create default roles.
* @param string|null $bundle_id
* (Optional) The bundle ID of the group for which to create default roles.
* When this property is set it means the roles will be "global" roles
* that apply to any group of the bundle ID. Defaults to NULL.
* @param string|null $group_id
* (Optional) The group ID of the group for which to create default roles.
* When this property is set it means the roles will be "per group" roles
* and apply only to this specific group ID. Defaults to NULL.
*
* @see \Drupal\og\GroupManager::createPerBundleRoles()
* @see \Drupal\og\GroupManager::createPerGroupRoles()
*/
protected function createRoles($entity_type_id, $bundle_id) {
protected function createRoles($entity_type_id, $bundle_id = NULL, $group_id = NULL) {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pfrenssen I guess it is a bit ugly having to pass either bundle ID or group ID. Which made me thing that maybe we should have two Og Roles bundles: per_bundle and per_group so the schema will just have those two keys: group_type and reference_id (bad name) . reference_id will be populated either with the bundle ID or the group ID.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OgRole is a config entity, I don't think this supports bundles. If you look at the inherited method OgRole::bundle() you see it returns the entity type instead of the bundle. Only content entities support custom bundles out of the box.

But I actually think we can hack in bundle support easily. We just need to ensure that the bundle is stored along with the entity, that we have a $this->bundle property that we populate in the constructor, and then we can use it to handle per_bundle and per_group specific logic.

if (!$bundle_id && !$group_id) {
throw new OgRoleException('Cannot create OG roles without either bundle ID or the group ID being set.');
}

$properties = [
'group_type' => $entity_type_id,
'group_bundle' => $bundle_id,
'group_id' => $group_id,
];
foreach ([OgRoleInterface::ANONYMOUS, OgRoleInterface::AUTHENTICATED, OgRoleInterface::ADMINISTRATOR] as $role_name) {
$properties['id'] = $role_name;
Expand All @@ -309,6 +325,31 @@ protected function createRoles($entity_type_id, $bundle_id) {
}
}


/**
* Creates per bundle OG roles.
*
* @param string $entity_type_id
* The entity type ID of the group for which to create default roles.
* @param string $bundle_id
* The bundle ID of the group for which to create default roles.
*/
protected function createPerBundleRoles($entity_type_id, $bundle_id) {
$this->createRoles($entity_type_id, $bundle_id);
}


/**
* Creates per group OG roles.
*
* @param Drupal\Core\Entity\EntityInterface $group
* The group entity.
*/
protected function createPerGroupRoles(EntityInterface $group) {
$entity_type_id = $group->getEntityTypeId();
$this->createRoles($entity_type_id, NULL, $group->id());
}

/**
* Deletes the roles associated with a group type.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/**
* @file
* Contains \Drupal\og\Plugin\OgFields\AccessField.
*/

namespace Drupal\og\Plugin\OgFields;

Expand All @@ -15,27 +11,19 @@
* Determine if group should use default roles and permissions.
*
* @OgFields(
* id = OG_DEFAULT_ACCESS_FIELD,
* id = "roles_override",
* type = "group",
* description = @Translation("Determine if group should use default roles and permissions.")
* )
*/
class AccessField extends OgFieldBase implements OgFieldsInterface {
class RolesOverride extends OgFieldBase implements OgFieldsInterface {

/**
* {@inheritdoc}
*/
public function getFieldStorageBaseDefinition(array $values = []) {
$values += [
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings' => [
'allowed_values' => [
0 => 'Use default roles and permissions',
1 => 'Override default roles and permissions',
],
'allowed_values_function' => '',
],
'type' => 'list_integer',
'type' => 'boolean',
];

return parent::getFieldStorageBaseDefinition($values);
Expand All @@ -46,11 +34,9 @@ public function getFieldStorageBaseDefinition(array $values = []) {
*/
public function getFieldBaseDefinition(array $values = []) {
$values += [
'default_value' => [0 => ['value' => 0]],
'description' => $this->t('Determine if group should use default roles and permissions.'),
'display_label' => TRUE,
'label' => $this->t('Group roles and permissions'),
'required' => TRUE,
'description' => $this->t('Determine if group should override the default roles and permissions.'),
'label' => $this->t('Group roles'),
'required' => FALSE,
];

return parent::getFieldBaseDefinition($values);
Expand All @@ -61,8 +47,7 @@ public function getFieldBaseDefinition(array $values = []) {
*/
public function getFormDisplayDefinition(array $values = []) {
$values += [
'type' => 'options_select',
'settings' => [],
'type' => 'boolean_checkbox',
];


Expand All @@ -74,7 +59,7 @@ public function getFormDisplayDefinition(array $values = []) {
*/
public function getViewDisplayDefinition(array $values = []) {
$values += [
'type' => 'list_default',
'type' => 'boolean',
'label' => 'above',
];

Expand Down