Files
metabuilder/packages/ui_auth/seed/scripts/gate.lua
JohnDoe6345789 bf1401fe34 Enhance module definitions and add type annotations across various packages
- Added type annotations and class definitions in the dashboard layout, stats, and data table modules for improved type safety and documentation.
- Introduced new classes for UI components, props, and configuration in the form builder, navigation menu, notification center, and UI dialogs packages.
- Implemented detailed type definitions for actions, fields, and pagination components to streamline usage and enhance clarity.
- Updated initialization functions in multiple packages to include versioning and installation context.
- Improved structure and readability of the codebase by organizing and documenting component properties and methods.
2025-12-30 10:21:33 +00:00

50 lines
1.1 KiB
Lua

local check = require("check")
---@class AuthGate
local M = {}
---@class User
---@field id string
---@field level? number
---@class GateContext
---@field user? User
---@field requiredLevel? number
---@field children? table
---@class CheckResult
---@field allowed boolean
---@field reason? string
---@field redirect? string
---@class UIComponent
---@field type string
---@field props? table
---@param ctx GateContext
---@return CheckResult
function M.check(ctx)
if not ctx.user then
return { allowed = false, reason = "not_authenticated", redirect = "/login" }
end
if ctx.requiredLevel and not check.can_access(ctx.user, ctx.requiredLevel) then
return { allowed = false, reason = "insufficient_permission" }
end
return { allowed = true }
end
---@param ctx GateContext
---@return UIComponent|table
function M.wrap(ctx)
local result = M.check(ctx)
if not result.allowed then
if result.redirect then
return { type = "Redirect", props = { to = result.redirect } }
end
return { type = "AccessDenied", props = { reason = result.reason } }
end
return ctx.children
end
return M