feat(permissions): Add multiple admins capability

This commit is contained in:
Vlad Vladov
2025-09-03 02:01:14 +03:00
parent f8ebf77575
commit 544408886e
10 changed files with 90 additions and 39 deletions
@@ -158,6 +158,7 @@ export const AddInvitation = () => {
</FormControl> </FormControl>
<SelectContent> <SelectContent>
<SelectItem value="member">Member</SelectItem> <SelectItem value="member">Member</SelectItem>
<SelectItem value="admin">Admin</SelectItem>
</SelectContent> </SelectContent>
</Select> </Select>
<FormDescription> <FormDescription>
+47 -17
View File
@@ -156,7 +156,8 @@ const MENU: Menu = {
url: "/dashboard/schedules", url: "/dashboard/schedules",
icon: Clock, icon: Clock,
// Only enabled in non-cloud environments // Only enabled in non-cloud environments
isEnabled: ({ isCloud, auth }) => !isCloud && auth?.role === "owner", isEnabled: ({ isCloud, auth }) =>
!isCloud && (auth?.role === "owner" || auth?.role === "admin"),
}, },
{ {
isSingle: true, isSingle: true,
@@ -166,7 +167,9 @@ const MENU: Menu = {
// Only enabled for admins and users with access to Traefik files in non-cloud environments // Only enabled for admins and users with access to Traefik files in non-cloud environments
isEnabled: ({ auth, isCloud }) => isEnabled: ({ auth, isCloud }) =>
!!( !!(
(auth?.role === "owner" || auth?.canAccessToTraefikFiles) && (auth?.role === "owner" ||
auth?.role === "admin" ||
auth?.canAccessToTraefikFiles) &&
!isCloud !isCloud
), ),
}, },
@@ -177,7 +180,12 @@ const MENU: Menu = {
icon: BlocksIcon, icon: BlocksIcon,
// Only enabled for admins and users with access to Docker in non-cloud environments // Only enabled for admins and users with access to Docker in non-cloud environments
isEnabled: ({ auth, isCloud }) => isEnabled: ({ auth, isCloud }) =>
!!((auth?.role === "owner" || auth?.canAccessToDocker) && !isCloud), !!(
(auth?.role === "owner" ||
auth?.role === "admin" ||
auth?.canAccessToDocker) &&
!isCloud
),
}, },
{ {
isSingle: true, isSingle: true,
@@ -186,7 +194,12 @@ const MENU: Menu = {
icon: PieChart, icon: PieChart,
// Only enabled for admins and users with access to Docker in non-cloud environments // Only enabled for admins and users with access to Docker in non-cloud environments
isEnabled: ({ auth, isCloud }) => isEnabled: ({ auth, isCloud }) =>
!!((auth?.role === "owner" || auth?.canAccessToDocker) && !isCloud), !!(
(auth?.role === "owner" ||
auth?.role === "admin" ||
auth?.canAccessToDocker) &&
!isCloud
),
}, },
{ {
isSingle: true, isSingle: true,
@@ -195,7 +208,12 @@ const MENU: Menu = {
icon: Forward, icon: Forward,
// Only enabled for admins and users with access to Docker in non-cloud environments // Only enabled for admins and users with access to Docker in non-cloud environments
isEnabled: ({ auth, isCloud }) => isEnabled: ({ auth, isCloud }) =>
!!((auth?.role === "owner" || auth?.canAccessToDocker) && !isCloud), !!(
(auth?.role === "owner" ||
auth?.role === "admin" ||
auth?.canAccessToDocker) &&
!isCloud
),
}, },
// Legacy unused menu, adjusted to the new structure // Legacy unused menu, adjusted to the new structure
@@ -262,7 +280,8 @@ const MENU: Menu = {
url: "/dashboard/settings/server", url: "/dashboard/settings/server",
icon: Activity, icon: Activity,
// Only enabled for admins in non-cloud environments // Only enabled for admins in non-cloud environments
isEnabled: ({ auth, isCloud }) => !!(auth?.role === "owner" && !isCloud), isEnabled: ({ auth, isCloud }) =>
!!((auth?.role === "owner" || auth?.role === "admin") && !isCloud),
}, },
{ {
isSingle: true, isSingle: true,
@@ -276,7 +295,8 @@ const MENU: Menu = {
url: "/dashboard/settings/servers", url: "/dashboard/settings/servers",
icon: Server, icon: Server,
// Only enabled for admins // Only enabled for admins
isEnabled: ({ auth }) => !!(auth?.role === "owner"), isEnabled: ({ auth }) =>
!!(auth?.role === "owner" || auth?.role === "admin"),
}, },
{ {
isSingle: true, isSingle: true,
@@ -284,7 +304,8 @@ const MENU: Menu = {
icon: Users, icon: Users,
url: "/dashboard/settings/users", url: "/dashboard/settings/users",
// Only enabled for admins // Only enabled for admins
isEnabled: ({ auth }) => !!(auth?.role === "owner"), isEnabled: ({ auth }) =>
!!(auth?.role === "owner" || auth?.role === "admin"),
}, },
{ {
isSingle: true, isSingle: true,
@@ -300,7 +321,8 @@ const MENU: Menu = {
icon: BotIcon, icon: BotIcon,
url: "/dashboard/settings/ai", url: "/dashboard/settings/ai",
isSingle: true, isSingle: true,
isEnabled: ({ auth }) => !!(auth?.role === "owner"), isEnabled: ({ auth }) =>
!!(auth?.role === "owner" || auth?.role === "admin"),
}, },
{ {
isSingle: true, isSingle: true,
@@ -317,7 +339,8 @@ const MENU: Menu = {
url: "/dashboard/settings/registry", url: "/dashboard/settings/registry",
icon: Package, icon: Package,
// Only enabled for admins // Only enabled for admins
isEnabled: ({ auth }) => !!(auth?.role === "owner"), isEnabled: ({ auth }) =>
!!(auth?.role === "owner" || auth?.role === "admin"),
}, },
{ {
isSingle: true, isSingle: true,
@@ -325,7 +348,8 @@ const MENU: Menu = {
url: "/dashboard/settings/destinations", url: "/dashboard/settings/destinations",
icon: Database, icon: Database,
// Only enabled for admins // Only enabled for admins
isEnabled: ({ auth }) => !!(auth?.role === "owner"), isEnabled: ({ auth }) =>
!!(auth?.role === "owner" || auth?.role === "admin"),
}, },
{ {
@@ -334,7 +358,8 @@ const MENU: Menu = {
url: "/dashboard/settings/certificates", url: "/dashboard/settings/certificates",
icon: ShieldCheck, icon: ShieldCheck,
// Only enabled for admins // Only enabled for admins
isEnabled: ({ auth }) => !!(auth?.role === "owner"), isEnabled: ({ auth }) =>
!!(auth?.role === "owner" || auth?.role === "admin"),
}, },
{ {
isSingle: true, isSingle: true,
@@ -342,7 +367,8 @@ const MENU: Menu = {
url: "/dashboard/settings/cluster", url: "/dashboard/settings/cluster",
icon: Boxes, icon: Boxes,
// Only enabled for admins in non-cloud environments // Only enabled for admins in non-cloud environments
isEnabled: ({ auth, isCloud }) => !!(auth?.role === "owner" && !isCloud), isEnabled: ({ auth, isCloud }) =>
!!((auth?.role === "owner" || auth?.role === "admin") && !isCloud),
}, },
{ {
isSingle: true, isSingle: true,
@@ -350,7 +376,8 @@ const MENU: Menu = {
url: "/dashboard/settings/notifications", url: "/dashboard/settings/notifications",
icon: Bell, icon: Bell,
// Only enabled for admins // Only enabled for admins
isEnabled: ({ auth }) => !!(auth?.role === "owner"), isEnabled: ({ auth }) =>
!!(auth?.role === "owner" || auth?.role === "admin"),
}, },
{ {
isSingle: true, isSingle: true,
@@ -358,7 +385,8 @@ const MENU: Menu = {
url: "/dashboard/settings/billing", url: "/dashboard/settings/billing",
icon: CreditCard, icon: CreditCard,
// Only enabled for admins in cloud environments // Only enabled for admins in cloud environments
isEnabled: ({ auth, isCloud }) => !!(auth?.role === "owner" && isCloud), isEnabled: ({ auth, isCloud }) =>
!!((auth?.role === "owner" || auth?.role === "admin") && isCloud),
}, },
], ],
@@ -654,7 +682,9 @@ function SidebarLogo() {
)} )}
</div> </div>
))} ))}
{(user?.role === "owner" || isCloud) && ( {(user?.role === "owner" ||
user?.role === "admin" ||
isCloud) && (
<> <>
<DropdownMenuSeparator /> <DropdownMenuSeparator />
<AddOrganization /> <AddOrganization />
@@ -1018,7 +1048,7 @@ export default function Page({ children }: Props) {
</SidebarContent> </SidebarContent>
<SidebarFooter> <SidebarFooter>
<SidebarMenu className="flex flex-col gap-2"> <SidebarMenu className="flex flex-col gap-2">
{!isCloud && auth?.role === "owner" && ( {!isCloud && (auth?.role === "owner" || auth?.role === "admin") && (
<SidebarMenuItem> <SidebarMenuItem>
<UpdateServerButton /> <UpdateServerButton />
</SidebarMenuItem> </SidebarMenuItem>
+7 -3
View File
@@ -101,7 +101,9 @@ export const UserNav = () => {
> >
Monitoring Monitoring
</DropdownMenuItem> </DropdownMenuItem>
{(data?.role === "owner" || data?.canAccessToTraefikFiles) && ( {(data?.role === "owner" ||
data?.role === "admin" ||
data?.canAccessToTraefikFiles) && (
<DropdownMenuItem <DropdownMenuItem
className="cursor-pointer" className="cursor-pointer"
onClick={() => { onClick={() => {
@@ -111,7 +113,9 @@ export const UserNav = () => {
Traefik Traefik
</DropdownMenuItem> </DropdownMenuItem>
)} )}
{(data?.role === "owner" || data?.canAccessToDocker) && ( {(data?.role === "owner" ||
data?.role === "admin" ||
data?.canAccessToDocker) && (
<DropdownMenuItem <DropdownMenuItem
className="cursor-pointer" className="cursor-pointer"
onClick={() => { onClick={() => {
@@ -125,7 +129,7 @@ export const UserNav = () => {
)} )}
</> </>
) : ( ) : (
data?.role === "owner" && ( (data?.role === "owner" || data?.role === "admin") && (
<DropdownMenuItem <DropdownMenuItem
className="cursor-pointer" className="cursor-pointer"
onClick={() => { onClick={() => {
+1 -1
View File
@@ -40,7 +40,7 @@ export async function getServerSideProps(
}; };
} }
const { user } = await validateRequest(ctx.req); const { user } = await validateRequest(ctx.req);
if (!user || user.role !== "owner") { if (!user || (user.role !== "owner" && user.role !== "admin")) {
return { return {
redirect: { redirect: {
permanent: true, permanent: true,
@@ -18,7 +18,9 @@ const Page = () => {
<div className="w-full"> <div className="w-full">
<div className="h-full rounded-xl max-w-5xl mx-auto flex flex-col gap-4"> <div className="h-full rounded-xl max-w-5xl mx-auto flex flex-col gap-4">
<ProfileForm /> <ProfileForm />
{(data?.canAccessToAPI || data?.role === "owner") && <ShowApiKeys />} {(data?.canAccessToAPI ||
data?.role === "owner" ||
data?.role === "admin") && <ShowApiKeys />}
{/* {isCloud && <RemoveSelfAccount />} */} {/* {isCloud && <RemoveSelfAccount />} */}
</div> </div>
@@ -15,7 +15,7 @@ export const organizationRouter = createTRPCRouter({
}), }),
) )
.mutation(async ({ ctx, input }) => { .mutation(async ({ ctx, input }) => {
if (ctx.user.role !== "owner" && !IS_CLOUD) { if (ctx.user.role !== "owner" && ctx.user.role !== "admin" && !IS_CLOUD) {
throw new TRPCError({ throw new TRPCError({
code: "FORBIDDEN", code: "FORBIDDEN",
message: "Only the organization owner can create an organization", message: "Only the organization owner can create an organization",
@@ -86,7 +86,7 @@ export const organizationRouter = createTRPCRouter({
}), }),
) )
.mutation(async ({ ctx, input }) => { .mutation(async ({ ctx, input }) => {
if (ctx.user.role !== "owner" && !IS_CLOUD) { if (ctx.user.role !== "owner" && ctx.user.role !== "admin" && !IS_CLOUD) {
throw new TRPCError({ throw new TRPCError({
code: "FORBIDDEN", code: "FORBIDDEN",
message: "Only the organization owner can update it", message: "Only the organization owner can update it",
@@ -109,7 +109,7 @@ export const organizationRouter = createTRPCRouter({
}), }),
) )
.mutation(async ({ ctx, input }) => { .mutation(async ({ ctx, input }) => {
if (ctx.user.role !== "owner" && !IS_CLOUD) { if (ctx.user.role !== "owner" && ctx.user.role !== "admin" && !IS_CLOUD) {
throw new TRPCError({ throw new TRPCError({
code: "FORBIDDEN", code: "FORBIDDEN",
message: "Only the organization owner can delete it", message: "Only the organization owner can delete it",
+4 -4
View File
@@ -201,7 +201,7 @@ export const settingsRouter = createTRPCRouter({
if (IS_CLOUD) { if (IS_CLOUD) {
return true; return true;
} }
await updateUser(ctx.user.id, { await updateUser(ctx.user.ownerId, {
sshPrivateKey: input.sshPrivateKey, sshPrivateKey: input.sshPrivateKey,
}); });
@@ -213,7 +213,7 @@ export const settingsRouter = createTRPCRouter({
if (IS_CLOUD) { if (IS_CLOUD) {
return true; return true;
} }
const user = await updateUser(ctx.user.id, { const user = await updateUser(ctx.user.ownerId, {
host: input.host, host: input.host,
...(input.letsEncryptEmail && { ...(input.letsEncryptEmail && {
letsEncryptEmail: input.letsEncryptEmail, letsEncryptEmail: input.letsEncryptEmail,
@@ -240,7 +240,7 @@ export const settingsRouter = createTRPCRouter({
if (IS_CLOUD) { if (IS_CLOUD) {
return true; return true;
} }
await updateUser(ctx.user.id, { await updateUser(ctx.user.ownerId, {
sshPrivateKey: null, sshPrivateKey: null,
}); });
return true; return true;
@@ -300,7 +300,7 @@ export const settingsRouter = createTRPCRouter({
} }
} }
} else if (!IS_CLOUD) { } else if (!IS_CLOUD) {
const userUpdated = await updateUser(ctx.user.id, { const userUpdated = await updateUser(ctx.user.ownerId, {
enableDockerCleanup: input.enableDockerCleanup, enableDockerCleanup: input.enableDockerCleanup,
}); });
+9 -7
View File
@@ -56,15 +56,16 @@ export const stripeRouter = createTRPCRouter({
}); });
const items = getStripeItems(input.serverQuantity, input.isAnnual); const items = getStripeItems(input.serverQuantity, input.isAnnual);
const user = await findUserById(ctx.user.id); // Always operate on the organization owner's Stripe customer
const owner = await findUserById(ctx.user.ownerId);
let stripeCustomerId = user.stripeCustomerId; let stripeCustomerId = owner.stripeCustomerId;
if (stripeCustomerId) { if (stripeCustomerId) {
const customer = await stripe.customers.retrieve(stripeCustomerId); const customer = await stripe.customers.retrieve(stripeCustomerId);
if (customer.deleted) { if (customer.deleted) {
await updateUser(user.id, { await updateUser(owner.id, {
stripeCustomerId: null, stripeCustomerId: null,
}); });
stripeCustomerId = null; stripeCustomerId = null;
@@ -78,7 +79,7 @@ export const stripeRouter = createTRPCRouter({
customer: stripeCustomerId, customer: stripeCustomerId,
}), }),
metadata: { metadata: {
adminId: user.id, adminId: owner.id,
}, },
allow_promotion_codes: true, allow_promotion_codes: true,
success_url: `${WEBSITE_URL}/dashboard/settings/servers?success=true`, success_url: `${WEBSITE_URL}/dashboard/settings/servers?success=true`,
@@ -88,15 +89,16 @@ export const stripeRouter = createTRPCRouter({
return { sessionId: session.id }; return { sessionId: session.id };
}), }),
createCustomerPortalSession: adminProcedure.mutation(async ({ ctx }) => { createCustomerPortalSession: adminProcedure.mutation(async ({ ctx }) => {
const user = await findUserById(ctx.user.id); // Use the organization's owner account for billing portal
const owner = await findUserById(ctx.user.ownerId);
if (!user.stripeCustomerId) { if (!owner.stripeCustomerId) {
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
message: "Stripe Customer ID not found", message: "Stripe Customer ID not found",
}); });
} }
const stripeCustomerId = user.stripeCustomerId; const stripeCustomerId = owner.stripeCustomerId;
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, { const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: "2024-09-30.acacia", apiVersion: "2024-09-30.acacia",
+5 -1
View File
@@ -86,7 +86,11 @@ export const userRouter = createTRPCRouter({
// Allow access if: // Allow access if:
// 1. User is requesting their own information // 1. User is requesting their own information
// 2. User has owner role (admin permissions) AND user is in the same organization // 2. User has owner role (admin permissions) AND user is in the same organization
if (memberResult.userId !== ctx.user.id && ctx.user.role !== "owner") { if (
memberResult.userId !== ctx.user.id &&
ctx.user.role !== "owner" &&
ctx.user.role !== "admin"
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this user", message: "You are not authorized to access this user",
+10 -2
View File
@@ -183,7 +183,11 @@ export const uploadProcedure = async (opts: any) => {
}; };
export const cliProcedure = t.procedure.use(({ ctx, next }) => { export const cliProcedure = t.procedure.use(({ ctx, next }) => {
if (!ctx.session || !ctx.user || ctx.user.role !== "owner") { if (
!ctx.session ||
!ctx.user ||
(ctx.user.role !== "owner" && ctx.user.role !== "admin")
) {
throw new TRPCError({ code: "UNAUTHORIZED" }); throw new TRPCError({ code: "UNAUTHORIZED" });
} }
return next({ return next({
@@ -197,7 +201,11 @@ export const cliProcedure = t.procedure.use(({ ctx, next }) => {
}); });
export const adminProcedure = t.procedure.use(({ ctx, next }) => { export const adminProcedure = t.procedure.use(({ ctx, next }) => {
if (!ctx.session || !ctx.user || ctx.user.role !== "owner") { if (
!ctx.session ||
!ctx.user ||
(ctx.user.role !== "owner" && ctx.user.role !== "admin")
) {
throw new TRPCError({ code: "UNAUTHORIZED" }); throw new TRPCError({ code: "UNAUTHORIZED" });
} }
return next({ return next({