Hash :
484783ab
Author :
Thomas de Grivel
Date :
2022-12-01T15:12:44
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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
## kmxgit
## Copyright 2022 kmx.io <contact@kmx.io>
##
## Permission is hereby granted to use this software granted
## the above copyright notice and this permission paragraph
## are included in all copies and substantial portions of this
## software.
##
## THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
## PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
## AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
## THIS SOFTWARE.
defmodule Kmxgit.UserManager do
@moduledoc """
The UserManager context.
"""
import Ecto.Query, warn: false
alias Kmxgit.IndexParams
alias Kmxgit.Pagination
alias Kmxgit.Repo
alias Kmxgit.UserManager.{Avatar, User, UserToken, UserNotifier}
@user_preload [:organisations,
owned_repositories: [:members,
:organisation,
:user]]
def list_all_users() do
from(u in User)
|> order_by([u], fragment("lower(?)", u.slug_))
|> preload([:owned_repositories, :slug])
|> Repo.all()
end
def list_users(params \\ %IndexParams{}) do
update_disk_usage()
from(u in User)
|> search(params)
|> index_order_by(params)
|> Pagination.page(params, preload: [:owned_repositories])
end
def search(query, %IndexParams{search: search}) do
expr = "%#{search}%"
query
|> where([u], ilike(u.name, ^expr) or ilike(u.slug_, ^expr) or ilike(u.email, ^expr))
end
def index_order_by(query, %IndexParams{column: "id", reverse: true}) do
order_by(query, [desc: :id])
end
def index_order_by(query, %IndexParams{column: "id"}) do
order_by(query, :id)
end
def index_order_by(query, %IndexParams{column: "name", reverse: true}) do
order_by(query, [u], [desc_nulls_last: fragment("lower(?)", u.name)])
end
def index_order_by(query, %IndexParams{column: "name"}) do
order_by(query, [u], [asc_nulls_last: fragment("lower(?)", u.name)])
end
def index_order_by(query, %IndexParams{column: "email", reverse: true}) do
order_by(query, [u], [desc_nulls_last: fragment("lower(?)", u.email)])
end
def index_order_by(query, %IndexParams{column: "email"}) do
order_by(query, [u], [asc_nulls_last: fragment("lower(?)", u.email)])
end
def index_order_by(query, %IndexParams{column: "login", reverse: true}) do
order_by(query, [u], [desc_nulls_last: fragment("lower(?)", u.slug_)])
end
def index_order_by(query, %IndexParams{column: "login"}) do
order_by(query, [u], [asc_nulls_last: fragment("lower(?)", u.slug_)])
end
def index_order_by(query, %IndexParams{column: "du", reverse: true}) do
order_by(query, [desc: :disk_usage])
end
def index_order_by(query, %IndexParams{column: "du"}) do
order_by(query, :disk_usage)
end
def index_order_by(query, %IndexParams{column: "mfa", reverse: true}) do
order_by(query, [u], [desc: u.totp_last == 0])
end
def index_order_by(query, %IndexParams{column: "mfa"}) do
order_by(query, [u], u.totp_last == 0)
end
def index_order_by(query, %IndexParams{column: "admin", reverse: true}) do
order_by(query, :is_admin)
end
def index_order_by(query, %IndexParams{column: "admin"}) do
order_by(query, [desc: :is_admin])
end
def index_order_by(query, %IndexParams{column: "deploy", reverse: true}) do
order_by(query, :deploy_only)
end
def index_order_by(query, %IndexParams{column: "deploy"}) do
order_by(query, [desc: :deploy_only])
end
def update_disk_usage() do
Repo.all(from user in User)
|> Enum.map(fn user ->
user
|> Ecto.Changeset.cast(%{}, [])
|> Ecto.Changeset.put_change(:disk_usage, User.disk_usage(user))
|> Repo.update!()
end)
end
def put_disk_usage(user = %User{}) do
%User{user | disk_usage: User.disk_usage(user)}
end
def put_disk_usage(users) when is_list(users) do
users
|> Enum.map(&put_disk_usage/1)
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: ^@user_preload
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,
where: u.slug_ == ^login,
limit: 1
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
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
changeset = User.admin_create_user_changeset(%User{}, attrs)
with {:ok, user} <- Repo.insert(changeset) do
{:ok, user, changeset}
end
end
def update_user(%User{} = user, attrs) do
old_login = User.login(user)
case user
|> User.changeset(attrs)
|> Repo.update() do
{:ok, u} ->
if User.login(u) != old_login do
UserNotifier.deliver_login_changed_email(u, old_login, User.login(u))
end
if attrs["avatar"] do
%{path: path} = attrs["avatar"]
Avatar.set_image(u, path)
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,
where: u.slug_ == ^login or u.email == ^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_all_users(), fn u ->
{:ok, _} = User.totp_changeset(u) |> Repo.update()
end
end
end
def totp_update(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 totp_verify(user = %User{}, token) do
User.totp_verify(user, token || 0)
end
def totp_delete(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
def update_slug_() do
for u <- list_all_users() do
Kmxgit.UserManager.update_user(u, %{slug_: u.slug.slug})
end
end
end