Skip to content

NoMongo (fix): allow all organization admins to block users #3459

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 53 additions & 46 deletions src/graphql/types/Mutation/blockUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z } from "zod";
import { blockedUsersTable } from "~/src/drizzle/tables/blockedUsers";
import { builder } from "~/src/graphql/builder";
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
import { assertOrganizationAdmin } from "~/src/utilities/authorization";

const mutationBlockUserArgumentsSchema = z.object({
organizationId: z.string().min(1, "Organization ID is required."),
Expand Down Expand Up @@ -44,45 +45,29 @@ builder.mutationField("blockUser", (t) =>

const currentUserId = ctx.currentClient.user.id;

const [
organization,
targetUser,
currentUserMembership,
targetUserMembership,
existingBlock,
] = await Promise.all([
ctx.drizzleClient.query.organizationsTable.findFirst({
where: (fields, operators) =>
operators.eq(fields.id, parsedArgs.organizationId),
}),
const [currentUser, existingOrganization] = await Promise.all([
ctx.drizzleClient.query.usersTable.findFirst({
where: (fields, operators) =>
operators.eq(fields.id, parsedArgs.userId),
}),
ctx.drizzleClient.query.organizationMembershipsTable.findFirst({
where: (fields, operators) =>
operators.and(
operators.eq(fields.memberId, currentUserId),
operators.eq(fields.organizationId, parsedArgs.organizationId),
),
}),
ctx.drizzleClient.query.organizationMembershipsTable.findFirst({
where: (fields, operators) =>
operators.and(
operators.eq(fields.memberId, parsedArgs.userId),
operators.eq(fields.organizationId, parsedArgs.organizationId),
),
columns: {
role: true,
},
where: (fields, operators) => operators.eq(fields.id, currentUserId),
}),
ctx.drizzleClient.query.blockedUsersTable.findFirst({
ctx.drizzleClient.query.organizationsTable.findFirst({
with: {
membershipsWhereOrganization: {
columns: {
role: true,
},
where: (fields, operators) =>
operators.eq(fields.memberId, currentUserId),
},
},
where: (fields, operators) =>
operators.and(
operators.eq(fields.userId, parsedArgs.userId),
operators.eq(fields.organizationId, parsedArgs.organizationId),
),
operators.eq(fields.id, parsedArgs.organizationId),
}),
]);

if (!organization) {
if (!existingOrganization) {
throw new TalawaGraphQLError({
extensions: {
code: "arguments_associated_resources_not_found",
Expand All @@ -95,6 +80,41 @@ builder.mutationField("blockUser", (t) =>
});
}

const currentUserOrganizationMembership =
existingOrganization.membershipsWhereOrganization[0];

assertOrganizationAdmin(
currentUser,
currentUserOrganizationMembership,
"You must be an admin of this organization to block users.",
);

const [targetUser, targetUserMembership, existingBlock] =
await Promise.all([
ctx.drizzleClient.query.usersTable.findFirst({
columns: {
id: true,
role: true,
},
where: (fields, operators) =>
operators.eq(fields.id, parsedArgs.userId),
}),
ctx.drizzleClient.query.organizationMembershipsTable.findFirst({
where: (fields, operators) =>
operators.and(
operators.eq(fields.memberId, parsedArgs.userId),
operators.eq(fields.organizationId, parsedArgs.organizationId),
),
}),
ctx.drizzleClient.query.blockedUsersTable.findFirst({
where: (fields, operators) =>
operators.and(
operators.eq(fields.userId, parsedArgs.userId),
operators.eq(fields.organizationId, parsedArgs.organizationId),
),
}),
]);

if (!targetUser) {
throw new TalawaGraphQLError({
extensions: {
Expand All @@ -108,19 +128,6 @@ builder.mutationField("blockUser", (t) =>
});
}

if (
!currentUserMembership ||
currentUserMembership.role !== "administrator"
) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthorized_action",
message:
"You must be an admin of this organization to block users.",
},
});
}

if (existingBlock) {
throw new TalawaGraphQLError({
extensions: {
Expand Down
91 changes: 53 additions & 38 deletions src/graphql/types/Mutation/unblockUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { blockedUsersTable } from "~/src/drizzle/tables/blockedUsers";
import { builder } from "~/src/graphql/builder";
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
import { assertOrganizationAdmin } from "~/src/utilities/authorization";

const mutationUnblockUserArgumentsSchema = z.object({
organizationId: z.string().min(1, "Organization ID is required."),
Expand Down Expand Up @@ -45,33 +46,37 @@

const currentUserId = ctx.currentClient.user.id;

const [organization, targetUser, currentUserMembership, existingBlock] =
await Promise.all([
ctx.drizzleClient.query.organizationsTable.findFirst({
where: (fields, operators) =>
operators.eq(fields.id, parsedArgs.organizationId),
}),
ctx.drizzleClient.query.usersTable.findFirst({
where: (fields, operators) =>
operators.eq(fields.id, parsedArgs.userId),
}),
ctx.drizzleClient.query.organizationMembershipsTable.findFirst({
where: (fields, operators) =>
operators.and(
const [currentUser, existingOrganization] = await Promise.all([
ctx.drizzleClient.query.usersTable.findFirst({
columns: {
role: true,
},
where: (fields, operators) => operators.eq(fields.id, currentUserId),
}),
ctx.drizzleClient.query.organizationsTable.findFirst({
with: {
membershipsWhereOrganization: {
columns: {
role: true,
},
where: (fields, operators) =>
operators.eq(fields.memberId, currentUserId),
operators.eq(fields.organizationId, parsedArgs.organizationId),
),
}),
ctx.drizzleClient.query.blockedUsersTable.findFirst({
where: (fields, operators) =>
operators.and(
operators.eq(fields.userId, parsedArgs.userId),
operators.eq(fields.organizationId, parsedArgs.organizationId),
),
}),
]);
},
},
where: (fields, operators) =>
operators.eq(fields.id, parsedArgs.organizationId),
}),
]);

if (currentUser === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}

Check warning on line 77 in src/graphql/types/Mutation/unblockUser.ts

View check run for this annotation

Codecov / codecov/patch

src/graphql/types/Mutation/unblockUser.ts#L72-L77

Added lines #L72 - L77 were not covered by tests

if (!organization) {
if (existingOrganization === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "arguments_associated_resources_not_found",
Expand All @@ -84,6 +89,29 @@
});
}

const currentUserOrganizationMembership =
existingOrganization.membershipsWhereOrganization[0];

assertOrganizationAdmin(
currentUser,
currentUserOrganizationMembership,
"You must be an admin of this organization to unblock users.",
);

const [targetUser, existingBlock] = await Promise.all([
ctx.drizzleClient.query.usersTable.findFirst({
where: (fields, operators) =>
operators.eq(fields.id, parsedArgs.userId),
}),
ctx.drizzleClient.query.blockedUsersTable.findFirst({
where: (fields, operators) =>
operators.and(
operators.eq(fields.userId, parsedArgs.userId),
operators.eq(fields.organizationId, parsedArgs.organizationId),
),
}),
]);

if (!targetUser) {
throw new TalawaGraphQLError({
extensions: {
Expand All @@ -97,19 +125,6 @@
});
}

if (
!currentUserMembership ||
currentUserMembership.role !== "administrator"
) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthorized_action",
message:
"You must be an admin of this organization to unblock users.",
},
});
}

if (!existingBlock) {
throw new TalawaGraphQLError({
extensions: {
Expand Down
23 changes: 23 additions & 0 deletions src/utilities/authorization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TalawaGraphQLError } from "./TalawaGraphQLError";

interface HasRole {
role?: string | null;
}

export const assertOrganizationAdmin = (
currentUser: HasRole | undefined,
membership: HasRole | undefined,
errorMessage: string,
): void => {
if (
currentUser?.role !== "administrator" &&
membership?.role !== "administrator"

Check warning on line 14 in src/utilities/authorization.ts

View check run for this annotation

Codecov / codecov/patch

src/utilities/authorization.ts#L14

Added line #L14 was not covered by tests
) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthorized_action",
message: errorMessage,
},
});
}

Check warning on line 22 in src/utilities/authorization.ts

View check run for this annotation

Codecov / codecov/patch

src/utilities/authorization.ts#L16-L22

Added lines #L16 - L22 were not covered by tests
};
Loading