Skip to content

Commit

Permalink
Created the orgAdminProcedure (#574)
Browse files Browse the repository at this point in the history
Created a procedure to separate the logic checking if the user  in the organization is an administrator
  • Loading branch information
Esther-Lita authored Jul 18, 2024
1 parent 32dced5 commit b50a94f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 94 deletions.
32 changes: 4 additions & 28 deletions apps/platform/trpc/routers/orgRouter/mail/domainsRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { router, orgProcedure } from '~platform/trpc/trpc';
import { router, orgProcedure, orgAdminProcedure } from '~platform/trpc/trpc';
import { and, eq } from '@u22n/database/orm';
import {
domains,
Expand All @@ -15,7 +15,7 @@ import { updateDnsRecords } from '~platform/utils/updateDnsRecords';
import { iCanHazCallerFactory } from '../iCanHaz/iCanHazRouter';

export const domainsRouter = router({
createNewDomain: orgProcedure
createNewDomain: orgAdminProcedure
.input(
z.object({
domainName: z.string().min(3).max(255)
Expand All @@ -41,14 +41,6 @@ export const domainsRouter = router({
const newPublicId = typeIdGenerator('domains');
const domainName = input.domainName.toLowerCase();

const isAdmin = await isAccountAdminOfOrg(org);
if (!isAdmin) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You are not an admin'
});
}

const dnsData = await lookupNS(domainName);
if (
dnsData.success === false &&
Expand Down Expand Up @@ -175,7 +167,7 @@ export const domainsRouter = router({
};
}),

getDomain: orgProcedure
getDomain: orgAdminProcedure
.input(
z.object({
domainPublicId: typeIdValidator('domains')
Expand All @@ -196,14 +188,6 @@ export const domainsRouter = router({
// Handle when adding database replicas
const dbReplica = db;

const isAdmin = await isAccountAdminOfOrg(org);
if (!isAdmin) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You are not an admin'
});
}

const domainResponse = await dbReplica.query.domains.findFirst({
where: and(
eq(domains.publicId, domainPublicId),
Expand All @@ -226,7 +210,7 @@ export const domainsRouter = router({
};
}),

getDomainDns: orgProcedure
getDomainDns: orgAdminProcedure
.input(
z.object({
domainPublicId: typeIdValidator('domains')
Expand All @@ -237,14 +221,6 @@ export const domainsRouter = router({
const orgId = org?.id;
const { domainPublicId } = input;

const isAdmin = await isAccountAdminOfOrg(org);
if (!isAdmin) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You are not an admin'
});
}

return updateDnsRecords({ domainPublicId, orgId }, db);
}),

Expand Down
12 changes: 2 additions & 10 deletions apps/platform/trpc/routers/orgRouter/mail/emailIdentityRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { router, orgProcedure } from '~platform/trpc/trpc';
import { router, orgProcedure, orgAdminProcedure } from '~platform/trpc/trpc';
import {
and,
eq,
Expand Down Expand Up @@ -87,7 +87,7 @@ export const emailIdentityRouter = router({
available: true
};
}),
createNewEmailIdentity: orgProcedure
createNewEmailIdentity: orgAdminProcedure
.input(
z.object({
emailUsername: z.string().min(1).max(255),
Expand Down Expand Up @@ -119,14 +119,6 @@ export const emailIdentityRouter = router({

const emailUsername = input.emailUsername.toLowerCase();

const isAdmin = await isAccountAdminOfOrg(org);
if (!isAdmin) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You are not an admin'
});
}

if (!routeToOrgMemberPublicIds && !routeToTeamsPublicIds) {
throw new TRPCError({
code: 'BAD_REQUEST',
Expand Down
12 changes: 2 additions & 10 deletions apps/platform/trpc/routers/orgRouter/setup/profileRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { router, orgProcedure } from '~platform/trpc/trpc';
import { router, orgProcedure, orgAdminProcedure } from '~platform/trpc/trpc';
import { eq } from '@u22n/database/orm';
import { orgs } from '@u22n/database/schema';
import { typeIdValidator } from '@u22n/utils/typeid';
Expand Down Expand Up @@ -45,7 +45,7 @@ export const orgProfileRouter = router({
};
}),

setOrgProfile: orgProcedure
setOrgProfile: orgAdminProcedure
.input(
z.object({
orgName: z.string().min(3).max(32)
Expand All @@ -62,14 +62,6 @@ export const orgProfileRouter = router({
const orgId = org?.id;
const { orgName } = input;

const isAdmin = await isAccountAdminOfOrg(org);
if (!isAdmin) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You are not an admin'
});
}

await db
.update(orgs)
.set({
Expand Down
23 changes: 4 additions & 19 deletions apps/platform/trpc/routers/orgRouter/users/invitesRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
router,
orgProcedure,
accountProcedure,
publicProcedure
publicProcedure,
orgAdminProcedure
} from '~platform/trpc/trpc';
import { eq } from '@u22n/database/orm';
import {
Expand Down Expand Up @@ -472,7 +473,7 @@ export const invitesRouter = router({
orgShortCode: queryInvitesResponse.org.shortcode
};
}),
invalidateInvite: orgProcedure
invalidateInvite: orgAdminProcedure
.input(
z.object({
invitePublicId: typeIdValidator('orgInvitations')
Expand All @@ -487,14 +488,6 @@ export const invitesRouter = router({
}
const { db, org } = ctx;

const isAdmin = await isAccountAdminOfOrg(org);
if (!isAdmin) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You are not an admin'
});
}

await db
.update(orgInvitations)
.set({
Expand All @@ -506,7 +499,7 @@ export const invitesRouter = router({
success: true
};
}),
refreshInvite: orgProcedure
refreshInvite: orgAdminProcedure
.input(
z.object({
invitePublicId: typeIdValidator('orgInvitations')
Expand All @@ -521,14 +514,6 @@ export const invitesRouter = router({
}
const { db, org } = ctx;

const isAdmin = await isAccountAdminOfOrg(org);
if (!isAdmin) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You are not an admin'
});
}

await db
.update(orgInvitations)
.set({
Expand Down
31 changes: 4 additions & 27 deletions apps/platform/trpc/routers/orgRouter/users/teamsRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { router, orgProcedure } from '~platform/trpc/trpc';
import { router, orgProcedure, orgAdminProcedure } from '~platform/trpc/trpc';
import { eq, and } from '@u22n/database/orm';
import { teams } from '@u22n/database/schema';
import { typeIdGenerator, typeIdValidator } from '@u22n/utils/typeid';
Expand All @@ -9,7 +9,7 @@ import { TRPCError } from '@trpc/server';
import { addOrgMemberToTeamHandler } from './teamsHandler';

export const teamsRouter = router({
createTeam: orgProcedure
createTeam: orgAdminProcedure
.input(
z.object({
teamName: z.string().min(2).max(50),
Expand All @@ -30,14 +30,6 @@ export const teamsRouter = router({
const { teamName, teamDescription, teamColor } = input;
const newPublicId = typeIdGenerator('teams');

const isAdmin = await isAccountAdminOfOrg(org);
if (!isAdmin) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You are not an admin'
});
}

await db.insert(teams).values({
publicId: newPublicId,
name: teamName,
Expand Down Expand Up @@ -176,7 +168,7 @@ export const teamsRouter = router({
team: teamQuery
};
}),
addOrgMemberToTeam: orgProcedure
addOrgMemberToTeam: orgAdminProcedure
.input(
z.object({
teamPublicId: typeIdValidator('teams'),
Expand All @@ -193,14 +185,6 @@ export const teamsRouter = router({
const { org, db } = ctx;
const { teamPublicId, orgMemberPublicId } = input;

const isAdmin = await isAccountAdminOfOrg(org);
if (!isAdmin) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You are not an admin'
});
}

const newTeamMemberPublicId = await addOrgMemberToTeamHandler(db, {
orgId: org.id,
teamPublicId: teamPublicId,
Expand All @@ -212,7 +196,7 @@ export const teamsRouter = router({
publicId: newTeamMemberPublicId
};
}),
updateTeamMembers: orgProcedure
updateTeamMembers: orgAdminProcedure
.input(
z.object({
teamPublicId: typeIdValidator('teams'),
Expand All @@ -229,13 +213,6 @@ export const teamsRouter = router({
const { org, db } = ctx;
const { teamPublicId, orgMemberPublicIds } = input;

const isAdmin = await isAccountAdminOfOrg(org);
if (!isAdmin) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You are not an admin'
});
}
const teamMembers = await db.query.teams.findFirst({
where: and(eq(teams.publicId, teamPublicId), eq(teams.orgId, org.id)),
columns: {},
Expand Down
13 changes: 13 additions & 0 deletions apps/platform/trpc/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { validateOrgShortCode } from '~platform/utils/orgShortCode';
import type { TrpcContext } from '~platform/ctx';
import { z } from 'zod';
import { env } from '~platform/env';
import { isAccountAdminOfOrg } from '~platform/utils/account';

export const trpcContext = initTRPC
.context<TrpcContext>()
Expand Down Expand Up @@ -49,6 +50,7 @@ export const publicProcedure = trpcContext.procedure.use(
);

export const accountProcedure = publicProcedure.use(isAccountAuthenticated);

export const orgProcedure = publicProcedure
.use(isAccountAuthenticated)
.input(z.object({ orgShortCode: z.string() }))
Expand Down Expand Up @@ -98,6 +100,17 @@ export const orgProcedure = publicProcedure
})
);

export const orgAdminProcedure = orgProcedure.use(async ({ ctx, next }) => {
const isAdmin = await isAccountAdminOfOrg(ctx.org);
if (!isAdmin) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You need to be an administrator'
});
}
return next();
});

export const turnstileProcedure = publicProcedure
.input(z.object({ turnstileToken: z.string().optional() }))
.use(async ({ input, ctx, next }) => {
Expand Down

0 comments on commit b50a94f

Please sign in to comment.