-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
init rbac db schema #19
Merged
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5d67603
init rbac db design
jsbroks 90d69a5
remove access-query
jsbroks 6625b4a
remove old access queries
jsbroks f28045c
upsert predefined roles
jsbroks 3e7572d
set admin role on create
jsbroks 3ef2baf
clean up invite tables
jsbroks 40c2510
init first migraiton
jsbroks 5afd7c0
invite select role id
jsbroks 826d795
refactor member add page
jsbroks 0c4d64e
fix update default permissions
jsbroks a81f34e
clean up predefined roles
jsbroks 94139d1
add more auth checks to endpoints
jsbroks 1634ff2
add more operation functions
jsbroks a41822e
clean up more checks
jsbroks 2c6f857
created can func
jsbroks 3077aa7
fix linting
jsbroks a6e479b
fix lint
jsbroks 202bef8
formatting
jsbroks 9bb612f
clean typecheck
jsbroks 394476a
fix import
jsbroks 2d912b9
add lib
jsbroks 4b856ac
add validators to migration dockerfile
jsbroks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
"use client"; | ||
|
||
import type { User, WorkspaceMember } from "@ctrlplane/db/schema"; | ||
import type { Role, User, Workspace } from "@ctrlplane/db/schema"; | ||
import type { ColumnDef, ColumnFiltersState } from "@tanstack/react-table"; | ||
import { useState } from "react"; | ||
import { useMemo, useState } from "react"; | ||
import { | ||
flexRender, | ||
getCoreRowModel, | ||
getFilteredRowModel, | ||
useReactTable, | ||
} from "@tanstack/react-table"; | ||
import { capitalCase } from "change-case"; | ||
import { TbCheck, TbChevronDown, TbCopy, TbDots } from "react-icons/tb"; | ||
import { TbCheck, TbCopy, TbDots } from "react-icons/tb"; | ||
import { useCopyToClipboard } from "react-use"; | ||
import { v4 } from "uuid"; | ||
|
||
import { Avatar, AvatarFallback, AvatarImage } from "@ctrlplane/ui/avatar"; | ||
|
@@ -30,44 +30,56 @@ import { | |
DropdownMenuTrigger, | ||
} from "@ctrlplane/ui/dropdown-menu"; | ||
import { Input } from "@ctrlplane/ui/input"; | ||
import { | ||
Select, | ||
SelectContent, | ||
SelectItem, | ||
SelectTrigger, | ||
SelectValue, | ||
} from "@ctrlplane/ui/select"; | ||
import { Table, TableBody, TableCell, TableRow } from "@ctrlplane/ui/table"; | ||
import { | ||
Tooltip, | ||
TooltipContent, | ||
TooltipProvider, | ||
TooltipTrigger, | ||
} from "@ctrlplane/ui/tooltip"; | ||
import { predefinedRoles } from "@ctrlplane/validators/auth"; | ||
|
||
import { api } from "~/trpc/react"; | ||
|
||
interface Member { | ||
type Member = { | ||
id: string; | ||
user: User; | ||
workspace_member: WorkspaceMember; | ||
} | ||
workspace: Workspace; | ||
role: Role; | ||
}; | ||
|
||
const InviteLinkSection: React.FC<{ | ||
sessionMember?: Member; | ||
workspaceSlug: string; | ||
workspace: Workspace; | ||
inviteLink?: string; | ||
}> = ({ sessionMember, workspaceSlug, inviteLink }) => { | ||
const workspace = api.workspace.bySlug.useQuery(workspaceSlug); | ||
const utils = api.useUtils(); | ||
const { mutateAsync } = api.invite.workspace.link.create.useMutation({ | ||
onSuccess: () => | ||
utils.invite.workspace.link.byWorkspaceMemberId.invalidate(), | ||
}); | ||
}> = ({ workspace, inviteLink }) => { | ||
const create = api.workspace.invite.token.create.useMutation(); | ||
|
||
const [, copy] = useCopyToClipboard(); | ||
const [clickedCopy, setClickedCopy] = useState(false); | ||
const [roleId, setRoleId] = useState(predefinedRoles.admin.id); | ||
|
||
const [token] = useState(inviteLink ?? v4()); | ||
const token = useMemo(() => inviteLink ?? v4(), [inviteLink]); | ||
const baseUrl = api.runtime.baseUrl.useQuery(); | ||
const link = `${baseUrl.data}/join/${token}`; | ||
|
||
const roles = api.workspace.roles.useQuery(workspace.id); | ||
|
||
const handleCopyClick = () => { | ||
navigator.clipboard.writeText(link).then(() => { | ||
setClickedCopy(true); | ||
setTimeout(() => setClickedCopy(false), 1000); | ||
copy(link); | ||
setClickedCopy(true); | ||
setTimeout(() => setClickedCopy(false), 1000); | ||
create.mutate({ | ||
roleId, | ||
workspaceId: workspace.id, | ||
token, | ||
}); | ||
|
||
if (inviteLink == null && workspace.data != null && sessionMember != null) | ||
mutateAsync({ | ||
workspaceId: workspace.data.id, | ||
workspaceMemberId: sessionMember.workspace_member.id, | ||
token, | ||
}); | ||
}; | ||
|
||
return ( | ||
|
@@ -79,6 +91,19 @@ const InviteLinkSection: React.FC<{ | |
</p> | ||
</div> | ||
|
||
<Select value={roleId} onValueChange={setRoleId}> | ||
<SelectTrigger className="w-[200px]"> | ||
<SelectValue placeholder="Select a role" /> | ||
</SelectTrigger> | ||
<SelectContent className="w-[200px]"> | ||
Comment on lines
+95
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{roles.data?.map((r) => ( | ||
<SelectItem key={r.id} value={r.id}> | ||
{r.name} | ||
</SelectItem> | ||
))} | ||
</SelectContent> | ||
</Select> | ||
|
||
<div className="flex items-center space-x-2"> | ||
<Input readOnly value={link} className="w-96 overflow-ellipsis" /> | ||
<Button variant="outline" size="icon" onClick={handleCopyClick}> | ||
|
@@ -90,14 +115,9 @@ const InviteLinkSection: React.FC<{ | |
}; | ||
|
||
const AddMembersDialog: React.FC<{ | ||
workspaceSlug: string; | ||
sessionMember?: Member; | ||
}> = ({ sessionMember, workspaceSlug }) => { | ||
const [inviteMode, setInviteMode] = useState<"email" | "link">("email"); | ||
const inviteLink = api.invite.workspace.link.byWorkspaceMemberId.useQuery( | ||
sessionMember?.workspace_member.id ?? "", | ||
{ enabled: sessionMember != null }, | ||
); | ||
workspace: Workspace; | ||
}> = ({ workspace }) => { | ||
const [inviteMode, setInviteMode] = useState<"email" | "link">("link"); | ||
|
||
return ( | ||
<Dialog> | ||
|
@@ -106,15 +126,11 @@ const AddMembersDialog: React.FC<{ | |
</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Invite to {capitalCase(workspaceSlug)}</DialogTitle> | ||
<DialogTitle>Invite to {workspace.name}</DialogTitle> | ||
</DialogHeader> | ||
|
||
{inviteMode === "link" ? ( | ||
<InviteLinkSection | ||
sessionMember={sessionMember} | ||
workspaceSlug={workspaceSlug} | ||
inviteLink={inviteLink.data?.token} | ||
/> | ||
<InviteLinkSection workspace={workspace} /> | ||
) : ( | ||
<div>email</div> | ||
)} | ||
|
@@ -169,36 +185,64 @@ const columns: ColumnDef<Member>[] = [ | |
{ | ||
id: "role", | ||
header: "Role", | ||
accessorKey: "workspace_member.role", | ||
accessorKey: "role", | ||
enableGlobalFilter: false, | ||
cell: "admin", | ||
cell: ({ row }) => { | ||
const { id: currentRoleId } = row.original.role; | ||
const roles = api.workspace.roles.useQuery(row.original.workspace.id); | ||
|
||
return ( | ||
<Select defaultValue={currentRoleId}> | ||
<SelectTrigger className="w-[230px]"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
<SelectValue placeholder="Select a role" /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
{roles.data?.map((role) => ( | ||
<SelectItem key={role.id} value={role.id}> | ||
<TooltipProvider> | ||
<Tooltip> | ||
<TooltipTrigger>{role.name}</TooltipTrigger> | ||
{role.description && ( | ||
<TooltipContent> | ||
<p>{role.description}</p> | ||
</TooltipContent> | ||
)} | ||
</Tooltip> | ||
</TooltipProvider> | ||
</SelectItem> | ||
))} | ||
</SelectContent> | ||
</Select> | ||
); | ||
}, | ||
}, | ||
{ | ||
id: "actions", | ||
header: "Actions", | ||
accessorKey: "user.id", | ||
enableGlobalFilter: false, | ||
cell: () => ( | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button variant="ghost" size="icon"> | ||
<TbDots /> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent> | ||
<DropdownMenuItem>Edit</DropdownMenuItem> | ||
<DropdownMenuItem>Delete</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
<div className="flex justify-end"> | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button variant="ghost" size="icon"> | ||
<TbDots /> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent> | ||
<DropdownMenuItem>Edit</DropdownMenuItem> | ||
<DropdownMenuItem>Delete</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
</div> | ||
), | ||
}, | ||
]; | ||
|
||
export const MembersTable: React.FC<{ | ||
workspaceSlug: string; | ||
workspace: Workspace; | ||
data: Array<Member>; | ||
sessionMember?: Member; | ||
}> = ({ workspaceSlug, data, sessionMember }) => { | ||
}> = ({ workspace, data }) => { | ||
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]); | ||
|
||
const table = useReactTable({ | ||
|
@@ -224,30 +268,15 @@ export const MembersTable: React.FC<{ | |
className="w-72" | ||
onChange={(e) => table.setGlobalFilter(e.target.value)} | ||
/> | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button variant="outline" className="flex items-center gap-1"> | ||
<TbChevronDown /> | ||
Filter | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align="start"> | ||
<DropdownMenuItem>Admins</DropdownMenuItem> | ||
<DropdownMenuItem>Members</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
</div> | ||
<AddMembersDialog | ||
workspaceSlug={workspaceSlug} | ||
sessionMember={sessionMember} | ||
/> | ||
<AddMembersDialog workspace={workspace} /> | ||
</div> | ||
<Table> | ||
<TableBody> | ||
{table.getRowModel().rows.map((row) => ( | ||
<TableRow | ||
key={row.id} | ||
className="flex items-center justify-between border-b-neutral-800/50 px-0 hover:bg-transparent" | ||
className="border-b-neutral-800/50 px-0 hover:bg-transparent" | ||
> | ||
{row.getVisibleCells().map((cell) => ( | ||
<TableCell key={cell.id}> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will handle this with the slug validation once my other PR looks good.