Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose metric registry for santiment team members outside of admin panel #4476

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/sanbase/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,12 @@ defmodule Sanbase.Accounts.User do
def by_id(user_id, opts \\ [])

def by_id(user_id, opts) when is_integer(user_id) do
query = from(u in __MODULE__, where: u.id == ^user_id)
# TODO: Make preload configurable via the opts
query =
from(u in __MODULE__,
where: u.id == ^user_id,
preload: [:eth_accounts, :user_settings, [roles: :role]]
)

query =
case Keyword.get(opts, :lock_for_update, false) do
Expand All @@ -230,7 +235,7 @@ defmodule Sanbase.Accounts.User do
u in __MODULE__,
where: u.id in ^user_ids,
order_by: fragment("array_position(?, ?::int)", ^user_ids, u.id),
preload: [:eth_accounts, :user_settings]
preload: [:eth_accounts, :user_settings, [roles: :role]]
)
|> Repo.all()

Expand Down Expand Up @@ -324,6 +329,10 @@ defmodule Sanbase.Accounts.User do
end
end

def has_role?(%__MODULE__{roles: roles}, role) when is_list(roles) do
Enum.any?(roles, &(&1.role.name == role))
end

def change_name(%__MODULE__{name: name} = user, name), do: {:ok, user}

def change_name(%__MODULE__{} = user, name) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule SanbaseWeb.MetricRegistryFormLive do
alias SanbaseWeb.AvailableMetricsComponents

@impl true
def mount(params, _session, socket) do
def mount(params, session, socket) do
{:ok, metric_registry} =
case socket.assigns.live_action do
:new -> {:ok, %Registry{}}
Expand Down
50 changes: 50 additions & 0 deletions lib/sanbase_web/plug/santiment_team_member_only.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
defmodule SanbaseWeb.Plug.SantimentTeamMemberOnly do
@moduledoc ~s"""
Check if the container type allows access to the admin dashboard
endpoints. T
"""

@behaviour Plug

import Plug.Conn

def init(opts), do: opts

def call(conn, _) do
case get_in(conn.private, [:san_authentication, :auth, :current_user]) do
%Sanbase.Accounts.User{} = user ->
if santiment_member?(user) do
conn
else
conn
|> send_resp(403, "Forbidden")
|> halt()
end

_ ->
conn
|> send_resp(403, "Forbidden")
|> halt()
end
end

defp santiment_member?(%Sanbase.Accounts.User{} = user) do
cond do
user_has_access_by_role?(user) -> true
is_binary(user.email) and String.ends_with?(user.email, "@santiment.net") -> true
true -> false
end
end

defp user_has_access_by_role?(user) do
Enum.any?(
user.roles,
&(&1.role.name in [
"Santiment Team Member",
"Santiment WebPanel Viewer",
"Santiment WebPanel Editor",
"Santiment WebPanel Admin"
])
)
end
end
15 changes: 15 additions & 0 deletions lib/sanbase_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ defmodule SanbaseWeb.Router do
plug(SanbaseWeb.Plug.AdminPodOnly)
end

pipeline :santiment_user_access do
plug(SanbaseWeb.Graphql.AuthPlug)
plug(SanbaseWeb.Plug.SantimentTeamMemberOnly)
end

pipeline :browser do
plug(:accepts, ["html"])
plug(:fetch_session)
Expand Down Expand Up @@ -50,6 +55,16 @@ defmodule SanbaseWeb.Router do
get("/:provider/callback", AuthController, :callback)
end

scope "/metric_registry", SanbaseWeb do
pipe_through([:browser, :santiment_user_access])

live("/", MetricRegistryIndexLive)
live("/change_suggestions", MetricRegistryChangeSuggestionsLive)
live("/show/:id", MetricRegistryShowLive)
live("/edit/:id", MetricRegistryFormLive, :edit)
live("/new", MetricRegistryFormLive, :new)
end

scope "/forms", SanbaseWeb do
pipe_through(:browser)
live("/", FormsLive)
Expand Down
19 changes: 19 additions & 0 deletions priv/repo/migrations/20241121133719_add_more_user_roles.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Sanbase.Repo.Migrations.AddMoreUserRoles do
use Ecto.Migration

def up do
execute("""
INSERT INTO roles (id, name) VALUES
((SELECT COALESCE(MAX(id), 0) + 1 FROM roles), 'Santiment WebPanel Viewer'),
((SELECT COALESCE(MAX(id), 0) + 2 FROM roles), 'Santiment WebPanel Editor'),
((SELECT COALESCE(MAX(id), 0) + 3 FROM roles), 'Santiment WebPanel Admin')
""")
end

def down do
execute("""
DELETE FROM roles
WHERE name IN ('Santiment WebPanel Viewer', 'Santiment WebPanel Editor', 'Santiment WebPanel Admin')
""")
end
end
1 change: 1 addition & 0 deletions priv/repo/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9622,3 +9622,4 @@ INSERT INTO public."schema_migrations" (version) VALUES (20241108112754);
INSERT INTO public."schema_migrations" (version) VALUES (20241114140339);
INSERT INTO public."schema_migrations" (version) VALUES (20241114141110);
INSERT INTO public."schema_migrations" (version) VALUES (20241116104556);
INSERT INTO public."schema_migrations" (version) VALUES (20241121133719);