Change primary keys for accounts and devices table
This commit is contained in:
parent
dfc3fa450d
commit
91a06aff1b
9 changed files with 81 additions and 77 deletions
|
@ -13,14 +13,14 @@ defmodule MatrixServer.Account do
|
|||
|
||||
@max_mxid_length 255
|
||||
|
||||
@primary_key {:localpart, :string, []}
|
||||
schema "accounts" do
|
||||
field :localpart, :string
|
||||
field :password_hash, :string, redact: true
|
||||
has_many :devices, Device, foreign_key: :localpart
|
||||
has_many :devices, Device
|
||||
|
||||
many_to_many :joined_rooms, Room,
|
||||
join_through: JoinedRoom,
|
||||
join_keys: [localpart: :localpart, room_id: :id]
|
||||
join_keys: [account_id: :id, room_id: :id]
|
||||
|
||||
timestamps(updated_at: false)
|
||||
end
|
||||
|
@ -50,31 +50,35 @@ defmodule MatrixServer.Account do
|
|||
Return an multi to register a new user.
|
||||
"""
|
||||
@spec register(Register.t()) :: Multi.t()
|
||||
def register(%Register{} = input) do
|
||||
def register(input) do
|
||||
localpart = input.username || MatrixServer.random_string(10, ?a..?z)
|
||||
|
||||
account_params = %{
|
||||
localpart: input.username || MatrixServer.random_string(10, ?a..?z),
|
||||
localpart: localpart,
|
||||
password_hash: Bcrypt.hash_pwd_salt(input.password)
|
||||
}
|
||||
|
||||
Multi.new()
|
||||
|> Multi.insert(:account, changeset(%Account{}, account_params))
|
||||
|> Multi.insert(:device, fn %{account: account} ->
|
||||
device_id = input.device_id || Device.generate_device_id(account.localpart)
|
||||
access_token = Device.generate_access_token(localpart, device_id)
|
||||
|
||||
device_params = %{
|
||||
display_name: input.initial_device_display_name,
|
||||
device_id: input.device_id || Device.generate_device_id(account.localpart)
|
||||
device_id: device_id
|
||||
}
|
||||
|
||||
Ecto.build_assoc(account, :devices)
|
||||
Ecto.build_assoc(account, :devices, access_token: access_token)
|
||||
|> Device.changeset(device_params)
|
||||
end)
|
||||
|> Multi.run(:device_with_access_token, &Device.insert_new_access_token/2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Return a function to log a user in.
|
||||
"""
|
||||
@spec login(Login.t()) :: (Ecto.Repo.t() -> {:error, any()} | {:ok, Device.t()})
|
||||
def login(%Login{} = input) do
|
||||
@spec login(Login.t()) :: (Ecto.Repo.t() -> {:error, any()} | {:ok, {Account.t(), Device.t()}})
|
||||
def login(input) do
|
||||
localpart = try_get_localpart(input.identifier.user)
|
||||
|
||||
fn repo ->
|
||||
|
@ -83,7 +87,7 @@ defmodule MatrixServer.Account do
|
|||
if Bcrypt.verify_pass(input.password, hash) do
|
||||
case Device.login(input, account) do
|
||||
{:ok, device} ->
|
||||
device
|
||||
{account, device}
|
||||
|
||||
{:error, _cs} ->
|
||||
repo.rollback(:forbidden)
|
||||
|
@ -119,7 +123,7 @@ defmodule MatrixServer.Account do
|
|||
|> validate_length(:password_hash, max: 60)
|
||||
|> validate_format(:localpart, MatrixServer.localpart_regex())
|
||||
|> validate_length(:localpart, max: localpart_length())
|
||||
|> unique_constraint(:localpart, name: :accounts_pkey)
|
||||
|> unique_constraint(:localpart, name: :accounts_localpart_index)
|
||||
end
|
||||
|
||||
@spec localpart_length :: integer()
|
||||
|
|
|
@ -4,43 +4,27 @@ defmodule MatrixServer.Device do
|
|||
import Ecto.{Changeset, Query}
|
||||
|
||||
alias MatrixServer.{Account, Device, Repo}
|
||||
alias MatrixServerWeb.Client.Request.Login
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
device_id: String.t(),
|
||||
access_token: String.t(),
|
||||
display_name: String.t(),
|
||||
localpart: String.t()
|
||||
account_id: integer()
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
schema "devices" do
|
||||
field :device_id, :string, primary_key: true
|
||||
field :device_id, :string
|
||||
field :access_token, :string, redact: true
|
||||
field :display_name, :string
|
||||
|
||||
belongs_to :account, Account,
|
||||
foreign_key: :localpart,
|
||||
references: :localpart,
|
||||
type: :string,
|
||||
primary_key: true
|
||||
belongs_to :account, Account
|
||||
end
|
||||
|
||||
def changeset(device, params \\ %{}) do
|
||||
device
|
||||
|> cast(params, [:display_name, :device_id])
|
||||
|> validate_required([:localpart, :device_id])
|
||||
|> unique_constraint([:localpart, :device_id], name: :devices_pkey)
|
||||
end
|
||||
|
||||
def insert_new_access_token(repo, %{
|
||||
device: %Device{localpart: localpart, device_id: device_id} = device
|
||||
}) do
|
||||
access_token = generate_access_token(localpart, device_id)
|
||||
|
||||
device
|
||||
|> change(%{access_token: access_token})
|
||||
|> repo.update()
|
||||
|> validate_required([:device_id])
|
||||
|> unique_constraint([:device_id, :account_id], name: :devices_device_id_account_id_index)
|
||||
end
|
||||
|
||||
def generate_access_token(localpart, device_id) do
|
||||
|
@ -52,9 +36,9 @@ defmodule MatrixServer.Device do
|
|||
"#{localpart}_#{System.os_time(:millisecond)}"
|
||||
end
|
||||
|
||||
def login(%Login{} = input, account) do
|
||||
device_id = input.device_id || generate_device_id(account.localpart)
|
||||
access_token = generate_access_token(account.localpart, device_id)
|
||||
def login(input, %Account{localpart: localpart} = account) do
|
||||
device_id = input.device_id || generate_device_id(localpart)
|
||||
access_token = generate_access_token(localpart, device_id)
|
||||
|
||||
update_query =
|
||||
from(d in Device)
|
||||
|
@ -75,6 +59,6 @@ defmodule MatrixServer.Device do
|
|||
Ecto.build_assoc(account, :devices)
|
||||
|> Device.changeset(device_params)
|
||||
|> put_change(:access_token, access_token)
|
||||
|> Repo.insert(on_conflict: update_query, conflict_target: [:localpart, :device_id])
|
||||
|> Repo.insert(on_conflict: update_query, conflict_target: [:account_id, :device_id])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,17 +4,13 @@ defmodule MatrixServer.JoinedRoom do
|
|||
alias MatrixServer.{Account, Room}
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
localpart: String.t(),
|
||||
account_id: integer(),
|
||||
room_id: String.t()
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
schema "joined_rooms" do
|
||||
belongs_to :account, Account,
|
||||
foreign_key: :localpart,
|
||||
references: :localpart,
|
||||
type: :string,
|
||||
primary_key: true
|
||||
belongs_to :account, Account, primary_key: true
|
||||
|
||||
belongs_to :room, Room, primary_key: true, type: :string
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue