diff --git a/server/lib/settings.ts b/server/lib/settings.ts index b64ed424..1fcb48bf 100644 --- a/server/lib/settings.ts +++ b/server/lib/settings.ts @@ -102,7 +102,7 @@ class Settings { this.data = { clientId: '', main: { - apiKey: 'temp', + apiKey: '', applicationUrl: '', }, plex: { @@ -144,6 +144,10 @@ class Settings { } get main(): MainSettings { + if (!this.data.main.apiKey) { + this.data.main.apiKey = this.generateApiKey(); + this.save(); + } return this.data.main; } @@ -200,6 +204,16 @@ class Settings { return this.data.clientId; } + public regenerateApiKey(): MainSettings { + this.main.apiKey = this.generateApiKey(); + this.save(); + return this.main; + } + + private generateApiKey(): string { + return Buffer.from(`${Date.now()}${this.clientId}`).toString('base64'); + } + /** * Settings Load * diff --git a/server/routes/settings.ts b/server/routes/settings.ts index d4194a85..422510f2 100644 --- a/server/routes/settings.ts +++ b/server/routes/settings.ts @@ -4,6 +4,7 @@ import { RadarrSettings, SonarrSettings, Library, + MainSettings, } from '../lib/settings'; import { getRepository } from 'typeorm'; import { User } from '../entity/User'; @@ -19,9 +20,15 @@ import { merge } from 'lodash'; const settingsRoutes = Router(); -settingsRoutes.get('/main', (_req, res) => { +settingsRoutes.get('/main', (req, res) => { const settings = getSettings(); + if (!req.user?.hasPermission(Permission.ADMIN)) { + return res.status(200).json({ + applicationUrl: settings.main.applicationUrl, + } as Partial); + } + res.status(200).json(settings.main); }); diff --git a/src/components/Settings/SettingsMain.tsx b/src/components/Settings/SettingsMain.tsx index 77fc69c4..611e11b9 100644 --- a/src/components/Settings/SettingsMain.tsx +++ b/src/components/Settings/SettingsMain.tsx @@ -7,6 +7,7 @@ import { Form, Formik, Field } from 'formik'; import axios from 'axios'; import Button from '../Common/Button'; import { defineMessages, useIntl } from 'react-intl'; +import { useUser, Permission } from '../../hooks/useUser'; const messages = defineMessages({ generalsettings: 'General Settings', @@ -19,6 +20,7 @@ const messages = defineMessages({ }); const SettingsMain: React.FC = () => { + const { hasPermission } = useUser(); const intl = useIntl(); const { data, error, revalidate } = useSWR( '/api/v1/settings/main' @@ -41,7 +43,6 @@ const SettingsMain: React.FC = () => {
{ @@ -59,40 +60,42 @@ const SettingsMain: React.FC = () => { {({ isSubmitting }) => { return (
-
- -
-
- - - + {hasPermission(Permission.ADMIN) && ( +
+ +
+
+ + + +
-
+ )}