Hash :
c3be0b9a
Author :
Thomas de Grivel
Date :
2022-01-18T11:18:15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
defmodule Kmxgit.UserManager do
@moduledoc """
The UserManager context.
"""
import Ecto.Query, warn: false
alias Kmxgit.Repo
alias Kmxgit.SlugManager.Slug
alias Kmxgit.UserManager.{User, UserToken, UserNotifier}
def list_users do
Repo.all from user in User, preload: :slug
end
def count_users do
Repo.one from user in User, select: count()
end
def get_user(id) do
Repo.one from user in User,
where: [id: ^id],
preload: [organisations: :slug],
preload: [owned_repositories: [organisation: :slug, user: :slug]],
preload: :slug
end
def get_user!(id) do
get_user(id) || raise Ecto.NoResultsError
end
def get_user_by_login(login) do
Repo.one from u in User,
join: s in Slug,
on: s.user_id == u.id,
where: fragment("lower(?)", s.slug) == ^String.downcase(login),
limit: 1,
preload: [:slug,
organisations: :slug,
owned_repositories: [organisation: :slug,
user: :slug]]
end
def get_user_by_login_and_password(login, password)
when is_binary(login) and is_binary(password) do
user = get_user_by_login(login)
if User.valid_password?(user, password), do: user
end
def get_user_by_email(email) do
Repo.one from u in User,
where: u.email == ^email,
limit: 1,
preload: [:slug,
organisations: :slug,
owned_repositories: [organisation: :slug,
user: :slug]]
end
def register_user(attrs) do
%User{}
|> User.registration_changeset(attrs)
|> Repo.insert()
end
def change_user_registration(%User{} = user, attrs \\ %{}) do
User.registration_changeset(user, attrs, hash_password: false)
end
def change_user_email(user, attrs \\ %{}) do
User.email_changeset(user, attrs)
end
def apply_user_email(user, password, attrs) do
user
|> User.email_changeset(attrs)
|> User.validate_current_password(password)
|> Ecto.Changeset.apply_action(:update)
end
def update_user_email(user, token) do
old = user.email
context = "change:#{user.email}"
with {:ok, query} <- UserToken.verify_change_email_token_query(token, context),
%UserToken{sent_to: email} <- Repo.one(query),
{:ok, _} <- Repo.transaction(user_email_multi(user, email, context)),
_ <- UserNotifier.deliver_email_changed_email(old, email) do
:ok
else
_ -> :error
end
end
defp user_email_multi(user, email, context) do
changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset()
Ecto.Multi.new()
|> Ecto.Multi.update(:user, changeset)
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, [context]))
end
def deliver_update_email_instructions(%User{} = user, current_email, update_email_url_fun)
when is_function(update_email_url_fun, 1) do
{encoded_token, user_token} = UserToken.build_email_token(user, "change:#{current_email}")
Repo.insert!(user_token)
UserNotifier.deliver_update_email_instructions(user, update_email_url_fun.(encoded_token))
end
def change_user_password(user, attrs \\ %{}) do
User.password_changeset(user, attrs, hash_password: false)
end
def update_user_password(user, password, attrs) do
changeset =
user
|> User.password_changeset(attrs)
|> User.validate_current_password(password)
Ecto.Multi.new()
|> Ecto.Multi.update(:user, changeset)
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, :all))
|> Repo.transaction()
|> case do
{:ok, %{user: user}} ->
UserNotifier.deliver_password_changed_email(user)
{:ok, user}
{:error, :user, changeset, _} -> {:error, changeset}
end
end
def generate_user_session_token(user) do
{token, user_token} = UserToken.build_session_token(user)
Repo.insert!(user_token)
token
end
def get_user_by_session_token(token) do
{:ok, query} = UserToken.verify_session_token_query(token)
user = Repo.one(query)
if user do
get_user(user.id)
end
end
def delete_session_token(token) do
Repo.delete_all(UserToken.token_and_context_query(token, "session"))
:ok
end
def deliver_user_confirmation_instructions(%User{} = user, confirmation_url_fun)
when is_function(confirmation_url_fun, 1) do
if user.confirmed_at do
{:error, :already_confirmed}
else
{encoded_token, user_token} = UserToken.build_email_token(user, "confirm")
Repo.insert!(user_token)
UserNotifier.deliver_confirmation_instructions(user, confirmation_url_fun.(encoded_token))
end
end
def confirm_user(token) do
with {:ok, query} <- UserToken.verify_email_token_query(token, "confirm"),
%User{} = user <- Repo.one(query),
{:ok, %{user: user}} <- Repo.transaction(confirm_user_multi(user)) do
{:ok, user}
else
_ -> :error
end
end
defp confirm_user_multi(user) do
Ecto.Multi.new()
|> Ecto.Multi.update(:user, User.confirm_changeset(user))
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, ["confirm"]))
end
def deliver_user_reset_password_instructions(%User{} = user, reset_password_url_fun)
when is_function(reset_password_url_fun, 1) do
{encoded_token, user_token} = UserToken.build_email_token(user, "reset_password")
Repo.insert!(user_token)
UserNotifier.deliver_reset_password_instructions(user, reset_password_url_fun.(encoded_token))
end
def get_user_by_reset_password_token(token) do
with {:ok, query} <- UserToken.verify_email_token_query(token, "reset_password"),
%User{} = user <- Repo.one(query) do
user
else
_ -> nil
end
end
def reset_user_password(user, attrs) do
Ecto.Multi.new()
|> Ecto.Multi.update(:user, User.password_changeset(user, attrs))
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, :all))
|> Repo.transaction()
|> case do
{:ok, %{user: user}} -> {:ok, user}
{:error, :user, changeset, _} -> {:error, changeset}
end
end
def admin_create_user(attrs \\ %{}) do
%User{}
|> User.admin_changeset(attrs)
|> Repo.insert()
end
def update_user(%User{} = user, attrs) do
old_login = user.slug.slug
case user
|> User.changeset(attrs)
|> Repo.update() do
{:ok, u} ->
if u.slug.slug != old_login do
UserNotifier.deliver_login_changed_email(u, old_login, u.slug.slug)
end
{:ok, u}
x -> x
end
end
def admin_update_user(%User{} = user, attrs) do
user
|> User.admin_changeset(attrs)
|> Repo.update()
end
def admin_update_user_password(%User{} = user, attrs) do
user
|> User.password_changeset(attrs)
|> Repo.update()
end
def delete_user(%User{} = user) do
user
|> change_user()
|> Repo.delete()
end
def change_user(%User{} = user \\ %User{}, params \\ %{}) do
User.changeset(user, params)
end
def authenticate_user(login, password) do
query = from u in User,
join: s in Slug,
on: s.user_id == u.id,
where: fragment("lower(?)", s.slug) == ^String.downcase(login) or fragment("lower(?)", u.email) == ^String.downcase(login),
limit: 1
case Repo.one(query) do
nil ->
Bcrypt.no_user_verify()
{:error, :invalid_credentials}
user ->
if Bcrypt.verify_pass(password, user.encrypted_password) do
{:ok, user}
else
{:error, :invalid_credentials}
end
end
end
def totp_init do
Repo.transaction fn ->
Enum.each list_users(), fn u ->
{:ok, _} = User.totp_changeset(u) |> Repo.update()
end
end
end
@doc """
Returns a URL that be rendered with a QR code.
It meets the Google Authenticator specification
at https://github.com/google/google-authenticator/wiki/Key-Uri-Format.
## Examples
iex> generate_totp_enrolment_url(user)
"""
def totp_enrolment_url(%User{email: email, totp_secret: secret}) do
"otpauth://totp/kmxgit:#{email}?secret=#{secret}&issuer=kmxgit&algorithm=SHA1&digits=6&period=30"
end
def update_user_totp(user = %User{}, params) do
case user
|> User.totp_changeset(params)
|> Repo.update()
do
{:ok, user1} ->
if (user.totp_last == 0 && user1.totp_last != 0), do: UserNotifier.deliver_totp_enabled_email(user)
{:ok, user1}
x -> x
end
end
def verify_user_totp(user = %User{}, token) do
User.totp_verify(user, token || 0)
end
def delete_user_totp(user = %User{}) do
case user
|> User.totp_changeset(:delete)
|> Repo.update()
do
{:ok, user1} ->
if (user.totp_last != 0 && user1.totp_last == 0), do: UserNotifier.deliver_totp_disabled_email(user)
{:ok, user1}
x -> x
end
end
def admin_user_present? do
if Repo.one(from user in User,
where: [is_admin: true],
limit: 1) do
true
else
false
end
end
end