Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check group content entity operation access #217

Merged
merged 98 commits into from
Aug 6, 2016
Merged
Show file tree
Hide file tree
Changes from 91 commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
8de919a
Check access to do CRUD operations on group content.
pfrenssen May 27, 2016
73d803b
Do not use CRUD in the method names, there are more operations than o…
pfrenssen Jun 8, 2016
551fa5d
Reuse the generation of the entity operation permissions.
pfrenssen Jun 8, 2016
764cf74
Inject the permission manager.
pfrenssen Jun 9, 2016
78fbae2
Use the injected account proxy to get information about the current u…
pfrenssen Jun 9, 2016
78ee472
Inject the permission manager now that OgAccess is a service.
pfrenssen Jun 10, 2016
a4b2de2
Update OgAccessEntityTest now that the entity operations are also che…
pfrenssen Jun 10, 2016
ab4159d
Merge remote-tracking branch 'origin/8.x-1.x' into check-group-conten…
pfrenssen Jun 10, 2016
4167dfe
Merge remote-tracking branch 'origin/8.x-1.x' into check-group-conten…
pfrenssen Jun 13, 2016
d6491e1
Merge remote-tracking branch 'origin/8.x-1.x' into check-group-conten…
pfrenssen Jun 13, 2016
15611c7
Update getPermissionList() now that the permissions are keyed by enti…
pfrenssen Jun 13, 2016
3096020
Don't speak about CRUD, entity operations go beyond the basic CRUD op…
pfrenssen Jun 13, 2016
970b78c
Update documentation.
pfrenssen Jun 13, 2016
9a7ef40
Put the additional metadata in the permission array.
pfrenssen Jun 13, 2016
aa14a43
If we handle all of this through the PermissionEvent we don't need to…
pfrenssen Jun 13, 2016
c79fd2e
Check the presence of the group content entity type and bundle for op…
pfrenssen Jun 13, 2016
b886f85
WIP documentation.
pfrenssen Jun 13, 2016
291aa23
Deliberately introduce a failure as a reminder.
pfrenssen Jun 14, 2016
4736124
Inject the permission manager. We'll need it.
pfrenssen Jun 14, 2016
e35e3be
Update documentation.
pfrenssen Jun 14, 2016
a2c2c4d
Merge branch 'return-partial-og-roles' into check-group-content-crud-…
pfrenssen Jun 15, 2016
e21cec4
Merge remote-tracking branch 'origin/8.x-1.x' into check-group-conten…
pfrenssen Jun 21, 2016
38bc56d
Merge branch 'split-permissions' into check-group-content-crud-access
pfrenssen Jun 21, 2016
46f9cb5
WIP
pfrenssen Jun 22, 2016
c97e381
Fix typo.
pfrenssen Jun 22, 2016
7bbca54
Fix matching of OgRole ID, it failed because 'non-member' contains a …
pfrenssen Jun 22, 2016
2362ffe
Musing about future improvements.
pfrenssen Jun 22, 2016
fba96ed
Inject the GroupManager.
pfrenssen Jun 22, 2016
6c51593
We have PermissionManagerInterface now.
pfrenssen Jun 22, 2016
7c238c4
Temporarily remove early abort.
pfrenssen Jun 22, 2016
ff655f9
Pass on the group content bundle IDs.
pfrenssen Jun 22, 2016
7c6a46c
Check if the user has the entity operation permission for the group m…
pfrenssen Jun 22, 2016
e820655
Merge branch 'split-permissions' into check-group-content-crud-access
pfrenssen Jun 28, 2016
47b2f0c
Update documentation.
pfrenssen Jun 28, 2016
6d666de
Add support for retrieving permissions from non-members.
pfrenssen Jun 28, 2016
4f1b190
Small restructuring to make the code more readable.
pfrenssen Jun 28, 2016
297e6c1
Merge remote-tracking branch 'origin/cleanup-og-access-test' into che…
pfrenssen Jun 29, 2016
517224b
More test greenage.
pfrenssen Jun 29, 2016
f1cdff3
Access the storage handler directly.
pfrenssen Jun 29, 2016
bf8c41c
Move the code to always return Neutral.
pfrenssen Jun 29, 2016
d600774
Clarify reminder.
pfrenssen Jun 29, 2016
60a2aa9
Small optimization pointed out by the unit test coverage.
pfrenssen Jun 29, 2016
4b0650b
Actually use the GroupManager, it has been injected for a reason.
pfrenssen Jun 29, 2016
2d21042
Correct documentation, this was never used as group content.
pfrenssen Jun 30, 2016
d55ba51
Remove unused use statement.
pfrenssen Jun 30, 2016
635227d
Start working on the test.
pfrenssen Jun 30, 2016
b7fce65
First round of failure fixing.
pfrenssen Jun 30, 2016
5125496
Change ownership from a string to a boolean.
pfrenssen Jul 5, 2016
fd88473
Update documentation.
pfrenssen Jul 5, 2016
2323d35
Rename [g|s]etRoles() to [g|s]etApplicableRoles().
pfrenssen Jul 5, 2016
9f8815c
Fix fatal errors discovered while testing.
pfrenssen Jul 5, 2016
25a6c3b
Update documentation.
pfrenssen Jul 5, 2016
9a9ee7d
Working on test coverage.
pfrenssen Jul 5, 2016
b32285e
Fix bug discovered by test.
pfrenssen Jul 6, 2016
d14bb31
Merge remote-tracking branch 'origin/8.x-1.x' into check-group-conten…
pfrenssen Jul 14, 2016
101f8ce
Only return a membership entity in which the user is a non-member if …
pfrenssen Jul 14, 2016
7a21262
Request both the memberships entities of members and non-members to d…
pfrenssen Jul 14, 2016
f8e5779
Add a functional test that checks access to an entity operation throu…
pfrenssen Jul 15, 2016
bbc5f94
Merge remote-tracking branch 'origin/8.x-1.x' into check-group-conten…
pfrenssen Jul 25, 2016
db804e6
Merge remote-tracking branch 'origin/8.x-1.x' into check-group-conten…
pfrenssen Aug 1, 2016
e11a717
Declare visibility on method.
pfrenssen Aug 1, 2016
0d89fba
Argument order of Og::getMembership() has changed.
pfrenssen Aug 2, 2016
3394ad5
Use the new DX friendly way to set the user and group when creating a…
pfrenssen Aug 2, 2016
b03c5b7
Fix PHP CodeSniffer warnings.
pfrenssen Aug 2, 2016
fe9170d
Actually is null.
pfrenssen Aug 2, 2016
463ef9f
Use the fullly qualified namespace for return values.
pfrenssen Aug 2, 2016
7a30183
Use shorthand array syntax.
pfrenssen Aug 3, 2016
5bf96e7
Merge remote-tracking branch 'origin/8.x-1.x' into check-group-conten…
pfrenssen Aug 4, 2016
7078165
Rename method. It only deals with a single operation at a time.
pfrenssen Aug 4, 2016
878164f
Improve documentation.
pfrenssen Aug 4, 2016
a42b96a
Use the ternary operator instead of an if statement.
pfrenssen Aug 4, 2016
c63d48b
Improve documentation.
pfrenssen Aug 4, 2016
74dac29
Rename $user to $membership_type since this better conveys what the v…
pfrenssen Aug 4, 2016
2128386
Rename 'newsletter_subscription' to 'newsletter'. Shorter is gooder.
pfrenssen Aug 4, 2016
1fbba71
Provide an issue number with a @todo so it becomes actionable.
pfrenssen Aug 4, 2016
8dd3c9b
Adopt the awesome new DX improvements for creating memberships.
pfrenssen Aug 4, 2016
7796f5a
Remove unused use statement.
pfrenssen Aug 4, 2016
6f64986
Change the membership state constants to strings.
pfrenssen Aug 5, 2016
2f2caa7
Small documentation update.
pfrenssen Aug 5, 2016
a841f2c
Test that blocked users cannot create, update or delete any group con…
pfrenssen Aug 5, 2016
b2e37e8
Fix PHP CodeSniffer warning.
pfrenssen Aug 5, 2016
37d2216
Update the membership state field definition, it has changed from int…
pfrenssen Aug 5, 2016
2a67361
Move the generation of the 'non-member membership' to the calling side.
pfrenssen Aug 5, 2016
f6c5af0
Blocked users should not have any permissions.
pfrenssen Aug 5, 2016
774e5c9
We don't care if a group is new when doing access checks.
pfrenssen Aug 5, 2016
370d543
Convert the functional access test into a kernel test.
pfrenssen Aug 5, 2016
0694f45
Fix PHP CodeSniffer warning.
pfrenssen Aug 5, 2016
9cf7f4d
Remove unused trait.
pfrenssen Aug 5, 2016
2063574
Add a test that proves that it is possible to grant permissions to no…
pfrenssen Aug 5, 2016
82f016f
Provide a convenient method OgRole::loadByGroupAndName().
pfrenssen Aug 5, 2016
768cbbe
Fix PHP CodeSniffer warnings.
pfrenssen Aug 5, 2016
b7df48b
Leverage OgAccess::userAccess() instead of duplicating half its logic…
pfrenssen Aug 6, 2016
a2ffe60
Add documentation.
pfrenssen Aug 6, 2016
cad927c
Remove unused variable.
pfrenssen Aug 6, 2016
ea97a93
Blocked users should not be granted any permissions.
pfrenssen Aug 6, 2016
d1e6ebc
Revert "Provide a convenient method OgRole::loadByGroupAndName()."
pfrenssen Aug 6, 2016
a9622bb
Merge remote-tracking branch 'origin/8.x-1.x' into check-group-conten…
pfrenssen Aug 6, 2016
bbceea5
Fix PHP_CodeSniffer warnings.
pfrenssen Aug 6, 2016
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.services.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
og.access:
class: Drupal\og\OgAccess
arguments: ['@config.factory', '@current_user', '@module_handler']
arguments: ['@config.factory', '@current_user', '@module_handler', '@og.group.manager', '@og.permission_manager']
og.event_subscriber:
class: Drupal\og\EventSubscriber\OgEventSubscriber
arguments: ['@og.permission_manager', '@entity_type.manager', '@entity_type.bundle.info']
Expand Down
13 changes: 9 additions & 4 deletions src/Entity/OgMembership.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function getRoles() {
/**
* {@inheritdoc}
*/
public function setRoles(array $roles = array()) {
public function setRoles(array $roles = []) {
$role_ids = array_map(function (OgRole $role) {
return $role->id();
}, $roles);
Expand All @@ -215,6 +215,11 @@ public function getRolesIds() {
* {@inheritdoc}
*/
public function hasPermission($permission) {
// Blocked users do not have any permissions.
if ($this->getState() === OgMembershipInterface::STATE_BLOCKED) {
return FALSE;
}

return array_filter($this->getRoles(), function (OgRole $role) use ($permission) {
return $role->hasPermission($permission);
});
Expand Down Expand Up @@ -255,10 +260,10 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Group entity id.'))
->setDescription(t("The entity ID of the group."));

$fields['state'] = BaseFieldDefinition::create('integer')
$fields['state'] = BaseFieldDefinition::create('string')
->setLabel(t('State'))
->setDescription(t("The state of the group content."))
->setDefaultValue(TRUE);
->setDescription(t('The user membership state: active, pending, or blocked.'))
->setDefaultValue(OgMembershipInterface::STATE_ACTIVE);

$fields['roles'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Roles'))
Expand Down
24 changes: 22 additions & 2 deletions src/Entity/OgRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\og\Entity;

use Drupal\Core\Config\ConfigValueException;
use Drupal\Core\Entity\EntityInterface;
use Drupal\og\Exception\OgRoleException;
use Drupal\og\OgRoleInterface;
use Drupal\user\Entity\Role;
Expand Down Expand Up @@ -36,6 +37,16 @@
*/
class OgRole extends Role implements OgRoleInterface {

/**
* Constructs an OgRole object.
*
* @param array $values
* An array of values to set, keyed by property name.
*/
public function __construct(array $values) {
parent::__construct($values, 'og_role');
}

/**
* Sets the ID of the role.
*
Expand Down Expand Up @@ -198,15 +209,23 @@ public function setName($name) {
return $this;
}

/**
* {@inheritdoc}
*/
public static function loadByGroupAndName(EntityInterface $group, $name) {
$role_id = "{$group->getEntityTypeId()}-{$group->bundle()}-$name";
return self::load($role_id);
}

/**
* {@inheritdoc}
*/
public function save() {
// The ID of a new OgRole has to consist of the entity type ID, bundle ID
// and role name, separated by dashes.
if ($this->isNew() && $this->id()) {
list($entity_type_id, $bundle_id, $name) = explode('-', $this->id());
if ($entity_type_id !== $this->getGroupType() || $bundle_id !== $this->getGroupBundle() || $name !== $this->getName()) {
$pattern = preg_quote("{$this->getGroupType()}-{$this->getGroupBundle()}-{$this->getName()}");
if (!preg_match("/$pattern/", $this->id())) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This fix covers having entity and bundle IDs containing dashes.

throw new ConfigValueException('The ID should consist of the group entity type ID, group bundle ID and role name, separated by dashes.');
}
}
Expand Down Expand Up @@ -257,6 +276,7 @@ public function set($property_name, $value) {
'group_type',
'group_bundle',
]);

if (!$is_locked_property || $this->isNew()) {
return parent::set($property_name, $value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/GroupContentOperationPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GroupContentOperationPermission extends Permission {
* FALSE if this permission applies to all entities, TRUE if it only applies
* to the entities owned by the user.
*/
protected $owner = 'any';
protected $owner = FALSE;

/**
* Returns the group content entity type ID to which this permission applies.
Expand Down
2 changes: 1 addition & 1 deletion src/GroupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function getGroupContentBundleIdsByGroupBundle($group_entity_type_id, $gr
}

/**
* Sets an entity type instance as being an OG group.
* Declares a bundle of an entity type as being an OG group.
*
* @param string $entity_type_id
* The entity type ID of the bundle to declare as being a group.
Expand Down
27 changes: 6 additions & 21 deletions src/Og.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\og\Entity\OgMembership;
use Drupal\og\Entity\OgRole;

/**
* A static helper class for OG.
Expand Down Expand Up @@ -249,15 +248,18 @@ public static function getMemberships(AccountInterface $user, array $states = [O
* (optional) Array with the state to return. Defaults to active.
*
* @return \Drupal\og\Entity\OgMembership|null
* The OgMembership entity, or NULL if the user is not a member of the
* group.
* The OgMembership entity. NULL will be returned if no membership is
* available that matches the passed in $states.
*/
public static function getMembership(EntityInterface $group, AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
foreach (static::getMemberships($user, $states) as $membership) {
if ($membership->getGroupEntityType() === $group->getEntityTypeId() && $membership->getGroupId() === $group->id()) {
return $membership;
}
}

// No membership matches the request.
return NULL;
}

/**
Expand Down Expand Up @@ -603,23 +605,6 @@ public static function groupManager() {
return \Drupal::service('og.group.manager');
}

/**
* Get a role by the group's bundle and role name.
*
* @param string $entity_type_id
* The group entity type ID.
* @param string $bundle
* The group bundle name.
* @param string $role_name
* The role name.
*
* @return \Drupal\og\OgRoleInterface|null
* The OG role object, or NULL if a matching role was not found.
*/
public static function getRole($entity_type_id, $bundle, $role_name) {
return OgRole::load($entity_type_id . '-' . $bundle . '-' . $role_name);
}

/**
* Return the og permission handler instance.
*
Expand Down Expand Up @@ -696,7 +681,7 @@ protected static function getFieldBaseDefinition($plugin_id) {
* @param array $options
* Overriding the default options of the selection handler.
*
* @return OgSelection
* @return \Drupal\og\Plugin\EntityReferenceSelection\OgSelection
* Returns the OG selection handler.
*
* @throws \Exception
Expand Down
Loading