Skip to content

Commit 3d4a30a

Browse files
committed
Expand through resources to have policies
1 parent 7269191 commit 3d4a30a

File tree

6 files changed

+90
-5
lines changed

6 files changed

+90
-5
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule AshPostgres.TestRepo.Migrations.AddPublicToClassrooms do
2+
@moduledoc """
3+
Adds public column to classrooms for through relationship policy testing.
4+
"""
5+
6+
use Ecto.Migration
7+
8+
def up do
9+
alter table(:classrooms) do
10+
add(:public, :boolean, default: true)
11+
end
12+
end
13+
14+
def down do
15+
alter table(:classrooms) do
16+
remove(:public)
17+
end
18+
end
19+
end

test/support/resources/through/classroom.ex

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,30 @@ defmodule AshPostgres.Test.Through.Classroom do
66
@moduledoc false
77
use Ash.Resource,
88
domain: AshPostgres.Test.Domain,
9-
data_layer: AshPostgres.DataLayer
9+
data_layer: AshPostgres.DataLayer,
10+
authorizers: [Ash.Policy.Authorizer]
1011

1112
postgres do
1213
table "classrooms"
1314
repo AshPostgres.TestRepo
1415
end
1516

17+
policies do
18+
policy action_type(:read) do
19+
authorize_if(expr(public == true))
20+
end
21+
end
22+
23+
field_policies do
24+
field_policy :* do
25+
authorize_if(always())
26+
end
27+
end
28+
1629
attributes do
1730
uuid_primary_key(:id)
1831
attribute(:name, :string, public?: true)
32+
attribute(:public, :boolean, public?: true, default: true)
1933
end
2034

2135
actions do

test/support/resources/through/classroom_teacher.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@ defmodule AshPostgres.Test.Through.ClassroomTeacher do
66
@moduledoc false
77
use Ash.Resource,
88
domain: AshPostgres.Test.Domain,
9-
data_layer: AshPostgres.DataLayer
9+
data_layer: AshPostgres.DataLayer,
10+
authorizers: [Ash.Policy.Authorizer]
1011

1112
require Ash.Query
1213

14+
policies do
15+
policy action_type(:read) do
16+
authorize_if(expr(is_nil(retired_at)))
17+
end
18+
end
19+
20+
field_policies do
21+
field_policy :* do
22+
authorize_if(always())
23+
end
24+
end
25+
1326
postgres do
1427
table "classroom_teachers"
1528
repo AshPostgres.TestRepo

test/support/resources/through/school.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@ defmodule AshPostgres.Test.Through.School do
66
@moduledoc false
77
use Ash.Resource,
88
domain: AshPostgres.Test.Domain,
9-
data_layer: AshPostgres.DataLayer
9+
data_layer: AshPostgres.DataLayer,
10+
authorizers: [Ash.Policy.Authorizer]
1011

1112
postgres do
1213
table "schools"
1314
repo AshPostgres.TestRepo
1415
end
1516

17+
policies do
18+
policy action_type(:read) do
19+
authorize_if(always())
20+
end
21+
end
22+
23+
field_policies do
24+
field_policy :* do
25+
authorize_if(always())
26+
end
27+
end
28+
1629
attributes do
1730
uuid_primary_key(:id)
1831
attribute(:name, :string, public?: true)

test/support/resources/through/student.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@ defmodule AshPostgres.Test.Through.Student do
66
@moduledoc false
77
use Ash.Resource,
88
domain: AshPostgres.Test.Domain,
9-
data_layer: AshPostgres.DataLayer
9+
data_layer: AshPostgres.DataLayer,
10+
authorizers: [Ash.Policy.Authorizer]
11+
12+
policies do
13+
policy action_type(:read) do
14+
authorize_if(always())
15+
end
16+
end
17+
18+
field_policies do
19+
field_policy :* do
20+
authorize_if(always())
21+
end
22+
end
1023

1124
postgres do
1225
table "students"

test/support/resources/through/teacher.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@ defmodule AshPostgres.Test.Through.Teacher do
66
@moduledoc false
77
use Ash.Resource,
88
domain: AshPostgres.Test.Domain,
9-
data_layer: AshPostgres.DataLayer
9+
data_layer: AshPostgres.DataLayer,
10+
authorizers: [Ash.Policy.Authorizer]
11+
12+
policies do
13+
policy action_type(:read) do
14+
authorize_if(always())
15+
end
16+
end
17+
18+
field_policies do
19+
field_policy :* do
20+
authorize_if(always())
21+
end
22+
end
1023

1124
postgres do
1225
table "teachers"

0 commit comments

Comments
 (0)