Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Commit

Permalink
Merge branch '8.x-1.x' into neutral-access-result
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrenssen authored Aug 18, 2020
2 parents cc0a723 + f90d663 commit 79ba576
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Controller/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ public function subscribe($entity_type_id, EntityInterface $group, OgMembershipT
return new RedirectResponse($group->toUrl()->setAbsolute(TRUE)->toString());
}

if (!$this->ogAccess->userAccess($group, 'subscribe', $user) && !$this->ogAccess->userAccess($group, 'subscribe without approval', $user)) {
$subscribe = $this->ogAccess->userAccess($group, 'subscribe');
$subscribe_without_approval = $this->ogAccess->userAccess($group, 'subscribe without approval');
if (!$subscribe->isAllowed() && !$subscribe_without_approval->isAllowed()) {
throw new AccessDeniedHttpException();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Event/AccessEventBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public function getUser(): AccountInterface {
public function getAccessResult(): AccessResultInterface {
$access = $this->access;

// Enrich the access result object with our cacheability metadata in case it
// supports it.
if ($access instanceof RefinableCacheableDependencyInterface) {
$access->addCacheableDependency($this);
}
Expand Down
49 changes: 45 additions & 4 deletions tests/src/Functional/GroupSubscribeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class GroupSubscribeTest extends BrowserTestBase {
*/
protected $group4;

/**
* Test entity group.
*
* @var \Drupal\node\NodeInterface
*/
protected $group5;

/**
* A group bundle name.
*
Expand All @@ -76,6 +83,13 @@ class GroupSubscribeTest extends BrowserTestBase {
*/
protected $groupBundle2;

/**
* A group bundle name.
*
* @var string
*/
protected $groupBundle3;

/**
* A membership type bundle name.
*
Expand Down Expand Up @@ -108,6 +122,8 @@ protected function setUp(): void {
NodeType::create(['type' => $this->groupBundle1])->save();
$this->groupBundle2 = mb_strtolower($this->randomMachineName());
NodeType::create(['type' => $this->groupBundle2])->save();
$this->groupBundle3 = mb_strtolower($this->randomMachineName());
NodeType::create(['type' => $this->groupBundle3])->save();
$this->nonGroupBundle = mb_strtolower($this->randomMachineName());
NodeType::create(['type' => $this->nonGroupBundle])->save();
$this->membershipTypeBundle = mb_strtolower($this->randomMachineName());
Expand All @@ -116,26 +132,30 @@ protected function setUp(): void {
// Define the entities as groups.
Og::groupTypeManager()->addGroup('node', $this->groupBundle1);
Og::groupTypeManager()->addGroup('node', $this->groupBundle2);
Og::groupTypeManager()->addGroup('node', $this->groupBundle3);

// Create node author user.
$user = $this->createUser();

// Create groups.
// Create test groups. The first group has the 'subscribe without approval'
// permission.
$this->group1 = Node::create([
'type' => $this->groupBundle1,
'title' => $this->randomString(),
'uid' => $user->id(),
]);
$this->group1->save();

// A group which is using default permissions; it grants the 'subscribe'
// permission to non-members.
$this->group2 = Node::create([
'type' => $this->groupBundle2,
'title' => $this->randomString(),
'uid' => $user->id(),
]);
$this->group2->save();

// Create an unpublished node.
// An unpublished group.
$this->group3 = Node::create([
'type' => $this->groupBundle1,
'title' => $this->randomString(),
Expand All @@ -152,12 +172,26 @@ protected function setUp(): void {
]);
$this->group4->save();

$role = OgRole::getRole('node', $this->groupBundle1, OgRoleInterface::ANONYMOUS);
// A group which is closed for subscription. It grants neither 'subscribe'
// nor 'subscribe without approval'.
$this->group5 = Node::create([
'type' => $this->groupBundle3,
'title' => $this->randomString(),
'uid' => $user->id(),
]);
$this->group5->save();

$role
// Grant the permission to 'subscribe without approval' to the first group
// type.
OgRole::getRole('node', $this->groupBundle1, OgRoleInterface::ANONYMOUS)
->grantPermission('subscribe without approval')
->save();

// Revoke the permission to subscribe from the third group type.
OgRole::getRole('node', $this->groupBundle3, OgRoleInterface::ANONYMOUS)
->revokePermission('subscribe')
->save();

// Create a new membership type.
$membership_type = OgMembershipType::create([
'type' => $this->membershipTypeBundle,
Expand Down Expand Up @@ -222,6 +256,13 @@ public function testSubscribeAccess() {
'entity' => $this->group4,
'code' => 403,
],

// A group which doesn't allow new subscriptions.
[
'entity' => $this->group5,
'code' => 403,
],

// A non existing entity type.
[
'entity_type_id' => mb_strtolower($this->randomMachineName()),
Expand Down

0 comments on commit 79ba576

Please sign in to comment.