defmodule Sukaato.User do use Ecto.Schema import Ecto.Changeset # import Sukaato.Vschemas import Sukaato.CustomValidators schema "users" do field :name, :string field :password, :string field :username, :string field :email, :string field :dob, :date field :gender_type, Ecto.Enum, values: [:trans, :cis, :nb] field :gender_id, Ecto.Enum, values: [:fem, :masc, :combined, :fluid, :none] field :bio, :string field :affil, {:array, :map} field :perms, {:array, :integer} field :user_token, :string field :pub_keys, :map field :totp_enabled, :boolean, default: false field :totp_active, :boolean, default: true field :totp_secret, :string field :ltotp, :utc_datetime field :fido_enabled, :boolean, default: false field :fido_active, :boolean, default: false field :fido_priority, :integer field :fido_creds, {:array, :string} field :fido_keys, {:array, :map} timestamps(type: :utc_datetime) end @doc false def changeset(user, attrs) do user |> cast(attrs, [ :name, :username, :password, :email, :dob, :gender_type, :gender_id, :bio, :affil, :perms, :user_token, :pub_keys, :totp_enabled, :totp_active, :totp_secret, :ltotp, :fido_enabled, :fido_active, :fido_priority, :fido_creds, :fido_keys ]) |> validate_required([:username, :password, :email, :perms]) |> validate_length(:perms, is: 6) |> validate_format(:email, ~r/@/) |> validate_map_format(:perms, Sukaato.Vschemas.perms) |> validate_map_format(:affil, Sukaato.Vschemas.affil) |> validate_map_format(:pub_keys, Sukaato.Vschemas.pub_keys) |> encrypt_password(:password) |> unique_constraint(:username) end end