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

Do not require to pass the full user object when only the ID is required #541

Merged
merged 4 commits into from
Jul 23, 2019
Merged
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
2 changes: 1 addition & 1 deletion og.module
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function og_entity_predelete(EntityInterface $entity) {
if ($entity instanceof UserInterface) {
/** @var \Drupal\og\MembershipManagerInterface $membership_manager */
$membership_manager = \Drupal::service('og.membership_manager');
foreach ($membership_manager->getMemberships($entity, []) as $membership) {
foreach ($membership_manager->getMemberships($entity->id(), []) as $membership) {
$membership->delete();
}
}
Expand Down
7 changes: 6 additions & 1 deletion phpcs-ruleset.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
<exclude-pattern>*.min.css</exclude-pattern>
<exclude-pattern>*.min.js</exclude-pattern>

<rule ref="./vendor/drupal/coder/coder_sniffer/Drupal" />
<rule ref="./vendor/drupal/coder/coder_sniffer/Drupal">
<!-- URLs in deprecation messages point to Github rather than drupal.org. -->
<exclude name="Drupal.Semantics.FunctionTriggerError.TriggerErrorSeeUrlFormat" />
<!-- Deprecation versions are allowed to include beta versions. -->
<exclude name="Drupal.Semantics.FunctionTriggerError.TriggerErrorVersion" />
</rule>
<rule ref="./vendor/drupal/coder/coder_sniffer/DrupalPractice">
<!-- https://github.com/Gizra/og/issues/549 -->
<exclude name="DrupalPractice.CodeAnalysis.VariableAnalysis.UndefinedVariable" />
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/Context/OgMembershipStateCacheContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function getContext() {
}

/** @var \Drupal\og\OgMembershipInterface $membership */
$membership = $this->membershipManager->getMembership($group, $this->user, OgMembershipInterface::ALL_STATES);
$membership = $this->membershipManager->getMembership($group, $this->user->id(), OgMembershipInterface::ALL_STATES);
return $membership ? $membership->getState() : self::NO_CONTEXT;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Cache/Context/OgRoleCacheContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function getContext() {
// compute the hash once.
if (empty($this->hashes[$this->user->id()])) {
$memberships = [];
foreach ($this->membershipManager->getMemberships($this->user) as $membership) {
foreach ($this->membershipManager->getMemberships($this->user->id()) as $membership) {
$role_names = array_map(function (OgRoleInterface $role) {
return $role->getName();
}, $membership->getRoles());
Expand Down
84 changes: 65 additions & 19 deletions src/MembershipManager.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);
Copy link
Collaborator

@MPParsley MPParsley Jul 22, 2019

Choose a reason for hiding this comment

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

Curious to see if this will break any beta blockers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Me too, that's why I added it, in the current code base all tests are passing with strict types! 👍


namespace Drupal\og;

use Drupal\Component\Utility\NestedArray;
Expand Down Expand Up @@ -60,11 +62,15 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, OgG
/**
* {@inheritdoc}
*/
public function getUserGroupIds(AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
public function getUserGroupIds($user_id, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
if ($user_id instanceof AccountInterface) {
trigger_error('Passing an account object is deprecated in og:8.1.0-alpha4 and is removed from og:8.1.0-beta1. Instead pass the user ID as an integer value. See https://github.com/Gizra/og/issues/542', E_USER_DEPRECATED);
$user_id = $user_id->id();
}
$group_ids = [];

/** @var \Drupal\og\Entity\OgMembership[] $memberships */
$memberships = $this->getMemberships($user, $states);
$memberships = $this->getMemberships($user_id, $states);
foreach ($memberships as $membership) {
$group_ids[$membership->getGroupEntityType()][] = $membership->getGroupId();
}
Expand All @@ -75,22 +81,32 @@ public function getUserGroupIds(AccountInterface $user, array $states = [OgMembe
/**
* {@inheritdoc}
*/
public function getUserGroups(AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
$group_ids = $this->getUserGroupIds($user, $states);
public function getUserGroups($user_id, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
if ($user_id instanceof AccountInterface) {
trigger_error('Passing an account object is deprecated in og:8.1.0-alpha4 and is removed from og:8.1.0-beta1. Instead pass the user ID as an integer value. See https://github.com/Gizra/og/issues/542', E_USER_DEPRECATED);
$user_id = $user_id->id();
}

$group_ids = $this->getUserGroupIds($user_id, $states);
return $this->loadGroups($group_ids);
}

/**
* {@inheritdoc}
*/
public function getMemberships(AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
public function getMemberships($user_id, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
if ($user_id instanceof AccountInterface) {
trigger_error('Passing an account object is deprecated in og:8.1.0-alpha4 and is removed from og:8.1.0-beta1. Instead pass the user ID as an integer value. See https://github.com/Gizra/og/issues/542', E_USER_DEPRECATED);
$user_id = $user_id->id();
}

// When an empty array is passed, retrieve memberships with all possible
// states.
$states = $this->prepareConditionArray($states, OgMembership::ALL_STATES);

$cid = [
__METHOD__,
$user->id(),
$user_id,
implode('|', $states),
];
$cid = implode(':', $cid);
Expand All @@ -100,7 +116,7 @@ public function getMemberships(AccountInterface $user, array $states = [OgMember
$query = $this->entityTypeManager
->getStorage('og_membership')
->getQuery()
->condition('uid', $user->id())
->condition('uid', $user_id)
->condition('state', $states, 'IN');

$membership_ids = $query->execute();
Expand All @@ -113,8 +129,13 @@ public function getMemberships(AccountInterface $user, array $states = [OgMember
/**
* {@inheritdoc}
*/
public function getMembership(EntityInterface $group, AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
foreach ($this->getMemberships($user, $states) as $membership) {
public function getMembership(EntityInterface $group, $user_id, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
if ($user_id instanceof AccountInterface) {
trigger_error('Passing an account object is deprecated in og:8.1.0-alpha4 and is removed from og:8.1.0-beta1. Instead pass the user ID as an integer value. See https://github.com/Gizra/og/issues/542', E_USER_DEPRECATED);
$user_id = $user_id->id();
}

foreach ($this->getMemberships($user_id, $states) as $membership) {
if ($membership->getGroupEntityType() === $group->getEntityTypeId() && $membership->getGroupId() === $group->id()) {
return $membership;
}
Expand All @@ -127,9 +148,14 @@ public function getMembership(EntityInterface $group, AccountInterface $user, ar
/**
* {@inheritdoc}
*/
public function getUserGroupIdsByRoleIds(AccountInterface $user, array $role_ids, array $states = [OgMembershipInterface::STATE_ACTIVE], bool $require_all_roles = TRUE): array {
public function getUserGroupIdsByRoleIds($user_id, array $role_ids, array $states = [OgMembershipInterface::STATE_ACTIVE], bool $require_all_roles = TRUE): array {
if ($user_id instanceof AccountInterface) {
trigger_error('Passing an account object is deprecated in og:8.1.0-alpha4 and is removed from og:8.1.0-beta1. Instead pass the user ID as an integer value. See https://github.com/Gizra/og/issues/542', E_USER_DEPRECATED);
$user_id = $user_id->id();
}

/** @var \Drupal\og\OgMembershipInterface[] $memberships */
$memberships = $this->getMemberships($user, $states);
$memberships = $this->getMemberships($user_id, $states);
$memberships = array_filter($memberships, function (OgMembershipInterface $membership) use ($role_ids, $require_all_roles): bool {
$membership_roles_ids = $membership->getRolesIds();
return $require_all_roles ? empty(array_diff($role_ids, $membership_roles_ids)) : !empty(array_intersect($membership_roles_ids, $role_ids));
Expand All @@ -145,8 +171,13 @@ public function getUserGroupIdsByRoleIds(AccountInterface $user, array $role_ids
/**
* {@inheritdoc}
*/
public function getUserGroupsByRoleIds(AccountInterface $user, array $role_ids, array $states = [OgMembershipInterface::STATE_ACTIVE], bool $require_all_roles = TRUE): array {
$group_ids = $this->getUserGroupIdsByRoleIds($user, $role_ids, $states, $require_all_roles);
public function getUserGroupsByRoleIds($user_id, array $role_ids, array $states = [OgMembershipInterface::STATE_ACTIVE], bool $require_all_roles = TRUE): array {
if ($user_id instanceof AccountInterface) {
trigger_error('Passing an account object is deprecated in og:8.1.0-alpha4 and is removed from og:8.1.0-beta1. Instead pass the user ID as an integer value. See https://github.com/Gizra/og/issues/542', E_USER_DEPRECATED);
$user_id = $user_id->id();
}

$group_ids = $this->getUserGroupIdsByRoleIds($user_id, $role_ids, $states, $require_all_roles);
return $this->loadGroups($group_ids);
}

Expand Down Expand Up @@ -383,24 +414,39 @@ public function getGroupContentIds(EntityInterface $entity, array $entity_types
/**
* {@inheritdoc}
*/
public function isMember(EntityInterface $group, AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
$group_ids = $this->getUserGroupIds($user, $states);
public function isMember(EntityInterface $group, $user_id, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
if ($user_id instanceof AccountInterface) {
trigger_error('Passing an account object is deprecated in og:8.1.0-alpha4 and is removed from og:8.1.0-beta1. Instead pass the user ID as an integer value. See https://github.com/Gizra/og/issues/542', E_USER_DEPRECATED);
$user_id = $user_id->id();
}

$group_ids = $this->getUserGroupIds($user_id, $states);
$entity_type_id = $group->getEntityTypeId();
return !empty($group_ids[$entity_type_id]) && in_array($group->id(), $group_ids[$entity_type_id]);
}

/**
* {@inheritdoc}
*/
public function isMemberPending(EntityInterface $group, AccountInterface $user) {
return $this->isMember($group, $user, [OgMembershipInterface::STATE_PENDING]);
public function isMemberPending(EntityInterface $group, $user_id) {
if ($user_id instanceof AccountInterface) {
trigger_error('Passing an account object is deprecated in og:8.1.0-alpha4 and is removed from og:8.1.0-beta1. Instead pass the user ID as an integer value. See https://github.com/Gizra/og/issues/542', E_USER_DEPRECATED);
$user_id = $user_id->id();
}

return $this->isMember($group, $user_id, [OgMembershipInterface::STATE_PENDING]);
}

/**
* {@inheritdoc}
*/
public function isMemberBlocked(EntityInterface $group, AccountInterface $user) {
return $this->isMember($group, $user, [OgMembershipInterface::STATE_BLOCKED]);
public function isMemberBlocked(EntityInterface $group, $user_id) {
if ($user_id instanceof AccountInterface) {
trigger_error('Passing an account object is deprecated in og:8.1.0-alpha4 and is removed from og:8.1.0-beta1. Instead pass the user ID as an integer value. See https://github.com/Gizra/og/issues/542', E_USER_DEPRECATED);
$user_id = $user_id->id();
}

return $this->isMember($group, $user_id, [OgMembershipInterface::STATE_BLOCKED]);
}

/**
Expand Down
54 changes: 27 additions & 27 deletions src/MembershipManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ interface MembershipManagerInterface {
* to the group is stored on a field attached to the content entity, while
* user memberships are tracked in OgMembership entities.
*
* @param \Drupal\Core\Session\AccountInterface $user
* The user to get groups for.
* @param int $user_id
* The ID of the user to get groups for.
* @param array $states
* (optional) Array with the state to return. Defaults to active.
*
Expand All @@ -30,7 +30,7 @@ interface MembershipManagerInterface {
*
* @see \Drupal\og\MembershipManager::getGroupIds()
*/
public function getUserGroupIds(AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]);
public function getUserGroupIds($user_id, array $states = [OgMembershipInterface::STATE_ACTIVE]);

/**
* Returns all groups associated with the given user.
Expand All @@ -41,8 +41,8 @@ public function getUserGroupIds(AccountInterface $user, array $states = [OgMembe
* on a field attached to the content entity, while user memberships are
* tracked in OgMembership entities.
*
* @param \Drupal\Core\Session\AccountInterface $user
* The user to get groups for.
* @param int $user_id
* The ID of the user to get groups for.
* @param string[] $states
* (optional) Array with the states to return. Defaults to active.
*
Expand All @@ -53,13 +53,13 @@ public function getUserGroupIds(AccountInterface $user, array $states = [OgMembe
* @see \Drupal\og\MembershipManager::getGroups()
* @see \Drupal\og\MembershipManager::getMemberships()
*/
public function getUserGroups(AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]);
public function getUserGroups($user_id, array $states = [OgMembershipInterface::STATE_ACTIVE]);

/**
* Returns an array of groups filtered by the OG roles of the user.
*
* @param \Drupal\Core\Session\AccountInterface $user
* The user to get the groups for.
* @param int $user_id
* The ID of the user to get the groups for.
* @param string[] $role_ids
* A list of OG role IDs to filter by.
* @param string[] $states
Expand All @@ -73,13 +73,13 @@ public function getUserGroups(AccountInterface $user, array $states = [OgMembers
* An associative array, keyed by group entity type, each item an array of
* group entities.
*/
public function getUserGroupsByRoleIds(AccountInterface $user, array $role_ids, array $states = [OgMembershipInterface::STATE_ACTIVE], bool $require_all_roles = TRUE): array;
public function getUserGroupsByRoleIds($user_id, array $role_ids, array $states = [OgMembershipInterface::STATE_ACTIVE], bool $require_all_roles = TRUE): array;

/**
* Returns an array of groups ids filtered by the og roles of the user.
*
* @param \Drupal\Core\Session\AccountInterface $user
* The user to get the groups for.
* @param int $user_id
* The ID of the user to get the groups for.
* @param string[] $role_ids
* A list of OG role IDs to filter by.
* @param string[] $states
Expand All @@ -93,13 +93,13 @@ public function getUserGroupsByRoleIds(AccountInterface $user, array $role_ids,
* An associative array, keyed by group entity type, each item an array of
* group IDs.
*/
public function getUserGroupIdsByRoleIds(AccountInterface $user, array $role_ids, array $states = [OgMembershipInterface::STATE_ACTIVE], bool $require_all_roles = TRUE): array;
public function getUserGroupIdsByRoleIds($user_id, array $role_ids, array $states = [OgMembershipInterface::STATE_ACTIVE], bool $require_all_roles = TRUE): array;

/**
* Returns the group memberships a user is associated with.
*
* @param \Drupal\Core\Session\AccountInterface $user
* The user to get groups for.
* @param int $user_id
* The ID of the user to get group memberships for.
* @param array $states
* (optional) Array with the states to return. Defaults to only returning
* active memberships. In order to retrieve all memberships regardless of
Expand All @@ -108,15 +108,15 @@ public function getUserGroupIdsByRoleIds(AccountInterface $user, array $role_ids
* @return \Drupal\og\OgMembershipInterface[]
* An array of OgMembership entities, keyed by ID.
*/
public function getMemberships(AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]);
public function getMemberships($user_id, array $states = [OgMembershipInterface::STATE_ACTIVE]);

/**
* Returns the group membership for a given user and group.
*
* @param \Drupal\Core\Entity\EntityInterface $group
* The group to get the membership for.
* @param \Drupal\Core\Session\AccountInterface $user
* The user to get the membership for.
* @param int $user_id
* The ID of the user to get the membership for.
* @param array $states
* (optional) Array with the states to return. Defaults to only returning
* active memberships. In order to retrieve all memberships regardless of
Expand All @@ -126,7 +126,7 @@ public function getMemberships(AccountInterface $user, array $states = [OgMember
* The OgMembership entity. NULL will be returned if no membership is
* available that matches the passed in $states.
*/
public function getMembership(EntityInterface $group, AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]);
public function getMembership(EntityInterface $group, $user_id, array $states = [OgMembershipInterface::STATE_ACTIVE]);

/**
* Returns the membership IDs of the given group filtered by role names.
Expand Down Expand Up @@ -272,8 +272,8 @@ public function getGroupContentIds(EntityInterface $entity, array $entity_types
*
* @param \Drupal\Core\Entity\EntityInterface $group
* The group entity.
* @param \Drupal\Core\Session\AccountInterface $user
* The user to test the membership for.
* @param int $user_id
* The ID of the user to test the membership for.
* @param array $states
* (optional) Array with the membership states to check the membership.
* Defaults to active memberships.
Expand All @@ -282,36 +282,36 @@ public function getGroupContentIds(EntityInterface $entity, array $entity_types
* TRUE if the entity (e.g. the user or node) belongs to a group with
* a certain state.
*/
public function isMember(EntityInterface $group, AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]);
public function isMember(EntityInterface $group, $user_id, array $states = [OgMembershipInterface::STATE_ACTIVE]);

/**
* Returns whether a user belongs to a group with a pending status.
*
* @param \Drupal\Core\Entity\EntityInterface $group
* The group entity.
* @param \Drupal\Core\Session\AccountInterface $user
* The user entity.
* @param int $user_id
* The ID of the user.
*
* @return bool
* True if the membership is pending.
*
* @see \Drupal\og\Og::isMember
*/
public function isMemberPending(EntityInterface $group, AccountInterface $user);
public function isMemberPending(EntityInterface $group, $user_id);

/**
* Returns whether an entity belongs to a group with a blocked status.
*
* @param \Drupal\Core\Entity\EntityInterface $group
* The group entity.
* @param \Drupal\Core\Session\AccountInterface $user
* The entity to test the membership for.
* @param int $user_id
* The ID of the user to test the membership for.
*
* @return bool
* True if the membership is blocked.
*
* @see \Drupal\og\Og::isMember
*/
public function isMemberBlocked(EntityInterface $group, AccountInterface $user);
public function isMemberBlocked(EntityInterface $group, $user_id);

}
Loading