forked from Gizra/og
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Gizra#47 from amitaibu/44-node-permission
Add group content type node specific permissions
- Loading branch information
Showing
5 changed files
with
104 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |