From 186b5374c45efb42cb3838c7e948d8e2c5f3e399 Mon Sep 17 00:00:00 2001 From: IanM <16573496+imorland@users.noreply.github.com> Date: Tue, 13 Feb 2024 12:59:57 +0000 Subject: [PATCH] feat: moderation of linked accounts (#73) * feat: moderation of linked accounts * Apply fixes from StyleCI * chore: export components --------- Co-authored-by: StyleCI Bot --- README.md | 4 ++ extend.php | 10 +--- js/src/admin/index.js | 13 ++++- js/src/forum/components/LinkStatus.tsx | 10 ++-- js/src/forum/components/LinkedAccounts.tsx | 2 +- js/src/forum/components/ProviderInfo.tsx | 37 ++++++++----- js/src/forum/components/index.ts | 9 ++++ .../addLinkedAccountsToUserSecurityPage.tsx | 2 +- js/src/forum/index.ts | 2 + js/src/forum/models/LinkedAccount.ts | 1 + resources/locale/en.yml | 3 ++ src/Api/AddForumAttributes.php | 28 ++++++++++ .../DeleteProviderLinkController.php | 18 +++++-- .../Controllers/ListProvidersController.php | 15 ++++-- src/Events/UnlinkingFromProvider.php | 8 ++- tests/integration/api/ForumSerializerTest.php | 53 ++++++++++++++++++- 16 files changed, 173 insertions(+), 42 deletions(-) create mode 100644 js/src/forum/components/index.ts create mode 100644 src/Api/AddForumAttributes.php diff --git a/README.md b/README.md index 39afbe2..34d476c 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,10 @@ By default these providers are included: - LinkedIn - Twitter +### Permissions + +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`. + ### Additional providers 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! diff --git a/extend.php b/extend.php index 3e35059..b9fb153 100644 --- a/extend.php +++ b/extend.php @@ -43,20 +43,14 @@ ->get('/auth/twitter', 'auth.twitter', Controllers\TwitterAuthController::class), (new Extend\Routes('api')) - ->get('/linked-accounts', 'users.provider.list', Api\Controllers\ListProvidersController::class) + ->get('/users/{id}/linked-accounts', 'users.provider.list', Api\Controllers\ListProvidersController::class) ->delete('/linked-accounts/{id}', 'users.provider.delete', Api\Controllers\DeleteProviderLinkController::class), (new Extend\ServiceProvider()) ->register(OAuthServiceProvider::class), (new Extend\ApiSerializer(ForumSerializer::class)) - ->attributes(function (ForumSerializer $serializer, $model, array $attributes): array { - if ($serializer->getActor()->isGuest()) { - $attributes['fof-oauth'] = resolve('fof-oauth.providers.forum'); - } - - return $attributes; - }), + ->attributes(Api\AddForumAttributes::class), (new Extend\Settings()) ->default('fof-oauth.only_icons', false) diff --git a/js/src/admin/index.js b/js/src/admin/index.js index cbf01be..0477c7f 100644 --- a/js/src/admin/index.js +++ b/js/src/admin/index.js @@ -1,11 +1,20 @@ import app from 'flarum/admin/app'; - import AuthSettingsPage from './components/AuthSettingsPage'; import ConfigureWithOAuthPage from './components/ConfigureWithOAuthPage'; import ConfigureWithOAuthButton from './components/ConfigureWithOAuthButton'; app.initializers.add('fof/oauth', () => { - app.extensionData.for('fof-oauth').registerPage(AuthSettingsPage); + app.extensionData + .for('fof-oauth') + .registerPage(AuthSettingsPage) + .registerPermission( + { + icon: 'fas fa-sign-in-alt', + label: app.translator.trans('fof-oauth.admin.permissions.moderate_user_providers'), + permission: 'moderateUserProviders', + }, + 'moderate' + ); }); export { AuthSettingsPage, ConfigureWithOAuthPage, ConfigureWithOAuthButton }; diff --git a/js/src/forum/components/LinkStatus.tsx b/js/src/forum/components/LinkStatus.tsx index 3e06eee..e67891f 100644 --- a/js/src/forum/components/LinkStatus.tsx +++ b/js/src/forum/components/LinkStatus.tsx @@ -8,7 +8,7 @@ import User from 'flarum/common/models/User'; import ProviderInfo from './ProviderInfo'; import extractText from 'flarum/common/utils/extractText'; -interface IAttrs { +interface IAttrs extends ComponentAttrs { provider: LinkedAccount; user: User; } @@ -18,7 +18,7 @@ export default class LinkStatus extends Component { loading: false, }; - onbeforeupdate(vnode: Mithril.Vnode) { + onbeforeupdate(vnode: Mithril.Vnode) { super.onbeforeupdate(vnode); if (app.fof_oauth_linkingInProgress && app.fof_oauth_linkingProvider === this.attrs.provider.name()) { this.state.loading = true; @@ -29,7 +29,7 @@ export default class LinkStatus extends Component { } } - view(vnode: Mithril.Vnode): Mithril.Children { + view(vnode: Mithril.Vnode): Mithril.Children { return (
{this.iconView()} @@ -70,7 +70,7 @@ export default class LinkStatus extends Component {
); - } else if (!provider.orphaned()) { + } else if (!provider.orphaned() && (user.id() === app.session.user?.id() || !app.forum.attribute('fofOauthModerate'))) { return (