aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/plugins')
-rw-r--r--server/lib/plugins/plugin-manager.ts45
-rw-r--r--server/lib/plugins/register-helpers-store.ts12
2 files changed, 41 insertions, 16 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts
index f7b84b1ff..950acf7ad 100644
--- a/server/lib/plugins/plugin-manager.ts
+++ b/server/lib/plugins/plugin-manager.ts
@@ -144,20 +144,6 @@ export class PluginManager implements ServerHook {
144 return this.translations[locale] || {} 144 return this.translations[locale] || {}
145 } 145 }
146 146
147 onLogout (npmName: string, authName: string, user: MUser) {
148 const auth = this.getAuth(npmName, authName)
149
150 if (auth?.onLogout) {
151 logger.info('Running onLogout function from auth %s of plugin %s', authName, npmName)
152
153 try {
154 auth.onLogout(user)
155 } catch (err) {
156 logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err })
157 }
158 }
159 }
160
161 async isTokenValid (token: MOAuthTokenUser, type: 'access' | 'refresh') { 147 async isTokenValid (token: MOAuthTokenUser, type: 'access' | 'refresh') {
162 const auth = this.getAuth(token.User.pluginAuth, token.authName) 148 const auth = this.getAuth(token.User.pluginAuth, token.authName)
163 if (!auth) return true 149 if (!auth) return true
@@ -180,6 +166,37 @@ export class PluginManager implements ServerHook {
180 return true 166 return true
181 } 167 }
182 168
169 // ###################### External events ######################
170
171 onLogout (npmName: string, authName: string, user: MUser) {
172 const auth = this.getAuth(npmName, authName)
173
174 if (auth?.onLogout) {
175 logger.info('Running onLogout function from auth %s of plugin %s', authName, npmName)
176
177 try {
178 auth.onLogout(user)
179 } catch (err) {
180 logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err })
181 }
182 }
183 }
184
185 onSettingsChanged (name: string, settings: any) {
186 const registered = this.getRegisteredPluginByShortName(name)
187 if (!registered) {
188 logger.error('Cannot find plugin %s to call on settings changed.', name)
189 }
190
191 for (const cb of registered.registerHelpersStore.getOnSettingsChangedCallbacks()) {
192 try {
193 cb(settings)
194 } catch (err) {
195 logger.error('Cannot run on settings changed callback for %s.', registered.npmName, { err })
196 }
197 }
198 }
199
183 // ###################### Hooks ###################### 200 // ###################### Hooks ######################
184 201
185 async runHook<T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> { 202 async runHook<T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> {
diff --git a/server/lib/plugins/register-helpers-store.ts b/server/lib/plugins/register-helpers-store.ts
index 151196bf1..6317ac2cf 100644
--- a/server/lib/plugins/register-helpers-store.ts
+++ b/server/lib/plugins/register-helpers-store.ts
@@ -52,6 +52,8 @@ export class RegisterHelpersStore {
52 private readonly idAndPassAuths: RegisterServerAuthPassOptions[] = [] 52 private readonly idAndPassAuths: RegisterServerAuthPassOptions[] = []
53 private readonly externalAuths: RegisterServerAuthExternalOptions[] = [] 53 private readonly externalAuths: RegisterServerAuthExternalOptions[] = []
54 54
55 private readonly onSettingsChangeCallbacks: ((settings: any) => void)[] = []
56
55 private readonly router: express.Router 57 private readonly router: express.Router
56 58
57 constructor ( 59 constructor (
@@ -149,6 +151,10 @@ export class RegisterHelpersStore {
149 return this.externalAuths 151 return this.externalAuths
150 } 152 }
151 153
154 getOnSettingsChangedCallbacks () {
155 return this.onSettingsChangeCallbacks
156 }
157
152 private buildGetRouter () { 158 private buildGetRouter () {
153 return () => this.router 159 return () => this.router
154 } 160 }
@@ -185,7 +191,7 @@ export class RegisterHelpersStore {
185 const self = this 191 const self = this
186 192
187 return (options: RegisterServerAuthExternalOptions) => { 193 return (options: RegisterServerAuthExternalOptions) => {
188 if (!options.authName || !options.onAuthRequest || typeof options.onAuthRequest !== 'function') { 194 if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
189 logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName) 195 logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName)
190 return 196 return
191 } 197 }
@@ -212,7 +218,9 @@ export class RegisterHelpersStore {
212 218
213 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names), 219 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names),
214 220
215 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value) 221 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
222
223 onSettingsChange: (cb: (settings: any) => void) => this.onSettingsChangeCallbacks.push(cb)
216 } 224 }
217 } 225 }
218 226