Problem/Motivation

For a group-based intranet system we want a specific role (manager) to be able to manage all groups (departments). The "manager" is currently using ginvite to add employees to specific groups/departments so they can manage the group's content. These employees are not a Drupal user, yet. So they create an account using the link from the invitation mail and then can log in. However, the next step is that they need to accept the invitation first.

Since they are employees, they don't really have a choice in that case so we don't want them to be able to decline, or better even don't even show them the invitation. Invitation should immediately be accepted as soon as possible (which is right after registration I assume as only then the user exists).

Proposed resolution

I guess, ginvite could hook into the user registration process and after user.insert it could check for open invitations for that user's email address and the accept them programmatically.

Remaining tasks

Create a patch.

User interface changes

None really, if any change at all, we are getting rid of intermediate steps.
Only for ginvite configuration page a new option is required.

API changes

None.

Data model changes

None.

Comments

broon created an issue. See original summary.

broon’s picture

StatusFileSize
new1.38 KB

A quick and dirty way is to copy&paste the code from the InvitationOperations controller's accept method to the .module file where a hook_user_insert function already exists.

broon’s picture

StatusFileSize
new1.7 KB

Patch is missing the use statement for GroupContent.

jimmynash’s picture

This fit our use case as well. I'm a little behind on updates but this applied to 2.0.1 just fine and works as advertised.
The user created the account with the invite link and their invite was automatically applied.

I can potentially see a need to not have this apply but if it was a ginvite setting that might be good enough to allow administrators to toggle it on if needed.

It saved a ton of confusion for new users/invitees.

jimmynash’s picture

StatusFileSize
new1.33 KB

After updating to 2.1.0 the patch in 3 no longer applied. I attempted to re-roll it here.
It seems to work fine in the latest 2.x of ginvite.

jimmynash’s picture

StatusFileSize
new1.69 KB

Oops. I'm still getting used to this process when composer is involved. The patch in 5 is busted.

jimmynash’s picture

I'm not sure what is different between 2.0.1 and 2.1.0 exactly that would cause this but when calling

$group_membership->save();

I get a problem where php exits with a connection reset by peer error.

The patch in 6 is not working for me anymore. Still investigating.

jimmynash’s picture

I ended up leveraging the event that is dispatched at the end of the ginvite user_insert hook.

In a custom module EventSubscriber I used the same basic idea, code snips here:

Necessary use statements:

use Drupal\ginvite\Event\UserRegisteredFromInvitationEvent;
use Drupal\group\Entity\GroupContent;
use Drupal\ginvite\Plugin\GroupContentEnabler\GroupInvitation;

Event Callback

  /**
   * Auto Accept Group Invitations from the ginvite module
   *
   * @param \Drupal\ginvite\Event\UserRegisteredFromInvitationEvent $event
   *   The UserRegisteredFromInvitationEvent to process.
   */
  public function autoAcceptGroupInvitation(UserRegisteredFromInvitationEvent $event) {
    $invitation = $event->getGroupInvitation();
    $group_content = $invitation->getGroupContent();
    $group = $group_content->getGroup();

    $contentTypeConfigId = $group_content->getGroup()
      ->getGroupType()
      ->getContentPlugin('group_membership')
      ->getContentTypeConfigId();

    // Pre-populate a group membership with the current user.
    $group_membership = GroupContent::create([
      'type' => $contentTypeConfigId,
      'entity_id' => $group_content->get('entity_id')->getString(),
      'content_plugin' => 'group_membership',
      'gid' => $group->id(),
      'uid' => $group_content->getOwnerId(),
      'group_roles' => $group_content->get('group_roles')->getValue(),
    ]);

    $group_membership->save();

    // set the status of the invitation to accepted and save it
    $group_content->set('invitation_status', GroupInvitation::INVITATION_ACCEPTED);
    $group_content->save();
  }

Subscribed Event:

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    // ginvite event to auto accept invitation
    $events['user_registered_from_invitation'][] = ['autoAcceptGroupInvitation'];
    return $events;
  }

This seems to work without causing memory issues like I was experiencing with the patch.

lighttree’s picture

The method in #8 works for me. Thanks!

lobsterr’s picture

I need your opinion:
1) How useful it would be to have such settings per plugin. So, we can enable / disable it per group type ?
2) Or it is better keep it per invitations. For example on the form, we can have a checkbox "Accept invitation automatically" ?

jimmynash’s picture

Would it be possible to provide it as an option per group instance?

I can see a need for one group of the same type to use autoaccept while another wants to provide the second step after invite.

If a per instance group setting would be too difficult, I would vote for option 1 (autoaccept toggle per group type). I think autoaccept is the best experience for most invitees to a group.

broon’s picture

Status: Active » Needs review
StatusFileSize
new2.31 KB

I've taken the approach from #8 and turned it into a patch for GInvite 3.0.0-beta3 (also applies cleanly to 2.1.0).

As for the question about making autoAccept an option, I would prefer an option per invitation, but make the default value (on/off) an option of the group instance.

lobsterr’s picture

1) I've added a new settings on the plugin. So, we can control it per group type and change it at any moment
2) Also, I've added a new event, when user login with invitations. In this case I check current invitations and accept them too. Also, this event could be useful for some other cases
3) I refactored a bit code

I will now continue to work on version for Group 2 and Group 3 based on #12

  • LOBsTerr committed 03db862c on 2.x authored by broon
    Issue #3294710 by broon, jimmynash, LOBsTerr: Skip the invitation...

  • LOBsTerr committed 0e575f89 on 3.0.x authored by broon
    Issue #3294710 by broon, jimmynash, LOBsTerr: Skip the invitation...

  • LOBsTerr committed 3782f57e on 4.0.x authored by broon
    Issue #3294710 by broon, jimmynash, LOBsTerr: Skip the invitation...

  • LOBsTerr committed 50192d72 on 2.x
    Issue #3294710 by LOBsTerr: Code improvements.
    

  • LOBsTerr committed 35bcb76d on 3.0.x
    Issue #3294710 by LOBsTerr: Code improvements.
    
lobsterr’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.