From 0bc53e20296de68288481e2375b297626087174b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 3 Dec 2021 17:04:47 +0100 Subject: [PATCH] Add ability to redirect users on external auth --- .../edit-basic-configuration.component.html | 32 ++++++++++++++++++- .../edit-basic-configuration.component.ts | 4 +++ .../edit-custom-config.component.ts | 12 +++++++ client/src/app/+login/login.component.ts | 5 +-- client/src/app/menu/menu.component.html | 4 ++- client/src/app/menu/menu.component.ts | 10 ++++++ client/src/root-helpers/plugins-manager.ts | 6 ++++ config/default.yaml | 6 ++++ config/production.yaml.example | 6 ++++ server/controllers/api/config.ts | 12 +++++++ server/initializers/checker-before-init.ts | 1 + server/initializers/config.ts | 5 +++ server/lib/server-config-manager.ts | 5 +++ server/tests/api/check-params/config.ts | 12 +++++++ server/tests/api/server/config.ts | 18 +++++++++++ shared/extra-utils/server/config-command.ts | 12 +++++++ shared/models/server/custom-config.model.ts | 14 ++++++++ shared/models/server/server-config.model.ts | 6 ++++ 18 files changed, 166 insertions(+), 4 deletions(-) diff --git a/client/src/app/+admin/config/edit-custom-config/edit-basic-configuration.component.html b/client/src/app/+admin/config/edit-custom-config/edit-basic-configuration.component.html index 318c8e2c2..c9533208a 100644 --- a/client/src/app/+admin/config/edit-custom-config/edit-basic-configuration.component.html +++ b/client/src/app/+admin/config/edit-custom-config/edit-basic-configuration.component.html @@ -56,6 +56,36 @@ + + + + +
+ +
+
+
+ + + +
+ + + ⚠️ You don't have any external auth plugin enabled. + ⚠️ You have multiple external auth plugins enabled. + + +
+
+
+
+ @@ -276,7 +306,7 @@
VIDEO CHANNELS
- +
diff --git a/client/src/app/+admin/config/edit-custom-config/edit-basic-configuration.component.ts b/client/src/app/+admin/config/edit-custom-config/edit-basic-configuration.component.ts index 7a8258820..81457bd36 100644 --- a/client/src/app/+admin/config/edit-custom-config/edit-basic-configuration.component.ts +++ b/client/src/app/+admin/config/edit-custom-config/edit-basic-configuration.component.ts @@ -36,6 +36,10 @@ export class EditBasicConfigurationComponent implements OnInit, OnChanges { } } + countExternalAuth () { + return this.serverConfig.plugin.registeredExternalAuths.length + } + getVideoQuotaOptions () { return this.configService.videoQuotaOptions } diff --git a/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts b/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts index fdb0a7532..f2eaa3033 100644 --- a/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts +++ b/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts @@ -106,6 +106,18 @@ export class EditCustomConfigComponent extends FormReactive implements OnInit { whitelisted: null } }, + client: { + videos: { + miniature: { + preferAuthorDisplayName: null + } + }, + menu: { + login: { + redirectOnSingleExternalAuth: null + } + } + }, cache: { previews: { size: CACHE_PREVIEWS_SIZE_VALIDATOR diff --git a/client/src/app/+login/login.component.ts b/client/src/app/+login/login.component.ts index 1fa4bd3b5..648b8db36 100644 --- a/client/src/app/+login/login.component.ts +++ b/client/src/app/+login/login.component.ts @@ -1,4 +1,4 @@ -import { environment } from 'src/environments/environment' + import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core' import { ActivatedRoute } from '@angular/router' import { AuthService, Notifier, RedirectService, UserService } from '@app/core' @@ -7,6 +7,7 @@ import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/ import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance' import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' +import { PluginsManager } from '@root-helpers/plugins-manager' import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models' @Component({ @@ -98,7 +99,7 @@ export class LoginComponent extends FormReactive implements OnInit, AfterViewIni } getAuthHref (auth: RegisteredExternalAuthConfig) { - return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}` + return PluginsManager.getExternalAuthHref(auth) } login () { diff --git a/client/src/app/menu/menu.component.html b/client/src/app/menu/menu.component.html index 55fc27b5f..9ea991042 100644 --- a/client/src/app/menu/menu.component.html +++ b/client/src/app/menu/menu.component.html @@ -99,7 +99,9 @@
diff --git a/client/src/app/menu/menu.component.ts b/client/src/app/menu/menu.component.ts index 97f07c956..bcc884878 100644 --- a/client/src/app/menu/menu.component.ts +++ b/client/src/app/menu/menu.component.ts @@ -21,6 +21,7 @@ import { LanguageChooserComponent } from '@app/menu/language-chooser.component' import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component' import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service' import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap' +import { PluginsManager } from '@root-helpers/plugins-manager' import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models' const logger = debug('peertube:menu:MenuComponent') @@ -129,6 +130,15 @@ export class MenuComponent implements OnInit { .subscribe(() => this.openQuickSettings()) } + getExternalLoginHref () { + if (this.serverConfig.client.menu.login.redirectOnSingleExternalAuth !== true) return undefined + + const externalAuths = this.serverConfig.plugin.registeredExternalAuths + if (externalAuths.length !== 1) return undefined + + return PluginsManager.getExternalAuthHref(externalAuths[0]) + } + isRegistrationAllowed () { if (!this.serverConfig) return false diff --git a/client/src/root-helpers/plugins-manager.ts b/client/src/root-helpers/plugins-manager.ts index a1b763ff2..9cba63373 100644 --- a/client/src/root-helpers/plugins-manager.ts +++ b/client/src/root-helpers/plugins-manager.ts @@ -15,6 +15,7 @@ import { RegisterClientHookOptions, RegisterClientSettingsScript, RegisterClientVideoFieldOptions, + RegisteredExternalAuthConfig, ServerConfigPlugin } from '../../../shared/models' import { environment } from '../environments/environment' @@ -78,6 +79,11 @@ class PluginsManager { return isTheme ? '/themes' : '/plugins' } + static getExternalAuthHref (auth: RegisteredExternalAuthConfig) { + return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}` + + } + loadPluginsList (config: HTMLServerConfig) { for (const plugin of config.plugin.registered) { this.addPlugin(plugin) diff --git a/config/default.yaml b/config/default.yaml index b9c725cea..074951117 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -82,6 +82,12 @@ client: # By default PeerTube client displays author username prefer_author_display_name: false + menu: + login: + # If you enable only one external auth plugin + # You can automatically redirect your users on this external platform when they click on the login button + redirect_on_single_external_auth: false + # From the project root directory storage: tmp: 'storage/tmp/' # Use to download data (imports etc), store uploaded files before and during processing... diff --git a/config/production.yaml.example b/config/production.yaml.example index d67349c1d..e38b79587 100644 --- a/config/production.yaml.example +++ b/config/production.yaml.example @@ -80,6 +80,12 @@ client: # By default PeerTube client displays author username prefer_author_display_name: false + menu: + login: + # If you enable only one external auth plugin + # You can automatically redirect your users on this external platform when they click on the login button + redirect_on_single_external_auth: false + # From the project root directory storage: tmp: '/var/www/peertube/storage/tmp/' # Use to download data (imports etc), store uploaded files before and during processing... diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 805ad99c7..b253db397 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts @@ -169,6 +169,18 @@ function customConfig (): CustomConfig { whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED } }, + client: { + videos: { + miniature: { + preferAuthorDisplayName: CONFIG.CLIENT.VIDEOS.MINIATURE.PREFER_AUTHOR_DISPLAY_NAME + } + }, + menu: { + login: { + redirectOnSingleExternalAuth: CONFIG.CLIENT.MENU.LOGIN.REDIRECT_ON_SINGLE_EXTERNAL_AUTH + } + } + }, cache: { previews: { size: CONFIG.CACHE.PREVIEWS.SIZE diff --git a/server/initializers/checker-before-init.ts b/server/initializers/checker-before-init.ts index 51c396548..c85c389cd 100644 --- a/server/initializers/checker-before-init.ts +++ b/server/initializers/checker-before-init.ts @@ -33,6 +33,7 @@ function checkMissedConfig () { 'transcoding.resolutions.2160p', 'import.videos.http.enabled', 'import.videos.torrent.enabled', 'import.videos.concurrency', 'auto_blacklist.videos.of_users.enabled', 'trending.videos.interval_days', + 'client.videos.miniature.prefer_author_display_name', 'client.menu.login.redirect_on_single_external_auth', 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route', 'instance.is_nsfw', 'instance.default_nsfw_policy', 'instance.robots', 'instance.securitytxt', 'services.twitter.username', 'services.twitter.whitelisted', diff --git a/server/initializers/config.ts b/server/initializers/config.ts index f3a7c6b6b..eb848be6b 100644 --- a/server/initializers/config.ts +++ b/server/initializers/config.ts @@ -63,6 +63,11 @@ const CONFIG = { MINIATURE: { get PREFER_AUTHOR_DISPLAY_NAME () { return config.get('client.videos.miniature.prefer_author_display_name') } } + }, + MENU: { + LOGIN: { + get REDIRECT_ON_SINGLE_EXTERNAL_AUTH () { return config.get('client.menu.login.redirect_on_single_external_auth') } + } } }, diff --git a/server/lib/server-config-manager.ts b/server/lib/server-config-manager.ts index bdf6492f9..6aa459f82 100644 --- a/server/lib/server-config-manager.ts +++ b/server/lib/server-config-manager.ts @@ -47,6 +47,11 @@ class ServerConfigManager { miniature: { preferAuthorDisplayName: CONFIG.CLIENT.VIDEOS.MINIATURE.PREFER_AUTHOR_DISPLAY_NAME } + }, + menu: { + login: { + redirectOnSingleExternalAuth: CONFIG.CLIENT.MENU.LOGIN.REDIRECT_ON_SINGLE_EXTERNAL_AUTH + } } }, diff --git a/server/tests/api/check-params/config.ts b/server/tests/api/check-params/config.ts index d0cd7722b..a6e87730a 100644 --- a/server/tests/api/check-params/config.ts +++ b/server/tests/api/check-params/config.ts @@ -54,6 +54,18 @@ describe('Test config API validators', function () { whitelisted: true } }, + client: { + videos: { + miniature: { + preferAuthorDisplayName: false + } + }, + menu: { + login: { + redirectOnSingleExternalAuth: false + } + } + }, cache: { previews: { size: 2 diff --git a/server/tests/api/server/config.ts b/server/tests/api/server/config.ts index ea524723c..96ec17b0f 100644 --- a/server/tests/api/server/config.ts +++ b/server/tests/api/server/config.ts @@ -43,6 +43,9 @@ function checkInitialConfig (server: PeerTubeServer, data: CustomConfig) { expect(data.services.twitter.username).to.equal('@Chocobozzz') expect(data.services.twitter.whitelisted).to.be.false + expect(data.client.videos.miniature.preferAuthorDisplayName).to.be.false + expect(data.client.menu.login.redirectOnSingleExternalAuth).to.be.false + expect(data.cache.previews.size).to.equal(1) expect(data.cache.captions.size).to.equal(1) expect(data.cache.torrents.size).to.equal(1) @@ -138,6 +141,9 @@ function checkUpdatedConfig (data: CustomConfig) { expect(data.services.twitter.username).to.equal('@Kuja') expect(data.services.twitter.whitelisted).to.be.true + expect(data.client.videos.miniature.preferAuthorDisplayName).to.be.true + expect(data.client.menu.login.redirectOnSingleExternalAuth).to.be.true + expect(data.cache.previews.size).to.equal(2) expect(data.cache.captions.size).to.equal(3) expect(data.cache.torrents.size).to.equal(4) @@ -246,6 +252,18 @@ const newCustomConfig: CustomConfig = { whitelisted: true } }, + client: { + videos: { + miniature: { + preferAuthorDisplayName: true + } + }, + menu: { + login: { + redirectOnSingleExternalAuth: true + } + } + }, cache: { previews: { size: 2 diff --git a/shared/extra-utils/server/config-command.ts b/shared/extra-utils/server/config-command.ts index 7a768b4df..a061ca89e 100644 --- a/shared/extra-utils/server/config-command.ts +++ b/shared/extra-utils/server/config-command.ts @@ -194,6 +194,18 @@ export class ConfigCommand extends AbstractCommand { whitelisted: true } }, + client: { + videos: { + miniature: { + preferAuthorDisplayName: false + } + }, + menu: { + login: { + redirectOnSingleExternalAuth: false + } + } + }, cache: { previews: { size: 2 diff --git a/shared/models/server/custom-config.model.ts b/shared/models/server/custom-config.model.ts index 3ed932494..52d3d9588 100644 --- a/shared/models/server/custom-config.model.ts +++ b/shared/models/server/custom-config.model.ts @@ -52,6 +52,20 @@ export interface CustomConfig { } } + client: { + videos: { + miniature: { + preferAuthorDisplayName: boolean + } + } + + menu: { + login: { + redirectOnSingleExternalAuth: boolean + } + } + } + cache: { previews: { size: number diff --git a/shared/models/server/server-config.model.ts b/shared/models/server/server-config.model.ts index e75eefd47..9f17276e0 100644 --- a/shared/models/server/server-config.model.ts +++ b/shared/models/server/server-config.model.ts @@ -39,6 +39,12 @@ export interface ServerConfig { preferAuthorDisplayName: boolean } } + + menu: { + login: { + redirectOnSingleExternalAuth: boolean + } + } } webadmin: {