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

Commit

Permalink
Do not needlessly load OgRole objects when only the role name is requ…
Browse files Browse the repository at this point in the history
…ired.
  • Loading branch information
pfrenssen committed Jul 23, 2019
1 parent 0074d58 commit 983a407
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
10 changes: 7 additions & 3 deletions src/Cache/Context/OgRoleCacheContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,13 @@ public function getContext() {
if (empty($this->hashes[$this->user->id()])) {
$memberships = [];
foreach ($this->membershipManager->getMemberships($this->user->id()) as $membership) {
$role_names = array_map(function (OgRoleInterface $role) {
return $role->getName();
}, $membership->getRoles());
// Derive the role names from the role IDs. This is faster than loading
// the OgRole object from the membership.
$role_names = array_map(function (string $role_id) use ($membership): string {
$pattern = preg_quote("{$membership->getGroupEntityType()}-{$membership->getGroupBundle()}-");
preg_match("/$pattern(.+)/", $role_id, $matches);
return $matches[1];
}, $membership->getRolesIds());
if ($role_names) {
$memberships[$membership->getGroupEntityType()][$membership->getGroupId()] = $role_names;
}
Expand Down
14 changes: 11 additions & 3 deletions src/Entity/OgMembership.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,17 @@ public function setRoles(array $roles = []): OgMembershipInterface {
* {@inheritdoc}
*/
public function getRolesIds(): array {
return array_map(function (OgRole $role) {
return $role->id();
}, $this->getRoles());
$roles_ids = array_map(function (array $role_data): string {
return $role_data['target_id'];
}, $this->get('roles')->getValue());

// Add the member role. This is only possible if a group has been set on the
// membership.
if ($this->hasGroup()) {
$roles_ids[] = "{$this->getGroupEntityType()}-{$this->getGroupBundle()}-" . OgRoleInterface::AUTHENTICATED;
}

return $roles_ids;
}

/**
Expand Down
16 changes: 6 additions & 10 deletions tests/src/Unit/Cache/Context/OgRoleCacheContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ public function testMembershipsWithOrphanedRole() {
// group entity type, but no roles.
/** @var \Drupal\og\OgMembershipInterface|\Prophecy\Prophecy\ObjectProphecy $membership */
$membership = $this->prophesize(OgMembershipInterface::class);
$membership->getGroupEntityType()->willReturn('test_entity');
$membership->getGroupId()->willReturn('test_id');
$membership->getRoles()->willReturn([]);
$membership->getRolesIds()->willReturn([]);

// The membership with the orphaned role will be returned by the membership
// manager.
Expand Down Expand Up @@ -137,19 +135,17 @@ public function testMemberships(array $group_memberships, array $expected_identi
$memberships[$user_id] = [];
foreach ($group_entity_type_ids as $group_entity_type_id => $group_ids) {
foreach ($group_ids as $group_id => $roles) {
// Mock the role objects that will be contained in the memberships.
$roles = array_map(function ($role_name) {
/** @var \Drupal\og\OgRoleInterface|\Prophecy\Prophecy\ObjectProphecy $role */
$role = $this->prophesize(OgRoleInterface::class);
$role->getName()->willReturn($role_name);
return $role->reveal();
// Construct the role IDs that will be returned by the membership.
$roles_ids = array_map(function (string $role_name) use ($group_entity_type_id) {
return "{$group_entity_type_id}-bundle-{$role_name}";
}, $roles);
// Mock the expected returns of method calls on the membership.
/** @var \Drupal\og\OgMembershipInterface|\Prophecy\Prophecy\ObjectProphecy $membership */
$membership = $this->prophesize(OgMembershipInterface::class);
$membership->getGroupEntityType()->willReturn($group_entity_type_id);
$membership->getGroupBundle()->willReturn('bundle');
$membership->getGroupId()->willReturn($group_id);
$membership->getRoles()->willReturn($roles);
$membership->getRolesIds()->willReturn($roles_ids);
$memberships[$user_id][++$membership_id] = $membership->reveal();
}
}
Expand Down

0 comments on commit 983a407

Please sign in to comment.