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

Flattened URLs (for discussion) #1381

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
31 changes: 0 additions & 31 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,6 @@ liveSocket.connect()

new Josh()

window.deploymentPolling = url => {
fetch(url, {
headers: {
Accept: 'application/json'
}
})
.then(response => response.json())
.then(json => {
let inflightUpdateBadges = $('#inflight-update-badges')
inflightUpdateBadges.empty()

let inflightEmpty = $('#inflight-empty')
if (json.inflight_updates.length == 0) {
inflightEmpty.html('No inflight updates')
} else {
inflightEmpty.empty()
}

json.inflight_updates.map(inflightUpdate => {
let badge = $(
`<span class="ff-m badge"><a href="${inflightUpdate.href}">${inflightUpdate.identifier}</a></span>`
)
inflightUpdateBadges.append(badge)
})

let deploymentPercentage = $('#deployment-percentage').first()
deploymentPercentage.html(`${json.percentage}%`)
deploymentPercentage.css('width', `${json.percentage}%`)
})
}

document.querySelectorAll('.date-time').forEach(d => {
d.innerHTML = dates.formatDateTime(d.innerHTML)
})
Expand Down
12 changes: 12 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ if level = System.get_env("LOG_LEVEL") do
config :logger, level: String.to_atom(level)
end

##
# Hashid configurations
#
alphabet = "abcdefghijkmnopqrstuvwxyz1234567890"

hashid_salt_org = System.get_env("HASHID_SALT_ORG", "ASUPERAMAZINGSALT")
hashid_salt_product = System.get_env("HASHID_SALT_PRODUCT", "ANOTHERSUPERAMAZINGSALT")

config :nerves_hub,
hashid_for_orgs: Hashids.new(alphabet: alphabet, salt: hashid_salt_org, min_len: 6),
hashid_for_products: Hashids.new(alphabet: alphabet, salt: hashid_salt_product, min_len: 6)

##
# Web and Device endpoints
#
Expand Down
13 changes: 12 additions & 1 deletion lib/nerves_hub/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ defmodule NervesHub.Accounts do
|> Repo.update()
end

def get_org_user!(org, user) do
OrgUser
|> where([ou], ou.org_id == ^org.id)
|> where([ou], ou.user_id == ^user.id)
|> OrgUser.with_user()
|> Repo.exclude_deleted()
|> Repo.one!()
end

def get_org_user(org, user) do
OrgUser
|> where([ou], ou.org_id == ^org.id)
Expand Down Expand Up @@ -234,8 +243,10 @@ defmodule NervesHub.Accounts do
def get_user_with_all_orgs_and_products(user_id) do
User
|> where(id: ^user_id)
|> join(:left, [u], o in assoc(u, :orgs))
|> join(:left, [u, o], p in assoc(o, :products))
|> Repo.exclude_deleted()
|> preload(orgs: [:products])
|> preload([u, o, p], orgs: {o, products: p})
|> Repo.one()
|> case do
nil -> {:error, :not_found}
Expand Down
13 changes: 13 additions & 0 deletions lib/nerves_hub/products.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ defmodule NervesHub.Products do

def __csv_header__, do: @csv_header

def get_product_by_user_and_id!(%User{id: user_id}, product_id) do
Product
|> join(:inner, [p], o in Org, on: p.org_id == o.id and is_nil(o.deleted_at))
|> join(:inner, [p, o], ou in OrgUser, on: o.id == ou.org_id and is_nil(ou.deleted_at))
|> join(:inner, [p, o, ou], u in User, on: ou.user_id == u.id and is_nil(u.deleted_at))
|> where([p, o, ou, u], u.id == ^user_id)
|> where([p, o, ou, u], p.id == ^product_id)
|> preload([p, o, ou, u], org: {o, org_users: ou})
|> Repo.one!()
end

