Raxol.Security.UserContext (Raxol v2.0.1)

View Source

Refactored Security User Context module with GenServer-based state management.

This module provides user context management for security operations, eliminating Process dictionary usage in favor of supervised state.

Migration Notes

This module replaces Process.get(:current_user) calls in encryption modules with proper OTP-compliant state management.

Summary

Functions

Records an audit log entry for the current user.

Clears the current user.

Gets security context for the current operation.

Gets the current user for security operations.

Sets security context for the current operation.

Sets the current user for security operations.

Executes a function with a specific user context.

Functions

audit_log(action, details \\ %{})

Records an audit log entry for the current user.

Examples

iex> audit_log(:encrypt_data, %{file: "sensitive.txt"})
:ok

clear_current_user()

Clears the current user.

Examples

iex> clear_current_user()
:ok

get_context(key, default \\ nil)

Gets security context for the current operation.

Examples

iex> get_context(:encryption_key_id)
"key_123"

get_current_user()

Gets the current user for security operations.

Examples

iex> get_current_user()
"alice"

set_context(key, value)

Sets security context for the current operation.

Examples

iex> set_context(:encryption_key_id, "key_123")
:ok

set_current_user(user_id)

Sets the current user for security operations.

Examples

iex> set_current_user("alice")
:ok

with_user(user_id, fun)

Executes a function with a specific user context.

Examples

iex> with_user("bob", fn ->
...>   # Operations will be performed as "bob"
...>   encrypt_file("data.txt")
...> end)