Skip to content

Commit

Permalink
Refactor: Renaming global to general
Browse files Browse the repository at this point in the history
fix wrong model name
  • Loading branch information
monsieurswag committed Oct 2, 2024
1 parent 41c2bbe commit 2159b94
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions backend/global_settings/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from iam.sso.views import SSOSettingsViewSet

from .views import GlobalSettingsViewSet, get_sso_info, update_global_settings
from .views import GlobalSettingsViewSet, get_sso_info, update_general_settings
from .routers import DefaultSettingsRouter


Expand All @@ -20,7 +20,7 @@
urlpatterns = [
# This route should ideally be placed under the routes of the routers, but the DefaultRouter usage overwrite the route and makes it inaccessible.
# Could we use DefaultSettingsRouter to register the "global" route to fix that ?
path(r"global/update/", update_global_settings, name="update_global_settings"),
path(r"general/update/", update_general_settings, name="update_general_settings"),
path(r"", include(router.urls)),
path(r"", include(settings_router.urls)),
path(r"sso/info/", get_sso_info, name="get_sso_info"),
Expand Down
24 changes: 12 additions & 12 deletions backend/global_settings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,33 @@ def update(self, request, *args, **kwargs):
)


UPDATABLE_GLOBAL_SETTINGS = frozenset(
UPDATABLE_GENERAL_SETTINGS = frozenset(
["lang"]
) # This represents the list of GlobalSettings an admin has the right to change.
) # This represents the list of "general" GlobalSettings an admin has the right to change.


@api_view(["PATCH"])
@permission_classes([permissions.IsAdminUser])
def update_global_settings(request):
def update_general_settings(request):
"""
API endpoint that returns the CSRF token.
API endpoint to update general settings as an administrator.
"""
BaseModelViewSet._process_request_data(request)

global_settings = GlobalSettings.objects.filter(name="general").first()
if global_settings is not None:
global_settings = global_settings.value
general_settings = GlobalSettings.objects.filter(name="general").first()
if general_settings is not None:
general_settings = general_settings.value
else:
global_settings = {}
general_settings = {}

for key, value in request.data.items():
# There is no schema verification for this
# An attacker may be able to break a ciso-assistant instance by injecting values with bad types in future global settings.
if key in UPDATABLE_GLOBAL_SETTINGS:
global_settings[key] = value
# An attacker may be able to break a ciso-assistant instance by injecting values with bad types in future general settings.
if key in UPDATABLE_GENERAL_SETTINGS:
general_settings[key] = value

GlobalSettings.objects.update_or_create(
name="general", defaults={"value": global_settings}
name="general", defaults={"value": general_settings}
)

return Response({})
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/lib/components/Forms/ModelForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import FrameworksForm from './ModelForm/FrameworkForm.svelte';
import UsersForm from './ModelForm/UserForm.svelte';
import SsoSettingsForm from './ModelForm/SsoSettingForm.svelte';
import GlobalSettingsForm from './ModelForm/GlobalSettingForm.svelte';
import GeneralSettingsForm from './ModelForm/GeneralSettingForm.svelte';
import AutocompleteSelect from './AutocompleteSelect.svelte';
Expand Down Expand Up @@ -243,8 +243,8 @@
<UsersForm {form} {model} {cacheLocks} {formDataCache} {shape} />
{:else if URLModel === 'sso-settings'}
<SsoSettingsForm {form} {model} {cacheLocks} {formDataCache} {data} />
{:else if URLModel === 'global-settings'}
<GlobalSettingsForm {form} {model} {cacheLocks} {formDataCache} {data} />
{:else if URLModel === 'general-settings'}
<GeneralSettingsForm {form} {model} {cacheLocks} {formDataCache} {data} />
{/if}
<div class="flex flex-row justify-between space-x-4">
{#if closeModal}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lib/utils/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export const SSOSettingsSchema = z.object({
want_name_id_encrypted: z.boolean().optional().nullable()
});

