name: p.name,
version: p.version,
authName: auth.authName,
- authDisplayName: auth.authDisplayName
+ authDisplayName: auth.authDisplayName()
})
}
}
plugin.settings = req.body.settings
await plugin.save()
+ await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
+
return res.sendStatus(204)
}
return this.translations[locale] || {}
}
- onLogout (npmName: string, authName: string, user: MUser) {
- const auth = this.getAuth(npmName, authName)
-
- if (auth?.onLogout) {
- logger.info('Running onLogout function from auth %s of plugin %s', authName, npmName)
-
- try {
- auth.onLogout(user)
- } catch (err) {
- logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err })
- }
- }
- }
-
async isTokenValid (token: MOAuthTokenUser, type: 'access' | 'refresh') {
const auth = this.getAuth(token.User.pluginAuth, token.authName)
if (!auth) return true
return true
}
+ // ###################### External events ######################
+
+ onLogout (npmName: string, authName: string, user: MUser) {
+ const auth = this.getAuth(npmName, authName)
+
+ if (auth?.onLogout) {
+ logger.info('Running onLogout function from auth %s of plugin %s', authName, npmName)
+
+ try {
+ auth.onLogout(user)
+ } catch (err) {
+ logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err })
+ }
+ }
+ }
+
+ onSettingsChanged (name: string, settings: any) {
+ const registered = this.getRegisteredPluginByShortName(name)
+ if (!registered) {
+ logger.error('Cannot find plugin %s to call on settings changed.', name)
+ }
+
+ for (const cb of registered.registerHelpersStore.getOnSettingsChangedCallbacks()) {
+ try {
+ cb(settings)
+ } catch (err) {
+ logger.error('Cannot run on settings changed callback for %s.', registered.npmName, { err })
+ }
+ }
+ }
+
// ###################### Hooks ######################
async runHook<T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> {
private readonly idAndPassAuths: RegisterServerAuthPassOptions[] = []
private readonly externalAuths: RegisterServerAuthExternalOptions[] = []
+ private readonly onSettingsChangeCallbacks: ((settings: any) => void)[] = []
+
private readonly router: express.Router
constructor (
return this.externalAuths
}
+ getOnSettingsChangedCallbacks () {
+ return this.onSettingsChangeCallbacks
+ }
+
private buildGetRouter () {
return () => this.router
}
const self = this
return (options: RegisterServerAuthExternalOptions) => {
- if (!options.authName || !options.onAuthRequest || typeof options.onAuthRequest !== 'function') {
+ if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName)
return
}
getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names),
- setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value)
+ setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
+
+ onSettingsChange: (cb: (settings: any) => void) => this.onSettingsChangeCallbacks.push(cb)
}
}
updatePlugin,
updatePluginPackageJSON,
updatePluginSettings,
- wait
+ wait,
+ waitUntilLog
} from '../../../../shared/extra-utils'
import { PluginType } from '../../../../shared/models/plugins/plugin.type'
import { PeerTubePluginIndex } from '../../../../shared/models/plugins/peertube-plugin-index.model'
it('Should have the correct global css', async function () {
const res = await getPluginsCSS(server.url)
- expect(res.text).to.contain('--mainBackgroundColor')
+ expect(res.text).to.contain('background-color: red')
})
it('Should have the plugin loaded in the configuration', async function () {
})
})
+ it('Should have watched settings changes', async function () {
+ this.timeout(10000)
+
+ await waitUntilLog(server, 'Settings changed!')
+ })
+
it('Should get a plugin and a theme', async function () {
{
const res = await getPlugin({
{
const result = registerExternalAuth({
authName: 'external-auth-1',
- authDisplayName: 'External Auth 1',
+ authDisplayName: () => 'External Auth 1',
onLogout: user => peertubeHelpers.logger.info('On logout %s', user.username),
onAuthRequest: (req, res) => {
const username = req.query.username
{
const result = registerExternalAuth({
authName: 'external-auth-2',
- authDisplayName: 'External Auth 2',
+ authDisplayName: () => 'External Auth 2',
onAuthRequest: (req, res) => {
result.userAuthenticated({
req,
{
const result = registerExternalAuth({
authName: 'external-auth-3',
- authDisplayName: 'External Auth 3',
+ authDisplayName: () => 'External Auth 3',
onAuthRequest: (req, res) => {
result.userAuthenticated({
req,
getSettings: (names: string[]) => Bluebird<{ [settingName: string]: string | boolean }>
setSetting: (name: string, value: string) => Bluebird<any>
+
+ onSettingsChange: (cb: (names: string[]) => void) => void
}
export interface RegisterServerAuthExternalOptions extends RegisterServerAuthBase {
// Will be displayed in a block next to the login form
- authDisplayName: string
+ authDisplayName: () => string
onAuthRequest: (req: express.Request, res: express.Response) => void
}