Skip to content

Utilize Role isAdmin() functionality#225

Closed
damiankloip wants to merge 6 commits into
amitaibu:8.x-1.xfrom
damiankloip:og-role-is-admin
Closed

Utilize Role isAdmin() functionality#225
damiankloip wants to merge 6 commits into
amitaibu:8.x-1.xfrom
damiankloip:og-role-is-admin

Conversation

@damiankloip

Copy link
Copy Markdown
Collaborator

Currently we extend Role with OgRole but remove the is_admin flag usage, but all the methods are available. We are also using all the Role methods to check a role has a permission etc.. isAdmin() is built into all that. As if a role have that, it will always return true. So this is a simpler way of implementing admin permission checking, and being more in line with core.

@damiankloip

Copy link
Copy Markdown
Collaborator Author

I think we need to keep OgAccess::ADMINISTER_GROUP_PERMISSION as that is a global thing.

@amitaibu

amitaibu commented Jun 9, 2016

Copy link
Copy Markdown
Owner

Wow, it's @damiankloip! :)

@damiankloip

Copy link
Copy Markdown
Collaborator Author

@amitaibu Surprise!

Comment thread src/OgAccess.php
foreach ($membership->getRoles() as $role) {

/** @var $role RoleInterface */
$permissions = array_unique(array_merge($permissions, $role->getPermissions()));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think you need to keep the array_unique, because you might have the same permissions defined in different roles.

@damiankloip damiankloip self-assigned this Jun 9, 2016
Comment thread src/OgAccess.php Outdated
$permissions = [];
$user_is_group_admin = FALSE;

if ($membership = Og::getUserMembership($user, $group)) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@amitaibu this is also a pretty critical fix, before this was adding permissions for a users group from all groups the user is a part of. uh oh. Test coverage also included below. I verified locally by first writing the tests, which failed. This then fixed them again.

We are caching this stuff per group/per user so the permissions cached based on this need to reflect that too.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

👍

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

you can use \Drupal\og\Og::isMember here instead

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

isMember only returns a bool though? We need the membership below to check the roles on it.

->setGroupType($this->group1->getEntityTypeId())
->setGroupBundle($this->groupBundle)
->setIsAdmin(TRUE)
->save();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I really like the DX of this.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Indeed. This reminds me that we should allow setting is-admin in the default roles definition

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Ok, in front of a computer, so here's the reference \Drupal\og\GroupManager::createPerBundleRoles. That is, \Drupal\og\GroupManager::getDefaultRoles should allow having roles that are is_admin

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@amitaibu wouldn't this already be supported I'd someone defines a default role that is_admin?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Currently not, we have

  protected function createPerBundleRoles($entity_type_id, $bundle_id) {
    foreach ($this->getDefaultRoles() as $role_name => $default_properties) {
      $properties = [
        'group_type' => $entity_type_id,
        'group_bundle' => $bundle_id,
        'id' => $role_name,
        'role_type' => OgRole::getRoleTypeByName($role_name),
      ];

(no is_admin). I'll create a follow up issue for this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Oh. I see. Cool!
On 10 Jun 2016 19:05, "Amitai Burstein" notifications@github.com wrote:

In tests/src/Kernel/Access/OgEntityAccessTest.php
#225 (comment):

@@ -133,6 +177,15 @@ protected function setUp() {
->grantPermission($this->randomMachineName())
->save();

  • $this->ogAdminRole = OgRole::create();
  • $this->ogAdminRole
  •  ->setId($this->randomMachineName())
    
  •  ->setLabel($this->randomString())
    
  •  ->setGroupType($this->group1->getEntityTypeId())
    
  •  ->setGroupBundle($this->groupBundle)
    
  •  ->setIsAdmin(TRUE)
    
  •  ->save();
    

Currently not, we have

protected function createPerBundleRoles($entity_type_id, $bundle_id) { foreach ($this->getDefaultRoles() as $role_name => $default_properties) { $properties = [ 'group_type' => $entity_type_id, 'group_bundle' => $bundle_id, 'id' => $role_name, 'role_type' => OgRole::getRoleTypeByName($role_name), ];

(no is_admin). I'll create a follow up issue for this.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/amitaibu/og/pull/225/files/e67b43cb7803205e91601fdaafe60e548a43f199#r66656440,
or mute the thread
https://github.com/notifications/unsubscribe/ABAUw_0l8PVfwIeJQK-rTwejZv8ov2Spks5qKadogaJpZM4Ix0vw
.

@pfrenssen

Copy link
Copy Markdown
Collaborator

This looks good to me!

@pfrenssen

Copy link
Copy Markdown
Collaborator

Note that getMembership() is not tested here, but this has been cherry-picked from #217. A test for this method will be added in that PR.

@pfrenssen

pfrenssen commented Jun 10, 2016

Copy link
Copy Markdown
Collaborator

Small remark: I think it's a good idea to rename Og::getUserMembership() to Og::getMembership(). For consistency, can you also rename Og:getUserMemberships() to Og::getMemberships()?

@damiankloip

Copy link
Copy Markdown
Collaborator Author

@pfrenssen yes, we already discussed that on here but it's on one of the 'out of date diff' comments, see #225 (comment) . Basically renaming that will extend the scope of this further than we need so I think that can be a simple follow up. That keeps things easier to review.

@pfrenssen

Copy link
Copy Markdown
Collaborator

👍

@damiankloip

Copy link
Copy Markdown
Collaborator Author

I think no conflicts with thw og access service PR. wow! :)

@amitaibu

Copy link
Copy Markdown
Owner

needs a re-roll

@damiankloip

damiankloip commented Jun 10, 2016

Copy link
Copy Markdown
Collaborator Author

Yeah, this is a nasty re-roll. I can't even be bothered to do this now... It will have to wait.

@amitaibu

Copy link
Copy Markdown
Owner

I can give it a try. I can take the shortcut and do a git merge 8.x-1.x. I hate rebasing..

@amitaibu

Copy link
Copy Markdown
Owner

@damiankloip since it's on your own repo, I'll create a new PR for this. Is that ok with you?

@damiankloip

damiankloip commented Jun 10, 2016

Copy link
Copy Markdown
Collaborator Author

Yes sure. Just base it from this branch and it should be all good?

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.

3 participants