-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create user auth tokens & source read apis
- Loading branch information
Loïc Knuchel
committed
Nov 11, 2023
1 parent
5fc0d94
commit f763a78
Showing
27 changed files
with
363 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule Azimutt.Accounts.UserAuthToken do | ||
@moduledoc "Auth tokens allowing to identify a user simply by passing this token" | ||
use Ecto.Schema | ||
use Azimutt.Schema | ||
import Ecto.Changeset | ||
alias Azimutt.Accounts.User | ||
|
||
schema "user_auth_tokens" do | ||
belongs_to :user, User | ||
field :name, :string | ||
field :nb_access, :integer | ||
field :last_access, :utc_datetime_usec | ||
field :expire_at, :utc_datetime_usec | ||
timestamps(updated_at: false) | ||
field :deleted_at, :utc_datetime_usec | ||
end | ||
|
||
def create_changeset(token, attrs) do | ||
token | ||
|> cast(attrs, [:name, :expire_at]) | ||
|> put_change(:user_id, attrs.user_id) | ||
|> put_change(:nb_access, 0) | ||
|> validate_required([:name]) | ||
end | ||
|
||
def access_changeset(token, now) do | ||
token | ||
|> cast(%{last_access: now, nb_access: token.nb_access + 1}, [:last_access, :nb_access]) | ||
end | ||
|
||
def delete_changeset(token, now) do | ||
token | ||
|> cast(%{deleted_at: now}, [:deleted_at]) | ||
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
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
41 changes: 41 additions & 0 deletions
41
backend/lib/azimutt_web/controllers/api/source_controller.ex
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,41 @@ | ||
defmodule AzimuttWeb.Api.SourceController do | ||
use AzimuttWeb, :controller | ||
use PhoenixSwagger | ||
alias Azimutt.Projects | ||
alias Azimutt.Projects.Project | ||
alias Azimutt.Utils.Result | ||
alias AzimuttWeb.Utils.CtxParams | ||
action_fallback AzimuttWeb.Api.FallbackController | ||
|
||
def index(conn, %{"organization_id" => _organization_id, "project_id" => project_id} = params) do | ||
current_user = conn.assigns.current_user | ||
ctx = CtxParams.from_params(params) | ||
|
||
with {:ok, %Project{} = project} <- Projects.get_project(project_id, current_user), | ||
{:ok, content} <- Projects.get_project_content(project) |> Result.flat_map(fn c -> Jason.decode(c) end), | ||
do: conn |> render("index.json", sources: content["sources"], ctx: ctx) | ||
end | ||
|
||
def show(conn, %{"organization_id" => _organization_id, "project_id" => project_id, "source_id" => source_id} = params) do | ||
current_user = conn.assigns.current_user | ||
ctx = CtxParams.from_params(params) | ||
|
||
with {:ok, %Project{} = project} <- Projects.get_project(project_id, current_user), | ||
{:ok, content} <- Projects.get_project_content(project) |> Result.flat_map(fn c -> Jason.decode(c) end), | ||
{:ok, source} <- content["sources"] |> Enum.find(fn s -> s["id"] == source_id end) |> Result.from_nillable(), | ||
do: conn |> render("show.json", source: source, ctx: ctx) | ||
end | ||
|
||
def update(conn, %{"organization_organization_id" => _organization_id, "project_id" => _project_id, "source_id" => _source_id} = _params) do | ||
# FIXME | ||
conn |> send_resp(:not_found, "WIP") | ||
end | ||
|
||
def create(conn, %{"organization_organization_id" => _organization_id, "project_id" => _project_id} = _params) do | ||
conn |> send_resp(:not_found, "WIP") | ||
end | ||
|
||
def delete(conn, %{"organization_organization_id" => _organization_id, "project_id" => _project_id, "source_id" => _source_id}) do | ||
conn |> send_resp(:not_found, "WIP") | ||
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
Oops, something went wrong.