feat(ash): added accounts, user for authentication

This commit is contained in:
carla 2025-05-30 12:20:47 +02:00 committed by carla
parent 80e7041146
commit f154eea055
5 changed files with 160 additions and 1 deletions

View file

@ -49,7 +49,7 @@ config :spark,
config :mv, config :mv,
ecto_repos: [Mv.Repo], ecto_repos: [Mv.Repo],
generators: [timestamp_type: :utc_datetime], generators: [timestamp_type: :utc_datetime],
ash_domains: [Mv.Membership] ash_domains: [Mv.Membership, Mv.Accounts]
# Configures the endpoint # Configures the endpoint
config :mv, MvWeb.Endpoint, config :mv, MvWeb.Endpoint,

13
lib/accounts/accounts.ex Normal file
View file

@ -0,0 +1,13 @@
defmodule Mv.Accounts do
use Ash.Domain,
extensions: [AshPhoenix]
resources do
resource Mv.Accounts.User do
define(:create_user, action: :create)
define(:list_users, action: :read)
define(:update_user, action: :update)
define(:destroy_user, action: :destroy)
end
end
end

26
lib/accounts/user.ex Normal file
View file

@ -0,0 +1,26 @@
defmodule Mv.Accounts.User do
use Ash.Resource,
domain: Mv.Accounts,
data_layer: AshPostgres.DataLayer
postgres do
table("users")
repo(Mv.Repo)
end
attributes do
uuid_primary_key(:id)
attribute(:email, :string, allow_nil?: true, public?: true)
attribute(:password_hash, :string, sensitive?: true)
attribute(:oicd_id, :string)
end
actions do
defaults([:read, :destroy, :create, :update])
end
relationships do
belongs_to(:member, Mv.Membership.Member)
end
end

View file

@ -0,0 +1,32 @@
defmodule Mv.Repo.Migrations.AccountMigration do
@moduledoc """
Updates resources based on their most recent snapshots.
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""
use Ecto.Migration
def up do
create table(:users, primary_key: false) do
add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
add :email, :text
add :password_hash, :text
add :oicd_id, :text
add :member_id,
references(:members,
column: :id,
name: "users_member_id_fkey",
type: :uuid,
prefix: "public"
)
end
end
def down do
drop constraint(:users, "users_member_id_fkey")
drop table(:users)
end
end

View file

@ -0,0 +1,88 @@
{
"attributes": [
{
"allow_nil?": false,
"default": "fragment(\"gen_random_uuid()\")",
"generated?": false,
"primary_key?": true,
"references": null,
"size": null,
"source": "id",
"type": "uuid"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "email",
"type": "text"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "password_hash",
"type": "text"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "oicd_id",
"type": "text"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": {
"deferrable": false,
"destination_attribute": "id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"index?": false,
"match_type": null,
"match_with": null,
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"name": "users_member_id_fkey",
"on_delete": null,
"on_update": null,
"primary_key?": true,
"schema": "public",
"table": "members"
},
"size": null,
"source": "member_id",
"type": "uuid"
}
],
"base_filter": null,
"check_constraints": [],
"custom_indexes": [],
"custom_statements": [],
"has_create_action": true,
"hash": "1CA33A4C9EBDC29717F4B6ADB27E76B42B5BEA7085E47BFBDD9D84E592D44649",
"identities": [],
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"repo": "Elixir.Mv.Repo",
"schema": null,
"table": "users"
}