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

Return partially populated OgRole entities when gathering default roles #237

Merged
merged 12 commits into from
Jun 17, 2016

Conversation

pfrenssen
Copy link
Collaborator

In #217 (comment) @amitaibu suggested that we should return partially populated OgRole entities instead of dumb arrays containing role properties when retrieving the default roles that are created when saving a new group.

As a side effect this also fixes #232 and #216 and obsoletes #235.

public function getName() {
// If the name is not set yet, try to derive it from the ID.
if (empty($this->name) && !empty($this->id())) {
list(, , $name) = explode('-', $this->id());
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems slightly limited. Maybe it would be better to explode, but pop off the last element and use that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There should always be three elements in the current implementation. But I think you're on to something, this is indeed a bug. I don't think there is anything stopping people to use dashes in their entity type IDs and bundle IDs, which means that counting the dashes is not going to cut it.

I'll change it to match exactly "{$this->getGroupEntityTypeId()}-{$this->getGroupBundleId()}-$role_name", then we'll eliminate any dash-related problems.

@pfrenssen
Copy link
Collaborator Author

Addressed everything.

Now that the OgRole entities are created in the event subscriber I was able to remove a lot of the complexity from the event listener, just as @amitaibu predicted. A very nice bonus is also that we no longer need to inject the OgRole entity storage in the listener, meaning it doesn't need to be a service any more, meaning that the ugly ::reset() method could be removed!

If tests come back green this is ready for a fresh look.

@@ -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.');
Copy link
Owner

Choose a reason for hiding this comment

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

we seem to have a similar check in validate, however that one throws a different exception. Maybe consolidate it, and call validate() from here instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 DefaultRoleEvent::validate() method is used to validate only the data that is necessary for the event listener to gather the default roles. This one is in OgRole::save() and validates everything required to successfully store the entity.

These are different use cases, and calling validate() from here will make OgRole depend on DefaultRoleEvent.

@amitaibu
Copy link
Owner

@pfrenssen , the recent changes have indeed cleaned up a lot, and it's much easier to follow the logic.

I've added a few comments, mostly about naming. Thanks!

@pfrenssen
Copy link
Collaborator Author

Addressed the remarks, ready for review!

@pfrenssen pfrenssen removed their assignment Jun 17, 2016
@amitaibu amitaibu merged commit 2e8581b into 8.x-1.x Jun 17, 2016
@amitaibu amitaibu deleted the return-partial-og-roles branch June 17, 2016 11:59
@amitaibu
Copy link
Owner

Wonderful, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow setting a default OgRole as "is_admin"
3 participants