This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
/
filter_check.ex
134 lines (110 loc) · 4.42 KB
/
filter_check.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
defmodule AshPolicyAuthorizer.FilterCheck do
@moduledoc """
A type of check that is represented by a filter statement
That filter statement can be templated, currently only supporting `{:_actor, field}`
which will replace that portion of the filter with the appropriate field value from the actor and
`{:_actor, :_primary_key}` which will replace the value with a keyword list of the primary key
fields of an actor to their values, like `[id: 1]`. If the actor is not present `{:_actor, field}`
becomes `nil`, and `{:_actor, :_primary_key}` becomes `false`.
You can customize what the "negative" filter looks like by defining `c:reject/1`. This is important for
filters over related data. For example, given an `owner` relationship and a data layer like `ash_postgres`
where `column != NULL` does *not* evaluate to true (see postgres docs on NULL for more):
# The opposite of
`owner.id == 1`
# in most cases is not
`not(owner.id == 1)`
# because in postgres that would be `NOT (owner.id = NULL)` in cases where there was no owner
# A better opposite would be
`owner.id != 1 or is_nil(owner.id)`
# alternatively
`not(owner.id == 1) or is_nil(owner.id)`
By being able to customize the `reject` filter, you can use related filters in your policies. Without it,
they will likely have undesired effects.
"""
@type options :: Keyword.t()
@callback filter(options()) :: Keyword.t()
@callback reject(options()) :: Keyword.t()
@optional_callbacks [filter: 1, reject: 1]
defmacro __using__(_) do
quote do
@behaviour AshPolicyAuthorizer.FilterCheck
@behaviour AshPolicyAuthorizer.Check
require Ash.Query
def type, do: :filter
def strict_check_context(opts) do
[:query, :changeset]
end
def strict_check(
actor,
%{query: %{filter: candidate}, resource: resource, api: api} = context,
opts
) do
opts = Keyword.put_new(opts, :resource, context.resource)
configured_filter = filter(opts)
if is_nil(actor) and
Ash.Filter.template_references_actor?(configured_filter) do
{:ok, false}
else
filter = Ash.Filter.build_filter_from_template(configured_filter, actor)
case Ash.Filter.parse(resource, filter) do
{:ok, parsed_filter} ->
if Ash.Filter.strict_subset_of?(parsed_filter, candidate) do
{:ok, true}
else
case Ash.Filter.parse(resource, not: filter) do
{:ok, negated_filter} ->
if Ash.Filter.strict_subset_of?(negated_filter, candidate) do
{:ok, false}
else
{:ok, :unknown}
end
{:error, error} ->
{:error, error}
end
end
{:error, error} ->
{:error, error}
end
end
end
def strict_check(_, _, _), do: {:ok, :unknown}
def auto_filter(actor, authorizer, opts) do
opts = Keyword.put_new(opts, :resource, authorizer.resource)
Ash.Filter.build_filter_from_template(filter(opts), actor)
end
def auto_filter_not(actor, authorizer, opts) do
opts = Keyword.put_new(opts, :resource, authorizer.resource)
Ash.Filter.build_filter_from_template(reject(opts), actor)
end
def reject(opts) do
[not: filter(opts)]
end
def check(actor, data, authorizer, opts) do
pkey = Ash.Resource.Info.primary_key(authorizer.resource)
filter =
case data do
[record] -> Map.take(record, pkey)
records -> [or: Enum.map(data, &Map.take(&1, pkey))]
end
authorizer.resource
|> authorizer.api.query()
|> Ash.Query.filter(^filter)
|> Ash.Query.filter(^auto_filter(authorizer.actor, authorizer, opts))
|> authorizer.api.read()
|> case do
{:ok, authorized_data} ->
authorized_pkeys = Enum.map(authorized_data, &Map.take(&1, pkey))
Enum.filter(data, fn record ->
Map.take(record, pkey) in authorized_pkeys
end)
{:error, error} ->
{:error, error}
end
end
defoverridable reject: 1
end
end
def is_filter_check?(module) do
:erlang.function_exported(module, :filter, 1)
end
end