feat(org): add create organization dialog and fix admin role check

- Add CreateOrgDialog component with name/slug form and auto-slug generation
- Add "New Organisation" button in TopBar org dropdown (limited to 10 orgs)
- Fix admin check in AppSidebar to use currently selected org role
  instead of global isOrgAdmin flag for proper org-scoped permissions
This commit is contained in:
2026-04-20 15:04:43 +09:30
parent e5fbbf521d
commit d927c17c60
3 changed files with 211 additions and 5 deletions
+9 -4
View File
@@ -22,6 +22,7 @@ import {
import { SecuirdLogo } from "@/components/branding/SecuirdLogo";
import { NavLink } from "@/components/NavLink";
import { useAuth } from "@/contexts/AuthContext";
import { useOrg } from "@/contexts/OrgContext";
import {
Sidebar,
SidebarContent,
@@ -78,7 +79,11 @@ export function AppSidebar() {
const { state } = useSidebar();
const collapsed = state === "collapsed";
const location = useLocation();
const { isOrgAdmin, isOrgMember, canViewSystemLogs } = useAuth();
const { isOrgMember, canViewSystemLogs } = useAuth();
const { selectedOrg } = useOrg();
// Check if user is admin/owner of the CURRENTLY SELECTED org (not just any org)
const isCurrentOrgAdmin = selectedOrg?.role === "owner" || selectedOrg?.role === "admin";
const isActive = (path: string) => location.pathname === path;
const isOrgActive = orgAdminNavItems.some((item) => isActive(item.url)) || adminNavItems.some((item) => isActive(item.url));
@@ -149,7 +154,7 @@ export function AppSidebar() {
)}
<SidebarGroupContent>
<SidebarMenu>
{(isOrgAdmin ? orgAdminNavItems : orgMemberNavItems).map((item) => (
{(isCurrentOrgAdmin ? orgAdminNavItems : orgMemberNavItems).map((item) => (
<SidebarMenuItem key={item.title}>
<SidebarMenuButton asChild>
<NavLink
@@ -175,8 +180,8 @@ export function AppSidebar() {
</SidebarGroup>
)}
{/* Admin Section — only visible to org admins/owners */}
{isOrgAdmin && (
{/* Admin Section — only visible to org admins/owners of the CURRENT org */}
{isCurrentOrgAdmin && (
<SidebarGroup className="mt-4">
{!collapsed && (
<SidebarGroupLabel className="px-4 text-xs font-medium text-sidebar-muted uppercase tracking-wider">
+33 -1
View File
@@ -1,5 +1,7 @@
import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { Menu, ChevronDown, LogOut, User, Shield, Building2, Loader2 } from "lucide-react";
import { useQueryClient } from "@tanstack/react-query";
import { Menu, ChevronDown, LogOut, User, Shield, Building2, Loader2, Plus } from "lucide-react";
import { Button } from "@/components/ui/button";
import { SidebarTrigger } from "@/components/ui/sidebar";
import {
@@ -15,15 +17,20 @@ import { useAuth } from "@/contexts/AuthContext";
import { useOrg } from "@/contexts/OrgContext";
import { useOrganizations } from "@/hooks/useOrganizations";
import { ComplianceBanner } from "@/components/auth/ComplianceBanner";
import { CreateOrgDialog } from "@/components/org/CreateOrgDialog";
export function TopBar() {
const navigate = useNavigate();
const { user, mfaCompliance, logout } = useAuth();
const { selectedOrg, selectOrg } = useOrg();
const queryClient = useQueryClient();
// Use React Query hook for organizations with automatic caching and deduplication
const { data: organizations = [], isLoading: orgsLoading } = useOrganizations();
// New org dialog state
const [createOrgOpen, setCreateOrgOpen] = useState(false);
// Ensure organizations is always an array (defensive check)
const organizationsArray = Array.isArray(organizations) ? organizations : [];
@@ -93,6 +100,19 @@ export function TopBar() {
</DropdownMenuItem>
))
)}
{/* New Organisation button - only show when under 10 orgs */}
{organizationsArray.length < 10 && (
<>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={() => setCreateOrgOpen(true)}
className="flex items-center gap-2 text-primary cursor-pointer"
>
<Plus className="w-4 h-4" />
New Organisation
</DropdownMenuItem>
</>
)}
</DropdownMenuContent>
</DropdownMenu>
@@ -136,6 +156,18 @@ export function TopBar() {
</DropdownMenu>
</div>
</div>
{/* Create Organisation Dialog - only render when user can create orgs */}
{organizationsArray.length < 10 && (
<CreateOrgDialog
open={createOrgOpen}
onOpenChange={setCreateOrgOpen}
onSuccess={(org) => {
queryClient.invalidateQueries({ queryKey: ['organizations'] });
selectOrg(org);
}}
/>
)}
</header>
);
}