feat: implements basic user locking for admins (#12393)
* feat: implements basic user locking for admins * Update sendPasswordReset.handler.ts * check fixes * Update packages/features/ee/users/components/UsersTable.tsx Co-authored-by: sean-brydon <[email protected]> --------- Co-authored-by: sean-brydon <[email protected]>
This commit is contained in:
1 parent
176564f4ed
commit
c818ef3188
12 files changed
+123
-43
No files matched your search
@@ -16,4 +16,5 @@ export enum ErrorCode {
|
||||
ThirdPartyIdentityProviderEnabled = "third-party-identity-provider-enabled",
|
||||
RateLimitExceeded = "rate-limit-exceeded",
|
||||
SocialIdentityProviderRequired = "social-identity-provider-required",
|
||||
UserAccountLocked = "user-account-locked",
|
||||
}
|
||||
@@ -105,6 +105,7 @@ const providers: Provider[] = [
|
||||
email: credentials.email.toLowerCase(),
|
||||
},
|
||||
select: {
|
||||
locked: true,
|
||||
role: true,
|
||||
id: true,
|
||||
username: true,
|
||||
@@ -136,6 +137,11 @@ const providers: Provider[] = [
|
||||
throw new Error(ErrorCode.IncorrectEmailPassword);
|
||||
}
|
||||
|
||||
// Locked users cannot login
|
||||
if (user.locked) {
|
||||
throw new Error(ErrorCode.UserAccountLocked);
|
||||
}
|
||||
|
||||
await checkRateLimitAndThrowError({
|
||||
identifier: user.email,
|
||||
});
|
||||
|
||||
@@ -76,6 +76,31 @@ function UsersTableBare() {
|
||||
},
|
||||
});
|
||||
|
||||
const lockUserAccount = trpc.viewer.admin.lockUserAccount.useMutation({
|
||||
onSuccess: ({ userId, locked }) => {
|
||||
showToast(locked ? "User was locked" : "User was unlocked", "success");
|
||||
utils.viewer.admin.listPaginated.setInfiniteData({ limit: FETCH_LIMIT }, (cachedData) => {
|
||||
if (!cachedData) {
|
||||
return {
|
||||
pages: [],
|
||||
pageParams: [],
|
||||
};
|
||||
}
|
||||
return {
|
||||
...cachedData,
|
||||
pages: cachedData.pages.map((page) => ({
|
||||
...page,
|
||||
rows: page.rows.map((row) => {
|
||||
const newUser = row;
|
||||
if (row.id === userId) newUser.locked = locked;
|
||||
return newUser;
|
||||
}),
|
||||
})),
|
||||
};
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
//we must flatten the array of arrays from the useInfiniteQuery hook
|
||||
const flatData = useMemo(() => data?.pages?.flatMap((page) => page.rows) ?? [], [data]);
|
||||
const totalDBRowCount = data?.pages?.[0]?.meta?.totalRowCount ?? 0;
|
||||
@@ -140,6 +165,11 @@ function UsersTableBare() {
|
||||
<div className="text-subtle ml-4 font-medium">
|
||||
<span className="text-default">{user.name}</span>
|
||||
<span className="ml-3">/{user.username}</span>
|
||||
{user.locked && (
|
||||
<span className="ml-3">
|
||||
<Lock />
|
||||
</span>
|
||||
)}
|
||||
<br />
|
||||
<span className="break-all">{user.email}</span>
|
||||
</div>
|
||||
@@ -167,6 +197,12 @@ function UsersTableBare() {
|
||||
onClick: () => sendPasswordResetEmail.mutate({ userId: user.id }),
|
||||
icon: Lock,
|
||||
},
|
||||
{
|
||||
id: "lock-user",
|
||||
label: user.locked ? "Unlock User Account" : "Lock User Account",
|
||||
onClick: () => lockUserAccount.mutate({ userId: user.id, locked: !user.locked }),
|
||||
icon: Lock,
|
||||
},
|
||||
{
|
||||
id: "delete",
|
||||
label: "Delete",
|
||||
|
||||
Reference in new issue
Block a user