2019-10-01 02:23:32 +00:00
|
|
|
defmodule Mix.Tasks.Clacks.User do
|
|
|
|
use Mix.Task
|
|
|
|
alias Clacks.{Repo, User, Actor, Keys, ActivityPub}
|
|
|
|
|
|
|
|
@shortdoc "Creates a new user"
|
|
|
|
def run(["create"]) do
|
|
|
|
username = IO.gets("Username: ") |> String.trim()
|
2019-10-02 21:04:56 +00:00
|
|
|
password = IO.gets("Password: ") |> String.trim()
|
2019-10-01 02:23:32 +00:00
|
|
|
|
|
|
|
{:ok, pem} = Keys.generate_rsa_pem()
|
2019-10-01 22:39:17 +00:00
|
|
|
{:ok, _private, public} = Keys.keys_from_private_key_pem(pem)
|
2019-10-01 02:23:32 +00:00
|
|
|
{:ok, public_key_pem} = Keys.public_key_pem(public)
|
|
|
|
|
2019-10-02 21:04:56 +00:00
|
|
|
changeset =
|
2020-01-16 02:13:02 +00:00
|
|
|
User.registration_changeset(%User{}, %{
|
|
|
|
username: username,
|
|
|
|
private_key: pem,
|
|
|
|
password: password
|
|
|
|
})
|
2019-10-01 02:23:32 +00:00
|
|
|
|
|
|
|
# start the app so the DB connection is established
|
|
|
|
Mix.Task.run("app.start")
|
|
|
|
|
|
|
|
{:ok, user} = Repo.insert(changeset)
|
|
|
|
|
|
|
|
url = Application.get_env(:clacks, ClacksWeb.Endpoint)[:url]
|
|
|
|
|
|
|
|
uri =
|
|
|
|
URI.to_string(%URI{
|
|
|
|
scheme: url[:scheme],
|
|
|
|
host: url[:host],
|
|
|
|
port: url[:port],
|
|
|
|
path: Path.join("/users", username)
|
|
|
|
})
|
|
|
|
|
|
|
|
actor =
|
|
|
|
Ecto.build_assoc(user, :actor, %{
|
|
|
|
ap_id: uri,
|
|
|
|
nickname: username,
|
|
|
|
local: true,
|
|
|
|
data: ActivityPub.actor(uri, username, username, public_key_pem)
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, actor} = Repo.insert(actor)
|
|
|
|
|
|
|
|
IO.puts("User #{username} successfully created")
|
|
|
|
end
|
2020-01-16 02:13:02 +00:00
|
|
|
|
|
|
|
@shortdoc "Changes the given user's password"
|
|
|
|
def run(["change_password", username]) do
|
|
|
|
case User.get_by_username(username) do
|
|
|
|
nil ->
|
|
|
|
IO.puts("No user found with username '#{username}'")
|
|
|
|
|
|
|
|
user ->
|
|
|
|
password = IO.gets("Password: ") |> String.trim()
|
|
|
|
|
|
|
|
changeset = User.change_password_changeset(user, %{password: password})
|
|
|
|
|
|
|
|
{:ok, user} = Repo.update(changeset)
|
|
|
|
|
|
|
|
IO.puts("Password updated")
|
|
|
|
end
|
|
|
|
end
|
2019-10-01 02:23:32 +00:00
|
|
|
end
|