mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 21:06:04 +00:00
Enhance Guard and Form components with improved error handling and debugging
- Added debug logging in the `Guard` class to track policy evaluations and conditions. - Updated error logging in the `Form` component to provide more context on invalid submissions. - Introduced state management for form errors in the `AuthRolesEdit` component, displaying alerts for invalid data submissions.
This commit is contained in:
@@ -203,14 +203,17 @@ export class Guard {
|
|||||||
// set the default effect of the role permission
|
// set the default effect of the role permission
|
||||||
let allowed = rolePermission.effect === "allow";
|
let allowed = rolePermission.effect === "allow";
|
||||||
for (const policy of rolePermission.policies) {
|
for (const policy of rolePermission.policies) {
|
||||||
|
$console.debug("guard: checking policy", { policy: policy.toJSON(), ctx });
|
||||||
// skip filter policies
|
// skip filter policies
|
||||||
if (policy.content.effect === "filter") continue;
|
if (policy.content.effect === "filter") continue;
|
||||||
|
|
||||||
// if condition is met, check the effect
|
// if condition is met, check the effect
|
||||||
const meets = policy.meetsCondition(ctx);
|
const meets = policy.meetsCondition(ctx);
|
||||||
if (meets) {
|
if (meets) {
|
||||||
|
$console.debug("guard: policy meets condition");
|
||||||
// if deny, then break early
|
// if deny, then break early
|
||||||
if (policy.content.effect === "deny") {
|
if (policy.content.effect === "deny") {
|
||||||
|
$console.debug("guard: policy is deny, setting allowed to false");
|
||||||
allowed = false;
|
allowed = false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -218,6 +221,8 @@ export class Guard {
|
|||||||
} else if (policy.content.effect === "allow") {
|
} else if (policy.content.effect === "allow") {
|
||||||
allowed = true;
|
allowed = true;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$console.debug("guard: policy does not meet condition");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ export function Form<
|
|||||||
if (errors.length === 0) {
|
if (errors.length === 0) {
|
||||||
await onSubmit(data as Data);
|
await onSubmit(data as Data);
|
||||||
} else {
|
} else {
|
||||||
console.log("invalid", errors);
|
console.error("form: invalid", { data, errors });
|
||||||
onInvalidSubmit?.(errors, data);
|
onInvalidSubmit?.(errors, data);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ import { cn } from "ui/lib/utils";
|
|||||||
import { JsonViewer } from "ui/components/code/JsonViewer";
|
import { JsonViewer } from "ui/components/code/JsonViewer";
|
||||||
import { mountOnce, useApiQuery } from "ui/client";
|
import { mountOnce, useApiQuery } from "ui/client";
|
||||||
import { CodePreview } from "ui/components/code/CodePreview";
|
import { CodePreview } from "ui/components/code/CodePreview";
|
||||||
|
import type { JsonError } from "json-schema-library";
|
||||||
|
import { Alert } from "ui/components/display/Alert";
|
||||||
|
|
||||||
export function AuthRolesEdit(props) {
|
export function AuthRolesEdit(props) {
|
||||||
useBrowserTitle(["Auth", "Roles", props.params.role]);
|
useBrowserTitle(["Auth", "Roles", props.params.role]);
|
||||||
@@ -72,6 +74,7 @@ const formConfig = {
|
|||||||
function AuthRolesEditInternal({ params }: { params: { role: string } }) {
|
function AuthRolesEditInternal({ params }: { params: { role: string } }) {
|
||||||
const [navigate] = useNavigate();
|
const [navigate] = useNavigate();
|
||||||
const { config, schema: authSchema, actions } = useBkndAuth();
|
const { config, schema: authSchema, actions } = useBkndAuth();
|
||||||
|
const [error, setError] = useState<JsonError[]>();
|
||||||
const roleName = params.role;
|
const roleName = params.role;
|
||||||
const role = config.roles?.[roleName];
|
const role = config.roles?.[roleName];
|
||||||
const { readonly, permissions } = useBknd();
|
const { readonly, permissions } = useBknd();
|
||||||
@@ -91,6 +94,7 @@ function AuthRolesEditInternal({ params }: { params: { role: string } }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
async function handleUpdate(data: any) {
|
async function handleUpdate(data: any) {
|
||||||
|
setError(undefined);
|
||||||
await actions.roles.patch(roleName, data);
|
await actions.roles.patch(roleName, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,10 +106,13 @@ function AuthRolesEditInternal({ params }: { params: { role: string } }) {
|
|||||||
beforeSubmit={(data) => {
|
beforeSubmit={(data) => {
|
||||||
return {
|
return {
|
||||||
...data,
|
...data,
|
||||||
permissions: [...Object.values(data.permissions)],
|
permissions: [...Object.values(data.permissions).filter(Boolean)],
|
||||||
};
|
};
|
||||||
}}
|
}}
|
||||||
onSubmit={handleUpdate}
|
onSubmit={handleUpdate}
|
||||||
|
onInvalidSubmit={(errors) => {
|
||||||
|
setError(errors);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<AppShell.SectionHeader
|
<AppShell.SectionHeader
|
||||||
right={
|
right={
|
||||||
@@ -160,6 +167,7 @@ function AuthRolesEditInternal({ params }: { params: { role: string } }) {
|
|||||||
/>
|
/>
|
||||||
</AppShell.SectionHeader>
|
</AppShell.SectionHeader>
|
||||||
<AppShell.Scrollable>
|
<AppShell.Scrollable>
|
||||||
|
{error && <Alert.Exception message={"Invalid form data"} />}
|
||||||
<div className="flex flex-col flex-grow px-5 py-5 gap-8">
|
<div className="flex flex-col flex-grow px-5 py-5 gap-8">
|
||||||
<div className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
<Permissions />
|
<Permissions />
|
||||||
|
|||||||
Reference in New Issue
Block a user