Framework.MessageSecurity (Framework v0.5.0)

View Source

MessageSecurity provides pure predicate functions compiled from AppSpec visible_if clauses. These predicates run during Accept/Plan phase and enforce the same authorization logic used for UI visibility.

Core Principle

Visibility and enforcement are the same predicate evaluated in different places.

AuthContext Structure

The context passed to visibility predicates includes:

  • actor: Current user/actor information
  • org: Organization context
  • roles: List of roles for current context
  • feature_flags: Available feature flags
  • acl_bitsets: Access control lists as bitsets
  • tenant_id: Multi-tenant identifier
  • authenticated?: Boolean authentication status

Summary

Functions

Compiles a visibility predicate from AppSpec page AST.

Creates an executable function from AST that supports param/1 calls.

Extracts the visible_if clause from page AST.

Generates verified route helpers from AppSpec definitions.

Gets the visibility predicate for a specific page from an AppSpec module.

Gets all registered AppSpec modules.

Gets the compiled visibility predicate for a given page.

Transforms predicate AST to handle param/1 calls and context access safely.

Validates route parameters against their schema.

Evaluates visibility predicate for a given page and context.

Functions

compile_visibility_predicate(page_ast)

Compiles a visibility predicate from AppSpec page AST.

create_predicate_function(ctx_var, predicate_ast)

Creates an executable function from AST that supports param/1 calls.

extract_visible_if(arg)

Extracts the visible_if clause from page AST.

generate_routes(pages)

Generates verified route helpers from AppSpec definitions.

This will be called at compile time to generate the routes() helpers.

get_page_predicate(module, page_name)

Gets the visibility predicate for a specific page from an AppSpec module.

get_registered_appspecs()

Gets all registered AppSpec modules.

get_visibility_predicate(page_name)

Gets the compiled visibility predicate for a given page.

This loads the predicate from the registered AppSpec modules.

transform_predicate_ast(ast, ctx_var_name)

Transforms predicate AST to handle param/1 calls and context access safely.

validate_params(page_name, raw_params)

Validates route parameters against their schema.

Returns {:ok, coerced_params} for valid params, {:error, :url_invalid} for invalid params.

visible?(page_name, auth_context, params \\ %{})

Evaluates visibility predicate for a given page and context.

Returns true if the page should be visible/accessible, false otherwise.

Examples

ctx = %{authenticated?: true, roles: [:member, :admin]}
MessageSecurity.visible?(:dashboard, ctx, %{})
#=> true or false