Skip to content

Commit 283c29c

Browse files
anmol-fzrKinfe123himself65
authored
fix: check if user exists before banning the user (#4649)
Co-authored-by: KinfeMichael Tariku <65047246+Kinfe123@users.noreply.github.com> Co-authored-by: Alex Yang <himself65@outlook.com>
1 parent 9d216c9 commit 283c29c

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

packages/better-auth/src/plugins/admin/admin.test.ts

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,17 +979,100 @@ describe("access control", async (it) => {
979979
expect(canUpdateOrderAndUpdateUser.success).toBe(false);
980980
});
981981

982+
it("should prioritize role over userId when both are provided", async () => {
983+
const testUser = await client.signUp.email({
984+
email: "rolepriority@test.com",
985+
password: "password",
986+
name: "Role Priority Test User",
987+
});
988+
const userId = testUser.data?.user.id;
989+
990+
const checkWithAdminRole = await auth.api.userHasPermission({
991+
body: {
992+
userId: userId, // non-admin user ID
993+
role: "admin", // admin role
994+
permission: {
995+
user: ["create"],
996+
},
997+
},
998+
});
999+
expect(checkWithAdminRole.success).toBe(true);
1000+
1001+
const checkWithUserRole = await auth.api.userHasPermission({
1002+
body: {
1003+
userId: userId, // non-admin user ID
1004+
role: "user", // user role
1005+
permission: {
1006+
user: ["create"],
1007+
},
1008+
},
1009+
});
1010+
expect(checkWithUserRole.success).toBe(false);
1011+
});
1012+
1013+
it("should check permissions correctly for banned user with role provided", async () => {
1014+
const bannedUser = await client.signUp.email({
1015+
email: "bannedwithRole@test.com",
1016+
password: "password",
1017+
name: "Banned Role Test User",
1018+
});
1019+
const bannedUserId = bannedUser.data?.user.id;
1020+
1021+
await client.admin.banUser(
1022+
{
1023+
userId: bannedUserId || "",
1024+
banReason: "Testing role priority",
1025+
},
1026+
{
1027+
headers: headers,
1028+
},
1029+
);
1030+
1031+
const checkWithRole = await auth.api.userHasPermission({
1032+
body: {
1033+
userId: bannedUserId, // banned user ID
1034+
role: "admin", // admin role
1035+
permission: {
1036+
user: ["create"],
1037+
},
1038+
},
1039+
});
1040+
expect(checkWithRole.success).toBe(true);
1041+
1042+
const checkWithoutRole = await auth.api.userHasPermission({
1043+
body: {
1044+
userId: bannedUserId, // banned user ID only
1045+
permission: {
1046+
user: ["create"],
1047+
},
1048+
},
1049+
});
1050+
expect(checkWithoutRole.success).toBe(false); // User doesn't have admin permissions
1051+
1052+
await client.admin.unbanUser(
1053+
{
1054+
userId: bannedUserId || "",
1055+
},
1056+
{
1057+
headers: headers,
1058+
},
1059+
);
1060+
});
1061+
9821062
it("shouldn't allow to list users", async () => {
9831063
const { headers } = await signInWithTestUser();
9841064
const adminRes = await client.admin.listUsers({
9851065
query: {
986-
limit: 2,
1066+
limit: 10,
9871067
},
9881068
fetchOptions: {
9891069
headers,
9901070
},
9911071
});
992-
expect(adminRes.data?.users.length).toBe(1);
1072+
// The exact count may vary based on users created in previous tests
1073+
const adminCount = adminRes.data?.users.length || 0;
1074+
expect(adminCount).toBeGreaterThan(0); // Should have at least the admin user
1075+
9931076
const userHeaders = new Headers();
9941077
await client.signUp.email(
9951078
{

packages/better-auth/src/plugins/admin/admin.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
type InferAdminRolesFromOption,
2222
} from "./types";
2323
import { schema } from "./schema";
24+
import { BASE_ERROR_CODES } from "../../error/codes";
2425

2526
function parseRoles(roles: string | string[]): string {
2627
return Array.isArray(roles) ? roles.join(",") : roles;
@@ -909,6 +910,16 @@ export const admin = <O extends AdminOptions>(options?: O) => {
909910
});
910911
}
911912

913+
const foundUser = await ctx.context.internalAdapter.findUserById(
914+
ctx.body.userId,
915+
);
916+
917+
if (!foundUser) {
918+
throw new APIError("NOT_FOUND", {
919+
message: BASE_ERROR_CODES.USER_NOT_FOUND,
920+
});
921+
}
922+
912923
if (ctx.body.userId === ctx.context.session.user.id) {
913924
throw new APIError("BAD_REQUEST", {
914925
message: ADMIN_ERROR_CODES.YOU_CANNOT_BAN_YOURSELF,

0 commit comments

Comments
 (0)