add license boilerplate to pleroma core
[akkoma] / lib / pleroma / user.ex
index 41d01cbf5cf1dc87af9efcb8d057db3d4f2017f5..1f930479d7aa718b948e8f5eed931b6d7db1cc3f 100644 (file)
@@ -1,3 +1,7 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
 defmodule Pleroma.User do
   use Ecto.Schema
 
@@ -38,7 +42,12 @@ defmodule Pleroma.User do
     timestamps()
   end
 
-  def auth_active?(user), do: user.info && !user.info.confirmation_pending
+  def auth_active?(%User{} = user) do
+    (user.info && !user.info.confirmation_pending) ||
+      !Pleroma.Config.get([:instance, :account_activation_required])
+  end
+
+  def superuser?(%User{} = user), do: user.info && User.Info.superuser?(user.info)
 
   def avatar_url(user) do
     case user.avatar do
@@ -74,15 +83,14 @@ defmodule Pleroma.User do
 
   def user_info(%User{} = user) do
     oneself = if user.local, do: 1, else: 0
-    user_info = user.info
 
     %{
       following_count: length(user.following) - oneself,
-      note_count: user_info.note_count,
-      follower_count: user_info.follower_count,
-      locked: user_info.locked,
-      confirmation_pending: user_info.confirmation_pending,
-      default_scope: user_info.default_scope
+      note_count: user.info.note_count,
+      follower_count: user.info.follower_count,
+      locked: user.info.locked,
+      confirmation_pending: user.info.confirmation_pending,
+      default_scope: user.info.default_scope
     }
   end
 
@@ -180,6 +188,8 @@ defmodule Pleroma.User do
         :unconfirmed
       end
 
+    info_change = User.Info.confirmation_changeset(%User.Info{}, confirmation_status)
+
     changeset =
       struct
       |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
@@ -191,7 +201,7 @@ defmodule Pleroma.User do
       |> validate_format(:email, @email_regex)
       |> validate_length(:bio, max: 1000)
       |> validate_length(:name, min: 1, max: 100)
-      |> put_change(:info, User.Info.confirmation_change(%User.Info{}, confirmation_status))
+      |> put_change(:info, info_change)
 
     if changeset.valid? do
       hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
@@ -210,14 +220,15 @@ defmodule Pleroma.User do
 
   @doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)"
   def register(%Ecto.Changeset{} = changeset) do
-    with {:ok, user} <- Repo.insert(changeset) do
-      {:ok, _} = try_send_confirmation_email(user)
+    with {:ok, user} <- Repo.insert(changeset),
+         {:ok, _} = try_send_confirmation_email(user) do
       {:ok, user}
     end
   end
 
   def try_send_confirmation_email(%User{} = user) do
-    if user.info.confirmation_pending do
+    if user.info.confirmation_pending &&
+         Pleroma.Config.get([:instance, :account_activation_required]) do
       user
       |> Pleroma.UserEmail.account_confirmation_email()
       |> Pleroma.Mailer.deliver()
@@ -393,10 +404,6 @@ defmodule Pleroma.User do
     end
   end
 
-  def get_by_confirmation_token(token) do
-    Repo.one(from(u in User, where: fragment("? ->> 'confirmation_token' = ?", u.info, ^token)))
-  end
-
   def get_followers_query(%User{id: id, follower_address: follower_address}) do
     from(
       u in User,