2020-10-29 15:26:45 +13:00
|
|
|
defmodule AshPostgres.MultitenancyTest.Org do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
2024-03-28 09:52:28 +13:00
|
|
|
domain: AshPostgres.MultitenancyTest.Domain,
|
2024-07-02 13:34:23 +12:00
|
|
|
data_layer: AshPostgres.DataLayer,
|
|
|
|
authorizers: [Ash.Policy.Authorizer]
|
|
|
|
|
|
|
|
policies do
|
|
|
|
policy action(:has_policies) do
|
|
|
|
authorize_if(relates_to_actor_via(:owner))
|
|
|
|
end
|
|
|
|
|
|
|
|
# policy always() do
|
|
|
|
# authorize_if(always())
|
|
|
|
# end
|
|
|
|
end
|
2020-10-29 15:26:45 +13:00
|
|
|
|
2023-07-13 02:56:15 +12:00
|
|
|
identities do
|
|
|
|
identity(:unique_by_name, [:name])
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 14:27:39 +13:00
|
|
|
uuid_primary_key(:id, writable?: true)
|
2024-03-28 09:52:28 +13:00
|
|
|
attribute(:name, :string, public?: true)
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:52:28 +13:00
|
|
|
default_accept(:*)
|
|
|
|
|
2022-04-20 03:08:28 +12:00
|
|
|
defaults([:create, :read, :update, :destroy])
|
2024-07-02 13:34:23 +12:00
|
|
|
|
|
|
|
read(:has_policies)
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
postgres do
|
|
|
|
table "multitenant_orgs"
|
2022-08-19 06:56:36 +12:00
|
|
|
repo(AshPostgres.TestRepo)
|
2020-10-29 15:26:45 +13:00
|
|
|
|
|
|
|
manage_tenant do
|
2022-08-19 06:56:36 +12:00
|
|
|
template(["org_", :id])
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
multitenancy do
|
2021-01-13 14:22:28 +13:00
|
|
|
strategy(:attribute)
|
|
|
|
attribute(:id)
|
|
|
|
global?(true)
|
|
|
|
parse_attribute({__MODULE__, :tenant, []})
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-07-02 13:34:23 +12:00
|
|
|
belongs_to :owner, AshPostgres.MultitenancyTest.User do
|
|
|
|
attribute_public?(false)
|
|
|
|
public?(false)
|
|
|
|
attribute_type(:string)
|
|
|
|
end
|
|
|
|
|
2024-03-28 09:52:28 +13:00
|
|
|
has_many(:posts, AshPostgres.MultitenancyTest.Post,
|
|
|
|
destination_attribute: :org_id,
|
|
|
|
public?: true
|
|
|
|
)
|
|
|
|
|
|
|
|
has_many(:users, AshPostgres.MultitenancyTest.User,
|
|
|
|
destination_attribute: :org_id,
|
|
|
|
public?: true
|
|
|
|
)
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
def tenant("org_" <> tenant) do
|
|
|
|
tenant
|
|
|
|
end
|
|
|
|
end
|