config_controller.ex 4.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.AdminAPI.ConfigController do
  use Pleroma.Web, :controller

  alias Pleroma.Config
  alias Pleroma.ConfigDB
  alias Pleroma.Plugs.OAuthScopesPlug

  @descriptions Pleroma.Docs.JSON.compile()

14
15
16
  plug(Pleroma.Web.ApiSpec.CastAndValidate)
  plug(OAuthScopesPlug, %{scopes: ["write"], admin: true} when action == :update)

17
18
19
20
21
22
23
24
  plug(
    OAuthScopesPlug,
    %{scopes: ["read"], admin: true}
    when action in [:show, :descriptions]
  )

  action_fallback(Pleroma.Web.AdminAPI.FallbackController)

25
26
  defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ConfigOperation

27
28
29
30
31
32
  def descriptions(conn, _params) do
    descriptions = Enum.filter(@descriptions, &whitelisted_config?/1)

    json(conn, descriptions)
  end

33
  def show(conn, %{only_db: true}) do
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    with :ok <- configurable_from_database() do
      configs = Pleroma.Repo.all(ConfigDB)
      render(conn, "index.json", %{configs: configs})
    end
  end

  def show(conn, _params) do
    with :ok <- configurable_from_database() do
      configs = ConfigDB.get_all_as_keyword()

      merged =
        Config.Holder.default_config()
        |> ConfigDB.merge(configs)
        |> Enum.map(fn {group, values} ->
          Enum.map(values, fn {key, value} ->
            db =
              if configs[group][key] do
                ConfigDB.get_db_keys(configs[group][key], key)
              end

            db_value = configs[group][key]

            merged_value =
              if not is_nil(db_value) and Keyword.keyword?(db_value) and
                   ConfigDB.sub_key_full_update?(group, key, Keyword.keys(db_value)) do
                ConfigDB.merge_group(group, key, value, db_value)
              else
                value
              end

64
            %{
65
66
67
68
              group: ConfigDB.convert(group),
              key: ConfigDB.convert(key),
              value: ConfigDB.convert(merged_value)
            }
69
            |> Pleroma.Maps.put_if_present(:db, db)
70
71
72
73
74
75
76
77
          end)
        end)
        |> List.flatten()

      json(conn, %{configs: merged, need_reboot: Restarter.Pleroma.need_reboot?()})
    end
  end

78
  def update(%{body_params: %{configs: configs}} = conn, _) do
79
80
81
82
83
    with :ok <- configurable_from_database() do
      results =
        configs
        |> Enum.filter(&whitelisted_config?/1)
        |> Enum.map(fn
84
85
          %{group: group, key: key, delete: true} = params ->
            ConfigDB.delete(%{group: group, key: key, subkeys: params[:subkeys]})
86

87
          %{group: group, key: key, value: value} ->
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
            ConfigDB.update_or_create(%{group: group, key: key, value: value})
        end)
        |> Enum.reject(fn {result, _} -> result == :error end)

      {deleted, updated} =
        results
        |> Enum.map(fn {:ok, config} ->
          Map.put(config, :db, ConfigDB.get_db_keys(config))
        end)
        |> Enum.split_with(fn config ->
          Ecto.get_meta(config, :state) == :deleted
        end)

      Config.TransferTask.load_and_update_env(deleted, false)

      if not Restarter.Pleroma.need_reboot?() do
        changed_reboot_settings? =
          (updated ++ deleted)
          |> Enum.any?(fn config ->
            group = ConfigDB.from_string(config.group)
            key = ConfigDB.from_string(config.key)
            value = ConfigDB.from_binary(config.value)
            Config.TransferTask.pleroma_need_restart?(group, key, value)
          end)

        if changed_reboot_settings?, do: Restarter.Pleroma.need_reboot()
      end

      render(conn, "index.json", %{
        configs: updated,
        need_reboot: Restarter.Pleroma.need_reboot?()
      })
    end
  end

  defp configurable_from_database do
    if Config.get(:configurable_from_database) do
      :ok
    else
      {:error, "To use this endpoint you need to enable configuration from database."}
    end
  end

  defp whitelisted_config?(group, key) do
    if whitelisted_configs = Config.get(:database_config_whitelist) do
      Enum.any?(whitelisted_configs, fn
        {whitelisted_group} ->
          group == inspect(whitelisted_group)

        {whitelisted_group, whitelisted_key} ->
          group == inspect(whitelisted_group) && key == inspect(whitelisted_key)
      end)
    else
      true
    end
  end

145
  defp whitelisted_config?(%{group: group, key: key}) do
146
147
148
    whitelisted_config?(group, key)
  end

149
  defp whitelisted_config?(%{group: group} = config) do
150
151
152
    whitelisted_config?(group, config[:key])
  end
end