Skip to content

Commit b42b1e9

Browse files
imorlandStyleCIBot
andauthored
feat: Assign signups via oauth to specified groups (#93)
* feat: Assign signups via oauth to specified groups * Apply fixes from StyleCI * chore: format js --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent de483eb commit b42b1e9

File tree

6 files changed

+126
-3
lines changed

6 files changed

+126
-3
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ By default these providers are included:
2121

2222
This extension provides the ability to view the status of linked OAuth providers (intended for admin and/or moderator use). In order for this to function correctly, you must also set the permission `Moderate Access Tokens` to at least the same group as you require for `Moderate user's linked accounts`.
2323

24+
### Group Assignment
25+
26+
You can configure each OAuth provider to automatically assign users to a specific group when they register. This is useful for tracking which provider users signed up with or for granting specific permissions based on the authentication method.
27+
28+
To configure group assignment:
29+
1. Go to the extension settings
30+
2. Enable the desired OAuth provider
31+
3. Click the settings icon for that provider
32+
4. Select a group from the "Assign Group" dropdown
33+
5. Save your changes
34+
35+
Users who register through that provider will automatically be assigned to the selected group.
36+
2437
### Additional providers
2538

2639
Additional OAuth providers are available for this extension. Here's a handy list of known extensions, let us know if you know of any more and we'll get them added!

extend.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Flarum\Extend;
1717
use Flarum\Frontend\Document;
1818
use Flarum\User\Event\LoggedOut;
19+
use Flarum\User\Event\RegisteringFromProvider;
1920
use Flarum\User\Filter\UserFilterer;
2021
use Flarum\User\Search\UserSearcher;
2122
use FoF\Extend\Events\OAuthLoginSuccessful;
@@ -68,6 +69,7 @@
6869
->default('fof-oauth.log-oauth-errors', false),
6970

7071
(new Extend\Event())
72+
->listen(RegisteringFromProvider::class, Listeners\AssignGroupToUser::class)
7173
->listen(OAuthLoginSuccessful::class, Listeners\UpdateEmailFromProvider::class)
7274
->listen(LoggedOut::class, Listeners\HandleLogout::class)
7375
->subscribe(Listeners\ClearOAuthCache::class),

js/src/admin/components/AuthSettingsPage.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import app from 'flarum/admin/app';
22
import Button from 'flarum/common/components/Button';
3+
import Dropdown from 'flarum/common/components/Dropdown';
34
import ExtensionPage from 'flarum/admin/components/ExtensionPage';
45
import icon from 'flarum/common/helpers/icon';
56
import ItemList from 'flarum/common/utils/ItemList';
@@ -152,9 +153,60 @@ export default class AuthSettingsPage extends ExtensionPage {
152153
return items;
153154
}
154155

156+
getAvailableGroups() {
157+
const groups = app.store.all('groups');
158+
return groups.filter((group) => group.id() !== '2'); // Exclude the "Guests" group
159+
}
160+
155161
customProviderSettings(name) {
156162
const items = new ItemList();
157163

164+
// Add group selection dropdown
165+
items.add(
166+
'group',
167+
<div className="Form-group">
168+
<label>{app.translator.trans('fof-oauth.admin.settings.providers.group_label')}</label>
169+
<div className="helpText">{app.translator.trans('fof-oauth.admin.settings.providers.group_help')}</div>
170+
171+
{(() => {
172+
const groupId = this.setting(`fof-oauth.${name}.group`)();
173+
const selectedGroup = groupId ? app.store.getById('groups', groupId) : null;
174+
const icons = {
175+
1: 'fas fa-check', // Admins
176+
3: 'fas fa-user', // Members
177+
4: 'fas fa-map-pin', // Mods
178+
};
179+
180+
return (
181+
<Dropdown
182+
label={
183+
selectedGroup
184+
? [icon(selectedGroup.icon() || icons[selectedGroup.id()]), '\t', selectedGroup.namePlural()]
185+
: app.translator.trans('fof-oauth.admin.settings.providers.no_group_label')
186+
}
187+
buttonClassName="Button"
188+
disabled={!this.setting(`fof-oauth.${name}`)()}
189+
>
190+
<Button icon="fas fa-times" onclick={() => this.setting(`fof-oauth.${name}.group`)('')} active={!groupId}>
191+
{app.translator.trans('fof-oauth.admin.settings.providers.no_group_label')}
192+
</Button>
193+
194+
{this.getAvailableGroups().map((group) => (
195+
<Button
196+
icon={group.icon() || icons[group.id()]}
197+
onclick={() => this.setting(`fof-oauth.${name}.group`)(group.id())}
198+
active={groupId === group.id()}
199+
key={group.id()}
200+
>
201+
{group.namePlural()}
202+
</Button>
203+
))}
204+
</Dropdown>
205+
);
206+
})()}
207+
</div>
208+
);
209+
158210
return items;
159211
}
160212
}

resources/less/admin.less

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
max-height: 0;
4040
margin-bottom: 0;
4141
transition:
42-
max-height 0.25s,
42+
max-height 0.5s,
4343
margin-bottom 0.25s;
4444
overflow-y: hidden;
4545
overflow-x: visible;
@@ -56,9 +56,9 @@
5656
}
5757

5858
&.showing .Provider--settings {
59-
max-height: 600px;
59+
max-height: 2000px;
6060
margin-bottom: 25px;
61-
overflow-y: auto;
61+
overflow-y: visible;
6262
padding: 0 5px 5px;
6363
}
6464
}

resources/locale/en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ fof-oauth:
2424

2525
providers:
2626
callback_url_text: If necessary, set the callback URL to {url}.
27+
group_label: Assign Group
28+
group_help: Select a group to automatically assign to users who register using this provider.
29+
no_group_label: No group assignment
2730

2831
discord:
2932
description: Create an app at {link}. Add the redirect URL in the OAuth2 tab.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of fof/oauth.
5+
*
6+
* Copyright (c) FriendsOfFlarum.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FoF\OAuth\Listeners;
13+
14+
use Flarum\Group\Group;
15+
use Flarum\Settings\SettingsRepositoryInterface;
16+
use Flarum\User\Event\RegisteringFromProvider;
17+
use Flarum\User\User;
18+
19+
class AssignGroupToUser
20+
{
21+
/**
22+
* @var SettingsRepositoryInterface
23+
*/
24+
protected $settings;
25+
26+
/**
27+
* @param SettingsRepositoryInterface $settings
28+
*/
29+
public function __construct(SettingsRepositoryInterface $settings)
30+
{
31+
$this->settings = $settings;
32+
}
33+
34+
/**
35+
* @param RegisteringFromProvider $event
36+
*/
37+
public function handle(RegisteringFromProvider $event)
38+
{
39+
$provider = $event->provider;
40+
$user = $event->user;
41+
42+
// Get the group ID for this provider
43+
$groupId = $this->settings->get("fof-oauth.{$provider}.group");
44+
45+
// If a group is specified, assign it to the user
46+
if ($groupId && is_numeric($groupId)) {
47+
$user->afterSave(function (User $user) use ($groupId) {
48+
// Attach the group to the user
49+
$user->groups()->attach($groupId);
50+
});
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)