Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

Github Sponsors integration

Started by [email protected] 3 years ago · 6 replies · 341 views
3 years ago

Here's the idea:
1) My Grav site only accepts Github OAuth2 usernames and logins.
2) I want to restrict access to my Grav site based on whether the user has sponsored my project and made a donation.

How would I accomplish this?

3 years ago

@Spindizzy,

A few options come to mind:

  1. Only create accounts for sponsors.
  2. Create a group 'sponsor' and assign it to accounts from sponsors. Then add to some pages that only 'sponsor' users can login.
  3. As mentioned in the README of the Login plugin, there are several events that can be subscribed to in a custom plugin.

    • onUserLoginAuthorize Allows plugins to block user from being logged in.

    This sounds like a good candidate to add your own logic, to either read the list of sponsors online, or read an offline copy of the list.

3 years ago

Im thinking of doing a combination of #2 and #3. i have a Flarum forum running under Grav that has a REST api to synchronize accounts between Grav and Flarum, as well as get the Group the user is in on the Flarum side of things.

So what I want to do is, when the user logs into Grav, OnUserLogin in a custom plugin can get the group the user is in from Flarum, and synchronize it. So how would I change the user's group in a Grav plugin?

3 years ago

I understand how to do it from the Admin panel and manually but how would i do it inside a plugin?

3 years ago

@Spindizzy,

So how would I change the user’s group in a Grav plugin?

Assuming the relevant Flarum group(s) has/have already been created in Grav, you might try something like:

PHP
public function onUserLoginAuthorize(Event $event)
{
  /** @var User */
  $user = $event['user'];
  $groups = $user->groups;

  // Get flarum group of user
  $flarum_group = ...

  if (!$groups) {
    $user->groups = [$flarum_group];
  } else if (!in_array($flarum_group, $groups)) {
    array_push($groups, $flarum_group);
    $user->groups = $groups;
  }

  $user->save();
}
last edited 01/13/23 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 133 1 day ago
General · by pamtbaau, 1 day ago
1 93 1 day ago
General · by Andy Miller, 2 days ago
0 77 2 days ago
General · by Marcel, 12 months ago
6 387 5 days ago
General · by Duc , 6 days ago
3 72 6 days ago