CommonValidations: Extract modification right checker
[akkoma] / lib / pleroma / web / activity_pub / object_validators / common_validations.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
6 import Ecto.Changeset
7
8 alias Pleroma.Activity
9 alias Pleroma.Object
10 alias Pleroma.User
11
12 def validate_any_presence(cng, fields) do
13 non_empty =
14 fields
15 |> Enum.map(fn field -> get_field(cng, field) end)
16 |> Enum.any?(fn
17 [] -> false
18 _ -> true
19 end)
20
21 if non_empty do
22 cng
23 else
24 fields
25 |> Enum.reduce(cng, fn field, cng ->
26 cng
27 |> add_error(field, "none of #{inspect(fields)} present")
28 end)
29 end
30 end
31
32 def validate_actor_presence(cng, options \\ []) do
33 field_name = Keyword.get(options, :field_name, :actor)
34
35 cng
36 |> validate_change(field_name, fn field_name, actor ->
37 case User.get_cached_by_ap_id(actor) do
38 %User{deactivated: true} ->
39 [{field_name, "user is deactivated"}]
40
41 %User{} ->
42 []
43
44 _ ->
45 [{field_name, "can't find user"}]
46 end
47 end)
48 end
49
50 def validate_object_presence(cng, options \\ []) do
51 field_name = Keyword.get(options, :field_name, :object)
52 allowed_types = Keyword.get(options, :allowed_types, false)
53
54 cng
55 |> validate_change(field_name, fn field_name, object_id ->
56 object = Object.get_cached_by_ap_id(object_id) || Activity.get_by_ap_id(object_id)
57
58 cond do
59 !object ->
60 [{field_name, "can't find object"}]
61
62 object && allowed_types && object.data["type"] not in allowed_types ->
63 [{field_name, "object not in allowed types"}]
64
65 true ->
66 []
67 end
68 end)
69 end
70
71 def validate_object_or_user_presence(cng, options \\ []) do
72 field_name = Keyword.get(options, :field_name, :object)
73 options = Keyword.put(options, :field_name, field_name)
74
75 actor_cng =
76 cng
77 |> validate_actor_presence(options)
78
79 object_cng =
80 cng
81 |> validate_object_presence(options)
82
83 if actor_cng.valid?, do: actor_cng, else: object_cng
84 end
85
86 def validate_host_match(cng, fields \\ [:id, :actor]) do
87 unique_hosts =
88 fields
89 |> Enum.map(fn field ->
90 %URI{host: host} =
91 cng
92 |> get_field(field)
93 |> URI.parse()
94
95 host
96 end)
97 |> Enum.uniq()
98 |> Enum.count()
99
100 if unique_hosts == 1 do
101 cng
102 else
103 fields
104 |> Enum.reduce(cng, fn field, cng ->
105 cng
106 |> add_error(field, "hosts of #{inspect(fields)} aren't matching")
107 end)
108 end
109 end
110
111 def validate_fields_match(cng, fields) do
112 unique_fields =
113 fields
114 |> Enum.map(fn field -> get_field(cng, field) end)
115 |> Enum.uniq()
116 |> Enum.count()
117
118 if unique_fields == 1 do
119 cng
120 else
121 fields
122 |> Enum.reduce(cng, fn field, cng ->
123 cng
124 |> add_error(field, "Fields #{inspect(fields)} aren't matching")
125 end)
126 end
127 end
128
129 def same_domain?(cng, field_one \\ :actor, field_two \\ :object) do
130 actor_uri =
131 cng
132 |> get_field(field_one)
133 |> URI.parse()
134
135 object_uri =
136 cng
137 |> get_field(field_two)
138 |> URI.parse()
139
140 object_uri.host == actor_uri.host
141 end
142
143 # This figures out if a user is able to create, delete or modify something
144 # based on the domain and superuser status
145 def validate_modification_rights(cng) do
146 actor = User.get_cached_by_ap_id(get_field(cng, :actor))
147
148 if User.superuser?(actor) || same_domain?(cng) do
149 cng
150 else
151 cng
152 |> add_error(:actor, "is not allowed to modify object")
153 end
154 end
155 end