Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Github Sponsors integration

Started by [email protected] 4 years ago · 6 replies · 599 views
4 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?

4 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.

4 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?

4 years ago

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

4 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 themetman, 1 day ago
2 41 18 hours ago
General · by Hanns Mattes, 1 week ago
0 121 1 week ago
General · by Karmalakas, 1 week ago
2 185 1 week ago
General · by Hanns Mattes, 3 weeks ago
0 235 3 weeks ago
General · by Dima, 4 weeks ago
1 211 4 weeks ago