Refactor admin settings access

This commit is contained in:
gpt-engineer-app[bot]
2025-09-28 19:27:19 +00:00
parent 1f0808a74a
commit 01b6d0b955
3 changed files with 12 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { supabase } from '@/integrations/supabase/client'; import { supabase } from '@/integrations/supabase/client';
import { useAuth } from './useAuth'; import { useAuth } from './useAuth';
import { useUserRole } from './useUserRole';
import { useToast } from './use-toast'; import { useToast } from './use-toast';
interface AdminSetting { interface AdminSetting {
@@ -13,6 +14,7 @@ interface AdminSetting {
export function useAdminSettings() { export function useAdminSettings() {
const { user } = useAuth(); const { user } = useAuth();
const { isSuperuser } = useUserRole();
const { toast } = useToast(); const { toast } = useToast();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
@@ -31,7 +33,7 @@ export function useAdminSettings() {
if (error) throw error; if (error) throw error;
return data as AdminSetting[]; return data as AdminSetting[];
}, },
enabled: !!user enabled: !!user && isSuperuser()
}); });
const updateSettingMutation = useMutation({ const updateSettingMutation = useMutation({

View File

@@ -15,7 +15,7 @@ import { Loader2, Save, Clock, Users, Bell, Shield, Settings, Trash2 } from 'luc
export default function AdminSettings() { export default function AdminSettings() {
const { user } = useAuth(); const { user } = useAuth();
const { isAdmin, isModerator, loading: roleLoading } = useUserRole(); const { isSuperuser, loading: roleLoading } = useUserRole();
const { const {
settings, settings,
isLoading, isLoading,
@@ -36,7 +36,7 @@ export default function AdminSettings() {
); );
} }
if (!user || (!isAdmin() && !isModerator())) { if (!user || !isSuperuser()) {
return ( return (
<> <>
<AdminHeader /> <AdminHeader />

View File

@@ -0,0 +1,7 @@
-- Update admin_settings RLS policy to only allow superusers
DROP POLICY IF EXISTS "Admins can manage settings" ON public.admin_settings;
CREATE POLICY "Superusers can manage settings"
ON public.admin_settings
FOR ALL
USING (is_superuser(auth.uid()));