Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat(limits): limit user invites

Marc Seitz01de92036fa1
diff --git a/pages/api/teams/[teamId]/invite.ts b/pages/api/teams/[teamId]/invite.ts
index 40b1d0fc..c3596597 100644
--- a/pages/api/teams/[teamId]/invite.ts
+++ b/pages/api/teams/[teamId]/invite.ts
@@ -7,6 +7,7 @@ import { errorhandler } from "@/lib/errorHandler";
import { sendTeammateInviteEmail } from "@/lib/emails/send-teammate-invite";
import { newId } from "@/lib/id-helper";
import { hashToken } from "@/lib/api/auth/token";
+import { getLimits } from "@/ee/limits/server";
export default async function handle(
req: NextApiRequest,
@@ -47,9 +48,14 @@ export default async function handle(
},
});
+ if (!team) {
+ res.status(404).json("Team not found");
+ return;
+ }
+
// check that the user is admin of the team, otherwise return 403
- const teamUsers = team?.users;
- const isUserAdmin = teamUsers?.some(
+ const teamUsers = team.users;
+ const isUserAdmin = teamUsers.some(
(user) =>
user.role === "ADMIN" &&
user.userId === (session.user as CustomUser).id,
@@ -59,6 +65,19 @@ export default async function handle(
return;
}
+ // Check if the user has reached the limit of users in the team
+ const limits = await getLimits({
+ teamId,
+ userId: (session.user as CustomUser).id,
+ });
+
+ if (limits && teamUsers.length >= limits.users) {
+ res
+ .status(403)
+ .json("You have reached the limit of users in your team");
+ return;
+ }
+
// check if user is already in the team
const isExistingMember = teamUsers?.some(
(user) => user.user.email === email,
diff --git a/pages/settings/people.tsx b/pages/settings/people.tsx
index 400de06d..fdaafe08 100644
--- a/pages/settings/people.tsx
+++ b/pages/settings/people.tsx
@@ -26,6 +26,7 @@ import { usePlan } from "@/lib/swr/use-billing";
import { useInvitations } from "@/lib/swr/use-invitations";
import { UpgradePlanModal } from "@/components/billing/upgrade-plan-modal";
import { useAnalytics } from "@/lib/analytics";
+import useLimits from "@/lib/swr/use-limits";
export default function Billing() {
const [isTeamMemberInviteModalOpen, setTeamMemberInviteModalOpen] =
@@ -36,6 +37,7 @@ export default function Billing() {
const { team, loading } = useGetTeam()!;
const teamInfo = useTeam();
const { plan: userPlan } = usePlan();
+ const { limits } = useLimits();
const { teams } = useTeams();
const analytics = useAnalytics();
@@ -43,6 +45,8 @@ export default function Billing() {
const router = useRouter();
+ const numUsers = (team && team.users.length) ?? 1;
+
const getUserDocumentCount = (userId: string) => {
const documents = team?.documents.filter(
(document) => document.owner.id === userId,
@@ -185,7 +189,8 @@ export default function Billing() {
Teammates that have access to this project.
</p>
</div>
- {userPlan !== "free" ? (
+ {userPlan !== "free" &&
+ (limits === null || (limits && limits.users >= numUsers)) ? (
<AddTeamMembers
open={isTeamMemberInviteModalOpen}
setOpen={setTeamMemberInviteModalOpen}