-
Notifications
You must be signed in to change notification settings - Fork 16
Return partially populated OgRole entities when gathering default roles #237
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
Changes from all commits
4c64f8f
5ad42f7
4fd7dc8
d3691d7
72b5136
8a2e506
e633578
495eb8e
82dba46
c7b916a
5dffcb4
0f2fef0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,7 +149,7 @@ public function setGroupBundle($group_bundle) { | |
| * OgRoleInterface::ROLE_TYPE_STANDARD. | ||
| */ | ||
| public function getRoleType() { | ||
| return $this->get('role_type'); | ||
| return $this->get('role_type') ?: OgRoleInterface::ROLE_TYPE_STANDARD; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -174,11 +174,46 @@ public function setRoleType($role_type) { | |
| return $this->set('role_type', $role_type); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getName() { | ||
| // If the name is not set yet, try to derive it from the ID. | ||
| if (empty($this->name) && !empty($this->id()) && !empty($this->getGroupType()) && !empty($this->getGroupBundle())) { | ||
| // Check if the ID matches the pattern '{entity type}-{bundle}-{name}'. | ||
| $pattern = preg_quote("{$this->getGroupType()}-{$this->getGroupBundle()}-"); | ||
| preg_match("/$pattern(.+)/", $this->id(), $matches); | ||
| if (!empty($matches[1])) { | ||
| $this->setName($matches[1]); | ||
| } | ||
| } | ||
| return $this->get('name'); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function setName($name) { | ||
| $this->name = $name; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function save() { | ||
| if ($this->isNew()) { | ||
| // 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() && !empty($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()) { | ||
| throw new ConfigValueException('The ID should consist of the group entity type ID, group bundle ID and role name, separated by dashes.'); | ||
| } | ||
| } | ||
|
|
||
| // If a new OgRole is saved and the ID is not set, construct the ID from | ||
| // the entity type ID, bundle ID and role name. | ||
| if ($this->isNew() && empty($this->id())) { | ||
| if (empty($this->getGroupType())) { | ||
| throw new ConfigValueException('The group type can not be empty.'); | ||
| } | ||
|
|
@@ -187,6 +222,10 @@ public function save() { | |
| throw new ConfigValueException('The group bundle can not be empty.'); | ||
| } | ||
|
|
||
| if (empty($this->getName())) { | ||
| throw new ConfigValueException('The role name can not be empty.'); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we seem to have a similar check in
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had a look at this but it is not really possible I think. The These are different use cases, and calling |
||
| } | ||
|
|
||
| // When assigning a role to group we need to add a prefix to the ID in | ||
| // order to prevent duplicate IDs. | ||
| $prefix = $this->getGroupType() . '-' . $this->getGroupBundle() . '-'; | ||
|
|
@@ -195,7 +234,7 @@ public function save() { | |
| $prefix .= $this->getGroupId() . '-'; | ||
| } | ||
|
|
||
| $this->id = $prefix . $this->id(); | ||
| $this->id = $prefix . $this->getName(); | ||
| } | ||
|
|
||
| parent::save(); | ||
|
|
@@ -233,33 +272,6 @@ public function delete() { | |
| parent::delete(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns default properties for the default OG roles. | ||
| * | ||
| * These are the two roles that are required by every group: the 'member' and | ||
| * 'non-member' roles. | ||
| * | ||
| * All other default roles are provided by DefaultRoleEvent. | ||
| * | ||
| * @return array | ||
| * An array of properties, keyed by OG role. | ||
| * | ||
| * @see \Drupal\og\Event\DefaultRoleEventInterface | ||
| * @see \Drupal\og\GroupManager::getDefaultRoles() | ||
| */ | ||
| public static function getDefaultRoles() { | ||
| return [ | ||
| self::ANONYMOUS => [ | ||
| 'role_type' => OgRoleInterface::ROLE_TYPE_REQUIRED, | ||
| 'label' => 'Non-member', | ||
| ], | ||
| self::AUTHENTICATED => [ | ||
| 'role_type' => OgRoleInterface::ROLE_TYPE_REQUIRED, | ||
| 'label' => 'Member', | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Maps role names to role types. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| namespace Drupal\og\EventSubscriber; | ||
|
|
||
| use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
| use Drupal\og\Event\DefaultRoleEventInterface; | ||
| use Drupal\og\Event\PermissionEventInterface; | ||
| use Drupal\og\OgRoleInterface; | ||
|
|
@@ -20,14 +21,24 @@ class OgEventSubscriber implements EventSubscriberInterface { | |
| */ | ||
| protected $permissionManager; | ||
|
|
||
| /** | ||
| * The storage handler for OgRole entities. | ||
| * | ||
| * @var \Drupal\core\Entity\EntityStorageInterface | ||
| */ | ||
| protected $ogRoleStorage; | ||
|
|
||
| /** | ||
| * Constructs an OgEventSubscriber object. | ||
| * | ||
| * @param \Drupal\og\PermissionManagerInterface $permission_manager | ||
| * The OG permission manager. | ||
| * @param \Drupal\core\Entity\EntityTypeManagerInterface $entity_type_manager | ||
| * The entity type manager. | ||
| */ | ||
| public function __construct(PermissionManagerInterface $permission_manager) { | ||
| public function __construct(PermissionManagerInterface $permission_manager, EntityTypeManagerInterface $entity_type_manager) { | ||
| $this->permissionManager = $permission_manager; | ||
| $this->ogRoleStorage = $entity_type_manager->getStorage('og_role'); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -69,7 +80,12 @@ public function provideDefaultOgPermissions(PermissionEventInterface $event) { | |
| * The default role event. | ||
| */ | ||
| public function provideDefaultRoles(DefaultRoleEventInterface $event) { | ||
| $event->addRole(OgRoleInterface::ADMINISTRATOR, ['label' => 'Administrator']); | ||
| $role = $this->ogRoleStorage->create([ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| 'name' => OgRoleInterface::ADMINISTRATOR, | ||
| 'label' => 'Administrator', | ||
| 'is_admin' => TRUE, | ||
| ]); | ||
| $event->addRole($role); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it also be good to be able to have more generic roles? rather than always tied to a group? I am currently trying to use something similar. I have an admin type role, and this is available for on on all groups I create.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand, but to clarify the logic in OG7 that should be ported to OG8:
OgRoles could be either perBundle or perGroupId.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, thanks for clarifying @amitaibu. I think I am just using per bundle in that case... :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically, ignore me please ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes the per-group ID roles are not in yet, these are going to be added in #209. I'm not worrying about them here, that's the responsibility of that ticket.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@damiankloip if you want to provide a role that is available for all groups, you can provide an event subscriber that simply ignores the passed in group entity type and bundle and returns the role every time, eg: