View Source Bonfire.Common.Settings (Bonfire v0.9.10-classic-beta.169)
Helpers to get app/extension settings, or to override a config key.
This module provides functionality for fetching and updating application and extension settings. The process for fetching settings follows a bottom-up system of overrides:
User-specific settings: If
opts
containscurrent_user
, settings are fetched from the user's settings.Account-specific settings: If no settings are found for the user and
opts
containscurrent_account
, settings are fetched from the account's settings.Instance-specific settings: NOTE: Changes to instance settings are stored both in the database and the OTP app config/application environment, and are loaded from the DB into OTP config at app startup by
Bonfire.Common.Settings.LoadInstanceConfig
.Default OTP config: If no settings are found at the user or account level, instance settings are loaded from OTP application configuration via
Bonfire.Common.Config
.Default value: If no settings are found in the previous steps, the provided
default
value is returned.
Summary
Functions
Retrieves the setting value for a given config key or nested key path.
Retrieves the setting value for a given config key like in get/3
, but raises an exception if the key is not found, ensuring that the setting must be present.
Sets a value for a specific key or list of nested keys.
Resets all settings.
Resets settings for the instance scope.
Sets multiple settings at once.
Functions
Retrieves the setting value for a given config key or nested key path.
As explained above, this function checks settings in the following order:
- User settings (if
opts
containscurrent_user
). - Account settings (if
opts
containscurrent_account
and no user settings are found). - Instance settings.
- OTP application config (includes compile time and runtime config).
- Default value (if no settings are found).
Examples
iex> get(:test_key)
"test_value"
iex> get([:non_existing_extension, :sub_key])
nil
iex> get(:non_existing_key, "default")
"default"
iex> get(:otp_app)
:bonfire
iex> get([:bonfire_common, :otp_app])
:bonfire
iex> get([:bonfire_common, :otp_app])
:bonfire
iex> get([Bonfire.Common.Localise.Cldr, :gettext])
Bonfire.Common.Localise.Gettext
iex> get([:bonfire_common, Bonfire.Common.Localise.Cldr, :gettext])
Bonfire.Common.Localise.Gettext
Options
:otp_app
- Optionally specifies the OTP application for which to fetch settings. If none is specified, it will look at the (first) key and check if it references a known OTP application (i.e. an extension) or a module, in which case it will fetch settings from that application. Otherwise it will look in the configured top-level OTP app (seeConfig.top_level_otp_app/0
).:scope
- Optionally defines the scope for settings retrieval (e.g.,:user
,:account
, or:instance
).
Retrieves the setting value for a given config key like in get/3
, but raises an exception if the key is not found, ensuring that the setting must be present.
Examples
iex> get!(:test_key)
"test_value"
iex> get!(:non_existing_key)
** (RuntimeError) Missing setting or configuration value: :non_existing_key
Sets a value for a specific key or list of nested keys.
This function updates the configuration with the provided value. You can specify a single key or a list of nested keys.
Examples
# when no scope or current_user are passed in opts:
> put(:some_key, "new_value")
{:error, "You need to be authenticated to change settings."}
# when the scope is :instance but an admin isn't passed as current_user in opts:
> put(:some_key, "new_value", scope: :instance)
** (Bonfire.Fail) You do not have permission to change instance settings. Please contact an admin.
> {:ok, %Bonfire.Data.Identity.Settings{}} = put(:some_key, "new_value", skip_boundary_check: true, scope: :instance)
> {:ok, %Bonfire.Data.Identity.Settings{}} = put([:top_key, :sub_key], "new_value", skip_boundary_check: true, scope: "instance")
Options
:otp_app
- Specifies the OTP application for which to set settings. If not specified, it decides where to put it using the same logic asget/3
.:scope
- Defines the scope for settings (e.g.,:user
,:account
, or:instance
).
Resets all settings.
This function deletes all settings from the database, including instance-specific settings and user-specific settings for all users. Please be careful!
Examples
> reset_all()
{:ok, %{}}
Resets settings for the instance scope.
This function deletes the settings associated with the whole instance and returns the result.
Examples
> reset_instance()
{:ok, %Bonfire.Data.Identity.Settings{id: "some_id"}}
Sets multiple settings at once.
This function accepts a map or keyword list of settings to be updated. It determines the appropriate scope and updates the settings accordingly.
Examples
> {:ok, %Bonfire.Data.Identity.Settings{}} = set(%{some_key: "value", another_key: "another_value"}, skip_boundary_check: true, scope: :instance)
> {:ok, %Bonfire.Data.Identity.Settings{}} = set([some_key: "value", another_key: "another_value"], skip_boundary_check: true, scope: "instance")
Options
:otp_app
- Specifies the OTP application for which to set settings.:scope
- Defines the scope for settings (e.g.,:user
,:account
, or:instance
).