export const GlobalSettingsSchema = z.object({
export const GeneralSettingsSchema = z.object({
lang: z.string()
});

Expand Down Expand Up @@ -347,7 +347,7 @@ const SCHEMA_MAP: Record<string, AnyZodObject> = {
evidences: EvidenceSchema,
users: UserCreateSchema,
'sso-settings': SSOSettingsSchema,
'global-settings': GlobalSettingsSchema,
'general-settings': GeneralSettingsSchema,
entities: EntitiesSchema,
'entity-assessments': EntityAssessmentSchema,
representatives: representativeSchema,
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/routes/(app)/(internal)/settings/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { handleErrorResponse } from '$lib/utils/actions';
import { BASE_API_URL } from '$lib/utils/constants';
import { getModelInfo } from '$lib/utils/crud';
import { SSOSettingsSchema, GlobalSettingsSchema } from '$lib/utils/schemas';
import { SSOSettingsSchema, GeneralSettingsSchema } from '$lib/utils/schemas';
import * as m from '$paraglide/messages';
import { fail, type Actions } from '@sveltejs/kit';
import { setFlash } from 'sveltekit-flash-message/server';
Expand All @@ -15,7 +15,7 @@ export const load: PageServerLoad = async ({ fetch }) => {
const selectOptions: Record<string, any> = {};

const ssoMmodel = getModelInfo('sso-settings');
const globalSettingsModel = getModelInfo('global-settings');
const generalSettingModel = getModelInfo('general-settings');

if (ssoMmodel.selectFields) {
for (const selectField of ssoMmodel.selectFields) {
Expand All @@ -37,25 +37,25 @@ export const load: PageServerLoad = async ({ fetch }) => {
ssoMmodel.selectOptions = selectOptions;

const ssoForm = await superValidate(settings, zod(SSOSettingsSchema), { errors: false });
const globalSettingsForm = await superValidate(settings, zod(GlobalSettingsSchema), {
const generalSettingForm = await superValidate(settings, zod(GeneralSettingsSchema), {
errors: false
});

return { settings, ssoForm, ssoMmodel, globalSettingsForm, globalSettingsModel };
return { settings, ssoForm, ssoMmodel, generalSettingForm, generalSettingModel };
};

export const actions: Actions = {
global: async (event) => {
general: async (event) => {
const formData = await event.request.formData();

if (!formData) {
return fail(400, { form: null });
}

const schema = GlobalSettingsSchema;
const schema = GeneralSettingsSchema;
const form = await superValidate(formData, zod(schema));

const endpoint = `${BASE_API_URL}/settings/global/update/`;
const endpoint = `${BASE_API_URL}/settings/general/update/`;

const requestInitOptions: RequestInit = {
method: 'PATCH',
Expand All @@ -67,7 +67,7 @@ export const actions: Actions = {
if (!response.ok) return handleErrorResponse({ event, response, form });

// Make the translation
// It must be called m.globalSettingsUpdated()
// It must be called m.generalSettingsUpdated()
setFlash({ type: 'success', message: m.ssoSettingsUpdated() }, event);

return { form };
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/routes/(app)/(internal)/settings/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<!-- Translate the "Global Settings" String -->
<!-- Check if a translation already exist-->
<Tab bind:group={tabSet} name="ssoSettings" value={1}
><i class="fa-solid fa-cog" /> Global Settings</Tab
><i class="fa-solid fa-cog" /> General Settings</Tab
>
</TabGroup>
</div>
Expand All @@ -31,10 +31,10 @@
<div>
<span class="text-gray-500">{m.ssoSettingsDescription()}</span>
<ModelForm
form={data.globalSettingsForm}
model={data.globalSettingsModel}
form={data.generalSettingForm}
model={data.generalSettingModel}
cancelButton={false}
action="?/global"
action="?/general"
/>
</div>
{/if}

0 comments on commit 2159b94

Please sign in to comment.