aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRoger Braun <roger@rogerbraun.net>2017-04-15 16:40:09 +0200
committerRoger Braun <roger@rogerbraun.net>2017-04-15 16:40:09 +0200
commit03c6148bb3dfe6efe512363d2793eb233e020d50 (patch)
treec45706122ae98a0b672aba967b0c41b7d368871f /lib
parent4f7adb343c66dba0a3c6883cd9f4156fb43594d4 (diff)
downloadpleroma-03c6148bb3dfe6efe512363d2793eb233e020d50.tar.gz
Add user registration changeset.
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/user.ex23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index 0a443d22a..ed85447fe 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -9,6 +9,8 @@ defmodule Pleroma.User do
field :name, :string
field :nickname, :string
field :password_hash, :string
+ field :password, :string, virtual: true
+ field :password_confirmation, :string, virtual: true
field :following, { :array, :string }, default: []
field :ap_id, :string
@@ -29,6 +31,27 @@ defmodule Pleroma.User do
|> validate_required([:following])
end
+ def register_changeset(struct, params \\ %{}) do
+ changeset = struct
+ |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
+ |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
+ |> validate_confirmation(:password)
+ |> unique_constraint(:email)
+ |> unique_constraint(:nickname)
+
+ if changeset.valid? do
+ hashed = Comeonin.Pbkdf2.hashpwsalt(changeset.changes[:password])
+ ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
+ followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
+ changeset
+ |> put_change(:password_hash, hashed)
+ |> put_change(:ap_id, ap_id)
+ |> put_change(:following, [followers])
+ else
+ changeset
+ end
+ end
+
def follow(%User{} = follower, %User{} = followed) do
ap_followers = User.ap_followers(followed)
following = [ap_followers | follower.following]