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:
@@ -22,6 +22,7 @@ import {
|
|||||||
import { SecuirdLogo } from "@/components/branding/SecuirdLogo";
|
import { SecuirdLogo } from "@/components/branding/SecuirdLogo";
|
||||||
import { NavLink } from "@/components/NavLink";
|
import { NavLink } from "@/components/NavLink";
|
||||||
import { useAuth } from "@/contexts/AuthContext";
|
import { useAuth } from "@/contexts/AuthContext";
|
||||||
|
import { useOrg } from "@/contexts/OrgContext";
|
||||||
import {
|
import {
|
||||||
Sidebar,
|
Sidebar,
|
||||||
SidebarContent,
|
SidebarContent,
|
||||||
@@ -78,7 +79,11 @@ export function AppSidebar() {
|
|||||||
const { state } = useSidebar();
|
const { state } = useSidebar();
|
||||||
const collapsed = state === "collapsed";
|
const collapsed = state === "collapsed";
|
||||||
const location = useLocation();
|
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 isActive = (path: string) => location.pathname === path;
|
||||||
const isOrgActive = orgAdminNavItems.some((item) => isActive(item.url)) || adminNavItems.some((item) => isActive(item.url));
|
const isOrgActive = orgAdminNavItems.some((item) => isActive(item.url)) || adminNavItems.some((item) => isActive(item.url));
|
||||||
@@ -149,7 +154,7 @@ export function AppSidebar() {
|
|||||||
)}
|
)}
|
||||||
<SidebarGroupContent>
|
<SidebarGroupContent>
|
||||||
<SidebarMenu>
|
<SidebarMenu>
|
||||||
{(isOrgAdmin ? orgAdminNavItems : orgMemberNavItems).map((item) => (
|
{(isCurrentOrgAdmin ? orgAdminNavItems : orgMemberNavItems).map((item) => (
|
||||||
<SidebarMenuItem key={item.title}>
|
<SidebarMenuItem key={item.title}>
|
||||||
<SidebarMenuButton asChild>
|
<SidebarMenuButton asChild>
|
||||||
<NavLink
|
<NavLink
|
||||||
@@ -175,8 +180,8 @@ export function AppSidebar() {
|
|||||||
</SidebarGroup>
|
</SidebarGroup>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Admin Section — only visible to org admins/owners */}
|
{/* Admin Section — only visible to org admins/owners of the CURRENT org */}
|
||||||
{isOrgAdmin && (
|
{isCurrentOrgAdmin && (
|
||||||
<SidebarGroup className="mt-4">
|
<SidebarGroup className="mt-4">
|
||||||
{!collapsed && (
|
{!collapsed && (
|
||||||
<SidebarGroupLabel className="px-4 text-xs font-medium text-sidebar-muted uppercase tracking-wider">
|
<SidebarGroupLabel className="px-4 text-xs font-medium text-sidebar-muted uppercase tracking-wider">
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
import { useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
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 { Button } from "@/components/ui/button";
|
||||||
import { SidebarTrigger } from "@/components/ui/sidebar";
|
import { SidebarTrigger } from "@/components/ui/sidebar";
|
||||||
import {
|
import {
|
||||||
@@ -15,15 +17,20 @@ import { useAuth } from "@/contexts/AuthContext";
|
|||||||
import { useOrg } from "@/contexts/OrgContext";
|
import { useOrg } from "@/contexts/OrgContext";
|
||||||
import { useOrganizations } from "@/hooks/useOrganizations";
|
import { useOrganizations } from "@/hooks/useOrganizations";
|
||||||
import { ComplianceBanner } from "@/components/auth/ComplianceBanner";
|
import { ComplianceBanner } from "@/components/auth/ComplianceBanner";
|
||||||
|
import { CreateOrgDialog } from "@/components/org/CreateOrgDialog";
|
||||||
|
|
||||||
export function TopBar() {
|
export function TopBar() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { user, mfaCompliance, logout } = useAuth();
|
const { user, mfaCompliance, logout } = useAuth();
|
||||||
const { selectedOrg, selectOrg } = useOrg();
|
const { selectedOrg, selectOrg } = useOrg();
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
// Use React Query hook for organizations with automatic caching and deduplication
|
// Use React Query hook for organizations with automatic caching and deduplication
|
||||||
const { data: organizations = [], isLoading: orgsLoading } = useOrganizations();
|
const { data: organizations = [], isLoading: orgsLoading } = useOrganizations();
|
||||||
|
|
||||||
|
// New org dialog state
|
||||||
|
const [createOrgOpen, setCreateOrgOpen] = useState(false);
|
||||||
|
|
||||||
// Ensure organizations is always an array (defensive check)
|
// Ensure organizations is always an array (defensive check)
|
||||||
const organizationsArray = Array.isArray(organizations) ? organizations : [];
|
const organizationsArray = Array.isArray(organizations) ? organizations : [];
|
||||||
|
|
||||||
@@ -93,6 +100,19 @@ export function TopBar() {
|
|||||||
</DropdownMenuItem>
|
</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>
|
</DropdownMenuContent>
|
||||||
</DropdownMenu>
|
</DropdownMenu>
|
||||||
|
|
||||||
@@ -136,6 +156,18 @@ export function TopBar() {
|
|||||||
</DropdownMenu>
|
</DropdownMenu>
|
||||||
</div>
|
</div>
|
||||||
</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>
|
</header>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
import { useState, useEffect } from "react";
|
||||||
|
import { Loader2 } from "lucide-react";
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
} from "@/components/ui/dialog";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
import { useToast } from "@/hooks/use-toast";
|
||||||
|
import { api, ApiError, Organization } from "@/lib/api";
|
||||||
|
|
||||||
|
interface CreateOrgDialogProps {
|
||||||
|
open: boolean;
|
||||||
|
onOpenChange: (open: boolean) => void;
|
||||||
|
onSuccess?: (org: Organization) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toSlug(name: string): string {
|
||||||
|
const slug = name
|
||||||
|
.toLowerCase()
|
||||||
|
.trim()
|
||||||
|
.replace(/[^\w\s-]/g, "")
|
||||||
|
.replace(/[\s_]+/g, "-")
|
||||||
|
.replace(/-+/g, "-")
|
||||||
|
.replace(/^-+|-+$/g, "");
|
||||||
|
return slug || "untitled-org";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function CreateOrgDialog({
|
||||||
|
open,
|
||||||
|
onOpenChange,
|
||||||
|
onSuccess,
|
||||||
|
}: CreateOrgDialogProps) {
|
||||||
|
const [orgName, setOrgName] = useState("");
|
||||||
|
const [orgSlug, setOrgSlug] = useState("");
|
||||||
|
const [slugTouched, setSlugTouched] = useState(false);
|
||||||
|
const [isCreating, setIsCreating] = useState(false);
|
||||||
|
const [createError, setCreateError] = useState<string | null>(null);
|
||||||
|
const { toast } = useToast();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (open) {
|
||||||
|
setOrgName("");
|
||||||
|
setOrgSlug("");
|
||||||
|
setSlugTouched(false);
|
||||||
|
setCreateError(null);
|
||||||
|
setIsCreating(false);
|
||||||
|
}
|
||||||
|
}, [open]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!slugTouched && orgName) {
|
||||||
|
setOrgSlug(toSlug(orgName));
|
||||||
|
}
|
||||||
|
}, [orgName, slugTouched]);
|
||||||
|
|
||||||
|
const handleClose = (isOpen: boolean) => {
|
||||||
|
if (!isOpen) {
|
||||||
|
setOrgName("");
|
||||||
|
setOrgSlug("");
|
||||||
|
setSlugTouched(false);
|
||||||
|
setCreateError(null);
|
||||||
|
setIsCreating(false);
|
||||||
|
}
|
||||||
|
onOpenChange(isOpen);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const trimmedName = orgName.trim();
|
||||||
|
const trimmedSlug = orgSlug.trim();
|
||||||
|
|
||||||
|
if (!trimmedName || !trimmedSlug) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsCreating(true);
|
||||||
|
setCreateError(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await api.organizations.create(trimmedName, trimmedSlug);
|
||||||
|
toast({
|
||||||
|
title: "Organisation created",
|
||||||
|
description: `${result.organization.name} has been created successfully.`,
|
||||||
|
});
|
||||||
|
onSuccess?.(result.organization);
|
||||||
|
handleClose(false);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Failed to create organisation:", err);
|
||||||
|
if (err instanceof ApiError) {
|
||||||
|
setCreateError(err.message);
|
||||||
|
} else {
|
||||||
|
setCreateError("An error occurred. Please try again.");
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
setIsCreating(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const isValid = orgName.trim() && orgSlug.trim();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={open} onOpenChange={handleClose}>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Create Organisation</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
|
||||||
|
<form onSubmit={handleSubmit} className="space-y-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="org-name">Organisation name</Label>
|
||||||
|
<Input
|
||||||
|
id="org-name"
|
||||||
|
placeholder="My Organisation"
|
||||||
|
value={orgName}
|
||||||
|
onChange={(e) => {
|
||||||
|
setOrgName(e.target.value);
|
||||||
|
setCreateError(null);
|
||||||
|
}}
|
||||||
|
disabled={isCreating}
|
||||||
|
autoFocus
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="org-slug">Slug</Label>
|
||||||
|
<Input
|
||||||
|
id="org-slug"
|
||||||
|
placeholder="my-organisation"
|
||||||
|
value={orgSlug}
|
||||||
|
onChange={(e) => {
|
||||||
|
setOrgSlug(e.target.value.toLowerCase().replace(/[^a-z0-9-]/g, ""));
|
||||||
|
setSlugTouched(true);
|
||||||
|
setCreateError(null);
|
||||||
|
}}
|
||||||
|
disabled={isCreating}
|
||||||
|
/>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
Used in URLs. Lowercase letters, numbers, and hyphens only.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{createError && (
|
||||||
|
<p className="text-sm text-destructive">{createError}</p>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="flex justify-end gap-2">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => handleClose(false)}
|
||||||
|
disabled={isCreating}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button type="submit" disabled={isCreating || !isValid}>
|
||||||
|
{isCreating && <Loader2 className="w-4 h-4 mr-2 animate-spin" />}
|
||||||
|
Create
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user