Skip to content

Commit

Permalink
Merge pull request Gizra#47 from amitaibu/44-node-permission
Browse files Browse the repository at this point in the history
Add group content type node specific permissions
  • Loading branch information
amitaibu committed Dec 6, 2015
2 parents 3dcbf38 + 597a887 commit 8577ee5
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 19 deletions.
18 changes: 0 additions & 18 deletions og.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@
* @{
*/

/**
* Add group permissions.
*/
function hook_og_permission() {
return array(
'subscribe' => array(
'title' => t('Subscribe user to group'),
'description' => t("Allow user to be a member of a group (approval required)."),
// Determine to which role to limit the permission. For example the
// "subscribe" can't be assigned only to a non-member, as a member doesn't
// need it.
'roles' => array(Og::ANONYMOUS_ROLE),
// Determine to which roles the permissions will be enabled by default.
'default roles' => array(Og::ANONYMOUS_ROLE),
),
);
}

/**
* Alter the organic groups permissions.
*
Expand Down
3 changes: 3 additions & 0 deletions og.og_permissions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ administer group:
'restrict access': TRUE
'default roles':
- ADMINISTRATOR_ROLE

permission_callbacks:
- \Drupal\og\OgNodePermissions::nodeTypePermissions
42 changes: 42 additions & 0 deletions src/OgNodePermissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @file
* Contains \Drupal\og\OgNodePermissions.
*/

namespace Drupal\og;

use Drupal\node\Entity\NodeType;
use Drupal\node\NodePermissions;

/**
* Provides dynamic groups permissions for node group content types.
*/
class OgNodePermissions extends NodePermissions {

/**
* Returns an array of node group content type permissions.
*
* @return array
* The node type permissions.
*
* @see \Drupal\user\PermissionHandlerInterface::getPermissions()
*/
public function nodeTypePermissions() {
$perms = array();

// Generate node permissions for all group content node types.
foreach (NodeType::loadMultiple() as $bundle) {

if (!Og::isGroupContent('node', $bundle->id())) {
continue;
}

$perms += $this->buildPermissions($bundle);
}

return $perms;
}

}
2 changes: 1 addition & 1 deletion src/OgPermissionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Drupal\user\PermissionHandlerInterface;

/**
* Provides permissions for groups based on YNL files.
* Provides permissions for groups based on YAML files.
*
* The permissions file should be constructed by the next format(with comments):
* @code
Expand Down
58 changes: 58 additions & 0 deletions tests/src/Kernel/Entity/OgNodePermissionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* @file
* Contains \Drupal\Tests\og\Kernel\Entity\OgNodePermissionsTest.
*/

namespace Drupal\Tests\og\Kernel\Entity;

use Drupal\Component\Utility\Unicode;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\og\Og;
use Drupal\og\OgGroupAudienceHelper;

/**
* @group og
*/
class OgNodePermissionsTest extends KernelTestBase {

/**
* {@inheritdoc}
*/
public static $modules = ['node', 'field', 'og', 'system', 'user'];

/**
* Testing group content node permissions.
*/
public function testGetPermissions() {
// Create a node group content.
$bundle = NodeType::create([
'type' => Unicode::strtolower($this->randomMachineName()),
'name' => $this->randomString(),
]);

$bundle->save();
Og::CreateField(OgGroupAudienceHelper::DEFAULT_FIELD, 'node', $bundle->id());

$name = $bundle->id();
$expected = [
'administer group',
'update group',
"create $name content",
"delete any $name content",
"delete own $name content",
"delete $name revisions",
"edit any $name content",
"edit own $name content",
"revert $name revisions",
"view $name revisions",
];

$permissions = Og::permissionHandler()->getPermissions();

$this->assertEquals($expected, array_keys($permissions));
}

}

0 comments on commit 8577ee5

Please sign in to comment.