-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enforce password security un user schema * Argon2 hash algorithm for user password * Add fullname and email fields to user schema * Mix phx gen json scaffolding for users * Users context * mix format * User controller refactored for the new users context * Pow custom context for getting non-deleted users * Unique email for users * Update release task and seed for users * Users context prevents deletion of user with id 1 * Username cannot be updated * Created and updated timestamps in users view * Change of the username when users are deleted * Add lock feature to Users * Locked and deleted users are excluded from renew * Session controller tests updated * /me endpoint returns also user id * User socket authenticatio * User controller broadcast user actions on user topic * Access token expiration to 3 minutes * Fix could not delete admin user in context * Trento default admin enabled * User controller with spex * Fix app jwt auth plug test typo * User controller actions are dispatched to per user channels * fix access token test * fix alias in factory file * mix credo * fix mispell * mix credo * removed comments from user_controller_test * Addressing review feedbacks * Introduced users factory * Addresing feedbacks * Updated trento_dev sql regression dump * Revert "Updated trento_dev sql regression dump" This reverts commit ccce860. * Ecto seed with user password update on conflict * Run db seed on regression tests
- Loading branch information
Showing
33 changed files
with
1,328 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
defmodule Trento.Users do | ||
@moduledoc """ | ||
The Users context. | ||
""" | ||
|
||
use Pow.Ecto.Context, | ||
repo: Trento.Repo, | ||
user: Trento.Users.User | ||
|
||
import Ecto.Query, warn: false | ||
alias Trento.Repo | ||
|
||
alias Trento.Users.User | ||
|
||
@impl true | ||
@doc """ | ||
get_by function overrides the one defined in Pow.Ecto.Context, | ||
we retrieve the user by username as traditional Pow flow but we also exclude | ||
deleted and locked users | ||
""" | ||
def get_by(clauses) do | ||
username = clauses[:username] | ||
|
||
User | ||
|> where([u], is_nil(u.deleted_at) and is_nil(u.locked_at) and u.username == ^username) | ||
|> Repo.one() | ||
end | ||
|
||
def list_users do | ||
User | ||
|> where([u], is_nil(u.deleted_at)) | ||
|> Repo.all() | ||
end | ||
|
||
def get_user(id) do | ||
case User | ||
|> where([u], is_nil(u.deleted_at) and u.id == ^id) | ||
|> Repo.one() do | ||
nil -> {:error, :not_found} | ||
user -> {:ok, user} | ||
end | ||
end | ||
|
||
def create_user(attrs \\ %{}) do | ||
%User{} | ||
|> User.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
def update_user(%User{id: 1}, _), do: {:error, :operation_not_permitted} | ||
|
||
def update_user(%User{locked_at: nil} = user, %{enabled: false} = attrs) do | ||
do_update(user, Map.put(attrs, :locked_at, DateTime.utc_now())) | ||
end | ||
|
||
def update_user(%User{locked_at: locked_at} = user, %{enabled: false} = attrs) | ||
when not is_nil(locked_at) do | ||
do_update(user, attrs) | ||
end | ||
|
||
def update_user(%User{} = user, attrs) do | ||
do_update(user, Map.put(attrs, :locked_at, nil)) | ||
end | ||
|
||
def delete_user(%User{id: 1}), do: {:error, :operation_not_permitted} | ||
|
||
def delete_user(%User{} = user) do | ||
user | ||
|> User.delete_changeset(%{deleted_at: DateTime.utc_now()}) | ||
|> Repo.update() | ||
end | ||
|
||
defp do_update(user, attrs) do | ||
user | ||
|> User.update_changeset(attrs) | ||
|> Repo.update() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule TrentoWeb.UserChannel do | ||
@moduledoc """ | ||
User channel, each user is subscribed to his channel, | ||
to receive personal broadcasts | ||
Users can't join other users channel | ||
""" | ||
require Logger | ||
use TrentoWeb, :channel | ||
|
||
@impl true | ||
def join( | ||
"users:" <> user_id, | ||
_payload, | ||
%{assigns: %{current_user_id: current_user_id}} = socket | ||
) do | ||
user_id = String.to_integer(user_id) | ||
|
||
if user_id == current_user_id do | ||
{:ok, socket} | ||
else | ||
Logger.error( | ||
"Could not join user channel, requested user id: #{user_id}, authenticated user id: #{current_user_id}" | ||
) | ||
|
||
{:error, :unauthorized} | ||
end | ||
end | ||
|
||
def join("users:" <> _user_id, _payload, _socket) do | ||
{:error, :user_not_logged} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.