def get_products_by_user_and_org(%User{id: user_id}, %Org{id: org_id}) do
query =
from(
Expand Down Expand Up @@ -67,6 +78,8 @@ defmodule NervesHub.Products do
def get_product_by_org_id_and_name(org_id, name) do
Product
|> Repo.exclude_deleted()
|> join(:inner, [p], o in Org, on: p.org_id == o.id and is_nil(o.deleted_at))
|> preload([p, o], org: o)
|> Repo.get_by(org_id: org_id, name: name)
|> case do
nil -> {:error, :not_found}
Expand Down
4 changes: 4 additions & 0 deletions lib/nerves_hub/role_validate_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ defmodule NervesHub.RoleValidateHelpers do
validate_org_user_role(conn, org, user, role)
end

def validate_role(%{assigns: %{product: product, user: user}} = conn, org: role) do
validate_org_user_role(conn, product.org, user, role)
end

def validate_role(conn, [{key, value}]) do
halt_role(conn, "#{key} #{value}")
end
Expand Down
8 changes: 8 additions & 0 deletions lib/nerves_hub_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ defmodule NervesHubWeb do

alias NervesHubWeb.Router.Helpers, as: Routes

import NervesHubWeb.Helpers.Hashids

# Routes generation with the ~p sigil
unquote(verified_routes())

Expand Down Expand Up @@ -131,6 +133,8 @@ defmodule NervesHubWeb do

unquote(view_helpers())

import NervesHubWeb.Helpers.Hashids

def ok(socket), do: {:ok, socket}
def noreply(socket), do: {:noreply, socket}

Expand Down Expand Up @@ -179,6 +183,8 @@ defmodule NervesHubWeb do

import NervesHubWeb.Components.SimpleActiveLink

import NervesHubWeb.Helpers.Hashids

# Include shared imports and aliases for views
unquote(view_helpers())

Expand Down Expand Up @@ -215,6 +221,8 @@ defmodule NervesHubWeb do

# Routes generation with the ~p sigil
unquote(verified_routes())

import NervesHubWeb.Helpers.Hashids
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/channels/user_console_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ defmodule NervesHubWeb.UserConsoleChannel do
false

org_user ->
Authorization.authorized?(:device_console, org_user)
Authorization.authorized?(:"device:console", org_user)
end
end
end
5 changes: 2 additions & 3 deletions lib/nerves_hub_web/components/device_header.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule NervesHubWeb.Components.DeviceHeader do

alias NervesHubWeb.LayoutView.DateTimeFormat

attr(:org, :any)
attr(:product, :any)
attr(:device, :any)
attr(:status, :any)
Expand Down Expand Up @@ -37,10 +36,10 @@ defmodule NervesHubWeb.Components.DeviceHeader do
<%= if is_nil(@device.firmware_metadata) do %>
<p>Unknown</p>
<% else %>
<a href={~p"/org/#{@org.name}/#{@product.name}/firmware/#{@device.firmware_metadata.uuid}"} class="badge ff-m">
<.link navigate={~p"/products/#{hashid(@product)}/firmware/#{@device.firmware_metadata.uuid}"} class="badge ff-m">
<%= @device.firmware_metadata.version %>
<%= @device.firmware_metadata.uuid %>
</a>
</.link>
<% end %>
</div>
<div>
Expand Down
56 changes: 32 additions & 24 deletions lib/nerves_hub_web/components/navigation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule NervesHubWeb.Components.Navigation do
<ul :if={Enum.any?(@user.orgs)} class="navbar-nav mr-auto flex-grow">
<li class="nav-item dropdown switcher">
<a class="nav-link dropdown-toggle org-select arrow-primary" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<%= if org = assigns[:org], do: org.name, else: "Select Org" %>
<%= org_name_header(assigns) %>
<%= if product = assigns[:product] do %>
<span class="workspace-divider">:</span> <%= product.name %>
<% end %>
Expand All @@ -36,7 +36,7 @@ defmodule NervesHubWeb.Components.Navigation do
<div class="dropdown-divider"></div>
<%= for org <- @user.orgs do %>
<div class="dropdown-submenu">
<.link href={~p"/org/#{org.name}"} class={"dropdown-item org #{org_classes(@current_path, org.name)}"}>
<.link href={~p"/orgs/#{hashid(org)}"} class={"dropdown-item org #{org_classes(@current_path, org.name)}"}>
<%= org.name %>
<div class="active-checkmark"></div>
</.link>
Expand All @@ -46,7 +46,7 @@ defmodule NervesHubWeb.Components.Navigation do
<%= unless Enum.empty?(org.products) do %>
<%= for product <- org.products do %>
<li>
<.link href={~p"/org/#{org.name}/#{product.name}/devices"} class={"dropdown-item product #{product_classes(@current_path, product.name)}"}>
<.link href={~p"/products/#{hashid(product)}/devices"} class={"dropdown-item product #{product_classes(@current_path, product.name)}"}>
<%= product.name %>
<div class="active-checkmark"></div>
</.link>
Expand All @@ -60,19 +60,19 @@ defmodule NervesHubWeb.Components.Navigation do
<div class="dropdown-divider"></div>
<% end %>

<a class="btn btn-outline-light mt-2 mb-3 ml-3 mr-3" aria-label="Create product" href={~p"/org/#{org.name}/new"}>
<.link navigate={~p"/orgs/#{hashid(org)}/new"} class="btn btn-outline-light mt-2 mb-3 ml-3 mr-3" aria-label="Create product">
<span class="action-text">Create Product</span>
<span class="button-icon add"></span>
</a>
</.link>
</ul>
<div class="dropdown-divider"></div>
</div>
<% end %>

<a class="btn btn-outline-light mt-2 mb-3 ml-3 mr-3" aria-label="Create organization" href={~p"/orgs/new"}>
<.link navigate={~p"/orgs/new"} class="btn btn-outline-light mt-2 mb-3 ml-3 mr-3" aria-label="Create organization">
<span class="action-text">Create Organization</span>
<div class="button-icon add"></div>
</a>
</.link>
</div>
</li>
</ul>
Expand Down Expand Up @@ -142,16 +142,16 @@ defmodule NervesHubWeb.Components.Navigation do

def sidebar_links(["orgs", "new"], _assigns), do: []

def sidebar_links(["org", _org_name] = path, assigns),
def sidebar_links(["orgs", _org_name] = path, assigns),
do: sidebar_org(assigns, path)

def sidebar_links(["org", _org_name, "new"] = path, assigns),
def sidebar_links(["orgs", _org_name, "new"] = path, assigns),
do: sidebar_org(assigns, path)

def sidebar_links(["org", _org_name, "settings" | _tail] = path, assigns),
def sidebar_links(["orgs", _org_name, "settings" | _tail] = path, assigns),
do: sidebar_org(assigns, path)

def sidebar_links(["org", _org_name | _tail] = path, assigns),
def sidebar_links(["products", _org_name | _tail] = path, assigns),
do: sidebar_product(assigns, path)

def sidebar_links(["account" | _tail] = path, assigns),
Expand All @@ -164,30 +164,30 @@ defmodule NervesHubWeb.Components.Navigation do
%{
title: "Products",
active: "",
href: ~p"/org/#{assigns.org.name}"
href: ~p"/orgs/#{hashid(assigns.org)}"
}
] ++
if assigns.org_user.role in NervesHub.Accounts.User.role_or_higher(:manage) do
[
%{
title: "Signing Keys",
active: "",
href: ~p"/org/#{assigns.org.name}/settings/keys"
href: ~p"/orgs/#{hashid(assigns.org)}/settings/keys"
},
%{
title: "Users",
active: "",
href: ~p"/org/#{assigns.org.name}/settings/users"
href: ~p"/orgs/#{hashid(assigns.org)}/settings/users"
},
%{
title: "Certificates",
active: "",
href: ~p"/org/#{assigns.org.name}/settings/certificates"
href: ~p"/orgs/#{hashid(assigns.org)}/settings/certificates"
},
%{
title: "Settings",
active: "",
href: ~p"/org/#{assigns.org.name}/settings"
href: ~p"/orgs/#{hashid(assigns.org)}/settings"
}
]
else
Expand All @@ -202,32 +202,32 @@ defmodule NervesHubWeb.Components.Navigation do
%{
title: "Devices",
active: "",
href: ~p"/org/#{assigns.org.name}/#{assigns.product.name}/devices"
href: ~p"/products/#{hashid(assigns.product)}/devices"
},
%{
title: "Firmware",
active: "",
href: ~p"/org/#{assigns.org.name}/#{assigns.product.name}/firmware"
href: ~p"/products/#{hashid(assigns.product)}/firmware"
},
%{
title: "Archives",
active: "",
href: ~p"/org/#{assigns.org.name}/#{assigns.product.name}/archives"
href: ~p"/products/#{hashid(assigns.product)}/archives"
},
%{
title: "Deployments",
active: "",
href: ~p"/org/#{assigns.org.name}/#{assigns.product.name}/deployments"
href: ~p"/products/#{hashid(assigns.product)}/deployments"
},
%{
title: "Scripts",
active: "",
href: ~p"/org/#{assigns.org.name}/#{assigns.product.name}/scripts"
href: ~p"/products/#{hashid(assigns.product)}/scripts"
},
%{
title: "Settings",
active: "",
href: ~p"/org/#{assigns.org.name}/#{assigns.product.name}/settings"
href: ~p"/products/#{hashid(assigns.product)}/settings"
}
]
|> sidebar_active(path)
Expand Down Expand Up @@ -264,7 +264,7 @@ defmodule NervesHubWeb.Components.Navigation do

defp org_classes(current_path, org_name) do
case path_pieces(current_path) do
["org", ^org_name | _] ->
["orgs", ^org_name | _] ->
"active"

_ ->
Expand All @@ -274,7 +274,7 @@ defmodule NervesHubWeb.Components.Navigation do

defp product_classes(current_path, product_name) do
case path_pieces(current_path) do
["org", _, ^product_name | _] ->
["orgs", _, ^product_name | _] ->
"active"

_ ->
Expand All @@ -295,4 +295,12 @@ defmodule NervesHubWeb.Components.Navigation do
def device_count(_conn) do
nil
end

defp org_name_header(assigns) do
cond do
org = assigns[:org] -> org.name
product = assigns[:product] -> product.org.name
true -> "Select Org"
end
end
end
Loading
Loading