2018-12-23 12:04:54 -08:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 14:44:49 -08:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 12:04:54 -08:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-11-06 07:00:48 -08:00
|
|
|
defmodule Pleroma.Config do
|
2018-11-06 10:34:57 -08:00
|
|
|
defmodule Error do
|
|
|
|
defexception [:message]
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(key), do: get(key, nil)
|
|
|
|
|
|
|
|
def get([key], default), do: get(key, default)
|
2018-11-06 07:00:48 -08:00
|
|
|
|
2018-11-06 10:34:57 -08:00
|
|
|
def get([parent_key | keys], default) do
|
2019-05-13 15:40:59 -07:00
|
|
|
case :pleroma
|
|
|
|
|> Application.get_env(parent_key)
|
|
|
|
|> get_in(keys) do
|
|
|
|
nil -> default
|
|
|
|
any -> any
|
|
|
|
end
|
2018-11-06 07:00:48 -08:00
|
|
|
end
|
|
|
|
|
2018-11-06 10:34:57 -08:00
|
|
|
def get(key, default) do
|
|
|
|
Application.get_env(:pleroma, key, default)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get!(key) do
|
|
|
|
value = get(key, nil)
|
|
|
|
|
|
|
|
if value == nil do
|
|
|
|
raise(Error, message: "Missing configuration value: #{inspect(key)}")
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
2018-11-06 07:00:48 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def put([key], value), do: put(key, value)
|
|
|
|
|
|
|
|
def put([parent_key | keys], value) do
|
|
|
|
parent =
|
2019-06-30 00:28:35 -07:00
|
|
|
Application.get_env(:pleroma, parent_key, [])
|
2018-11-06 07:00:48 -08:00
|
|
|
|> put_in(keys, value)
|
|
|
|
|
|
|
|
Application.put_env(:pleroma, parent_key, parent)
|
|
|
|
end
|
|
|
|
|
|
|
|
def put(key, value) do
|
|
|
|
Application.put_env(:pleroma, key, value)
|
|
|
|
end
|
2018-11-30 09:19:22 -08:00
|
|
|
|
|
|
|
def delete([key]), do: delete(key)
|
|
|
|
|
|
|
|
def delete([parent_key | keys]) do
|
|
|
|
{_, parent} =
|
|
|
|
Application.get_env(:pleroma, parent_key)
|
|
|
|
|> get_and_update_in(keys, fn _ -> :pop end)
|
|
|
|
|
|
|
|
Application.put_env(:pleroma, parent_key, parent)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(key) do
|
|
|
|
Application.delete_env(:pleroma, key)
|
|
|
|
end
|
2019-04-05 05:12:02 -07:00
|
|
|
|
|
|
|
def oauth_consumer_strategies, do: get([:auth, :oauth_consumer_strategies], [])
|
|
|
|
|
|
|
|
def oauth_consumer_enabled?, do: oauth_consumer_strategies() != []
|
2019-12-05 13:25:44 -08:00
|
|
|
|
|
|
|
def enforce_oauth_admin_scope_usage?, do: !!get([:auth, :enforce_oauth_admin_scope_usage])
|
|
|
|
|
2019-12-11 00:42:02 -08:00
|
|
|
def oauth_admin_scopes(scopes) when is_list(scopes) do
|
|
|
|
Enum.flat_map(
|
|
|
|
scopes,
|
|
|
|
fn scope ->
|
|
|
|
["admin:#{scope}"] ++
|
|
|
|
if enforce_oauth_admin_scope_usage?(), do: [], else: [scope]
|
|
|
|
end
|
|
|
|
)
|
2019-12-05 13:25:44 -08:00
|
|
|
end
|
2018-11-06 07:00:48 -08:00
|
|
|
end
|