mirror of
https://github.com/chillpadclub/bedolaga-cabinet.git
synced 2026-07-30 02:23:47 +00:00
feat: add weekdays condition to ABAC policies
This commit is contained in:
@@ -86,12 +86,23 @@ const BoltIcon = () => (
|
||||
</svg>
|
||||
);
|
||||
|
||||
const CalendarIcon = () => (
|
||||
<svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
d="M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 012.25-2.25h13.5A2.25 2.25 0 0121 7.5v11.25m-18 0A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75m-18 0v-7.5A2.25 2.25 0 015.25 9h13.5A2.25 2.25 0 0121 11.25v7.5"
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
|
||||
// === Helpers ===
|
||||
|
||||
interface PolicyConditions {
|
||||
time_range?: { start: string; end: string };
|
||||
ip_whitelist?: string[];
|
||||
rate_limit?: number;
|
||||
weekdays?: number[];
|
||||
}
|
||||
|
||||
function parseConditions(raw: Record<string, unknown>): PolicyConditions {
|
||||
@@ -112,6 +123,10 @@ function parseConditions(raw: Record<string, unknown>): PolicyConditions {
|
||||
result.rate_limit = raw.rate_limit;
|
||||
}
|
||||
|
||||
if (Array.isArray(raw.weekdays)) {
|
||||
result.weekdays = raw.weekdays.filter((d): d is number => typeof d === 'number');
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -239,6 +254,22 @@ export default function AdminPolicies() {
|
||||
);
|
||||
}
|
||||
|
||||
if (parsed.weekdays && parsed.weekdays.length > 0 && parsed.weekdays.length < 7) {
|
||||
const dayOrder = [1, 2, 3, 4, 5, 6, 0];
|
||||
const sorted = dayOrder.filter((d) => parsed.weekdays!.includes(d));
|
||||
const dayNames = sorted.map((d) => t(`admin.policies.conditions.day${d}`));
|
||||
icons.push(
|
||||
<span
|
||||
key="weekdays"
|
||||
className="inline-flex items-center gap-1 rounded bg-dark-700 px-1.5 py-0.5 text-xs text-dark-300"
|
||||
title={t('admin.policies.conditions.weekdays')}
|
||||
>
|
||||
<CalendarIcon />
|
||||
{dayNames.join(', ')}
|
||||
</span>,
|
||||
);
|
||||
}
|
||||
|
||||
return icons;
|
||||
},
|
||||
[t],
|
||||
|
||||
@@ -11,6 +11,7 @@ interface PolicyConditions {
|
||||
time_range?: { start: string; end: string };
|
||||
ip_whitelist?: string[];
|
||||
rate_limit?: number;
|
||||
weekdays?: number[];
|
||||
}
|
||||
|
||||
interface PolicyFormData {
|
||||
@@ -26,6 +27,7 @@ interface PolicyFormData {
|
||||
time_range: boolean;
|
||||
ip_whitelist: boolean;
|
||||
rate_limit: boolean;
|
||||
weekdays: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -41,11 +43,13 @@ const INITIAL_FORM: PolicyFormData = {
|
||||
time_range: { start: '09:00', end: '18:00' },
|
||||
ip_whitelist: [],
|
||||
rate_limit: 100,
|
||||
weekdays: [1, 2, 3, 4, 5],
|
||||
},
|
||||
conditionsEnabled: {
|
||||
time_range: false,
|
||||
ip_whitelist: false,
|
||||
rate_limit: false,
|
||||
weekdays: false,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -69,6 +73,10 @@ function parseConditions(raw: Record<string, unknown>): PolicyConditions {
|
||||
result.rate_limit = raw.rate_limit;
|
||||
}
|
||||
|
||||
if (Array.isArray(raw.weekdays)) {
|
||||
result.weekdays = raw.weekdays.filter((d): d is number => typeof d === 'number');
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -87,6 +95,9 @@ function buildConditionsPayload(
|
||||
if (enabled.rate_limit && conditions.rate_limit !== undefined) {
|
||||
result.rate_limit = conditions.rate_limit;
|
||||
}
|
||||
if (enabled.weekdays && conditions.weekdays && conditions.weekdays.length > 0) {
|
||||
result.weekdays = conditions.weekdays;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -232,11 +243,13 @@ export default function AdminPolicyEdit() {
|
||||
time_range: parsed.time_range ?? { start: '09:00', end: '18:00' },
|
||||
ip_whitelist: parsed.ip_whitelist ?? [],
|
||||
rate_limit: parsed.rate_limit ?? 100,
|
||||
weekdays: parsed.weekdays ?? [1, 2, 3, 4, 5],
|
||||
},
|
||||
conditionsEnabled: {
|
||||
time_range: !!parsed.time_range,
|
||||
ip_whitelist: !!parsed.ip_whitelist && parsed.ip_whitelist.length > 0,
|
||||
rate_limit: parsed.rate_limit !== undefined,
|
||||
weekdays: !!parsed.weekdays && parsed.weekdays.length > 0,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -687,6 +700,53 @@ export default function AdminPolicyEdit() {
|
||||
</span>
|
||||
</div>
|
||||
</ConditionToggle>
|
||||
|
||||
{/* Weekdays */}
|
||||
<ConditionToggle
|
||||
label={t('admin.policies.conditions.weekdays')}
|
||||
enabled={formData.conditionsEnabled.weekdays}
|
||||
onToggle={() =>
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
conditionsEnabled: {
|
||||
...prev.conditionsEnabled,
|
||||
weekdays: !prev.conditionsEnabled.weekdays,
|
||||
},
|
||||
}))
|
||||
}
|
||||
>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{[1, 2, 3, 4, 5, 6, 0].map((day) => {
|
||||
const selected = (formData.conditions.weekdays ?? []).includes(day);
|
||||
return (
|
||||
<button
|
||||
key={day}
|
||||
type="button"
|
||||
onClick={() =>
|
||||
setFormData((prev) => {
|
||||
const current = prev.conditions.weekdays ?? [];
|
||||
const next = selected
|
||||
? current.filter((d) => d !== day)
|
||||
: [...current, day];
|
||||
return {
|
||||
...prev,
|
||||
conditions: { ...prev.conditions, weekdays: next },
|
||||
};
|
||||
})
|
||||
}
|
||||
className={`rounded-lg px-3 py-1.5 text-xs font-medium transition-colors ${
|
||||
selected
|
||||
? 'bg-accent-500/20 text-accent-400'
|
||||
: 'bg-dark-700/50 text-dark-400 hover:bg-dark-700 hover:text-dark-300'
|
||||
}`}
|
||||
aria-pressed={selected}
|
||||
>
|
||||
{t(`admin.policies.conditions.day${day}`)}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</ConditionToggle>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user