Skip to content

Commit 3d28257

Browse files
authored
Allowing teammates to see emails (#206)
1 parent 1b3b155 commit 3d28257

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/authz/helpers.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ export const isSuperAdminOrSelf = (root: USER, ctx: Context) => {
4444
return ctx.USER?.isSuperAdmin || ctx.USER?.id === root.id;
4545
};
4646

47+
export const areUsersOnSameTeam = async (root: USER, ctx: Context) => {
48+
const currentUserId = ctx.USER?.id;
49+
50+
if (!currentUserId) {
51+
return false;
52+
}
53+
54+
const teams = await ctx.DB.query.userTeamsSchema.findMany({
55+
where: (uts, { eq, or }) =>
56+
or(eq(uts.userId, root.id), eq(uts.userId, currentUserId)),
57+
});
58+
59+
if (teams.length !== 2) {
60+
return false;
61+
}
62+
63+
const [user1, user2] = teams;
64+
65+
return user1.teamId === user2.teamId;
66+
};
67+
4768
export const authHelpers = {
4869
isCommuntiyAdmin,
4970
isOwnerOfPurchaseOrder,

src/schema/user/types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isSuperAdminOrSelf } from "~/authz/helpers";
1+
import { areUsersOnSameTeam, isSuperAdminOrSelf } from "~/authz/helpers";
22
import { builder } from "~/builder";
33
import {
44
AllowedUserTags,
@@ -33,10 +33,14 @@ builder.objectType(UserRef, {
3333
email: t.field({
3434
type: "String",
3535
nullable: true,
36-
resolve: (root, args, ctx) => {
36+
resolve: async (root, args, ctx) => {
3737
if (isSuperAdminOrSelf(root, ctx)) {
3838
return root.email;
3939
}
40+
41+
if (await areUsersOnSameTeam(root, ctx)) {
42+
return root.email;
43+
}
4044
},
4145
}),
4246
teams: t.field({

0 commit comments

Comments
 (0)