Skip to content

Commit

Permalink
Restrict teams features based on org status
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Dec 17, 2024
1 parent cd46b5c commit ac96e4b
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/livebook/hubs/team.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule Livebook.Hubs.Team do
field :session_token, :string, redact: true
field :hub_name, :string
field :hub_emoji, :string
field :active, :boolean, default: true

embeds_one :offline, Offline
end
Expand Down
27 changes: 26 additions & 1 deletion lib/livebook/hubs/team_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ defmodule Livebook.Hubs.TeamClient do

defp handle_event(:user_connected, user_connected, state) do
state
|> update_hub(user_connected)
|> dispatch_secrets(user_connected)
|> dispatch_file_systems(user_connected)
|> dispatch_deployment_groups(user_connected)
Expand Down Expand Up @@ -728,6 +729,10 @@ defmodule Livebook.Hubs.TeamClient do
state
end

defp handle_event(:org_updated, org_updated, state) do
update_hub(state, org_updated)
end

defp dispatch_secrets(state, %{secrets: secrets}) do
decrypted_secrets = Enum.map(secrets, &build_secret(state, &1))

Expand Down Expand Up @@ -796,7 +801,27 @@ defmodule Livebook.Hubs.TeamClient do
state
end

defp update_hub(state, %{public_key: org_public_key}) do
defp update_hub(state, %LivebookProto.UserConnected{org_active: active}) do
hub = %{state.hub | active: active}

if Livebook.Hubs.hub_exists?(hub.id) do
Hubs.save_hub(hub)
end

%{state | hub: hub}
end

defp update_hub(state, %LivebookProto.OrgUpdated{active: active}) do
hub = %{state.hub | active: active}

if Livebook.Hubs.hub_exists?(hub.id) do
Hubs.save_hub(hub)
end

%{state | hub: hub}
end

defp update_hub(state, %LivebookProto.AgentConnected{public_key: org_public_key}) do
hub = %{state.hub | org_public_key: org_public_key}

if Livebook.Hubs.hub_exists?(hub.id) do
Expand Down
2 changes: 2 additions & 0 deletions lib/livebook_web/live/hub/edit/personal_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ defmodule LivebookWeb.Hub.Edit.PersonalComponent do
id="hub-file-systems-list"
hub_id={@hub.id}
file_systems={@file_systems}
disabled={false}
/>
</div>
Expand Down Expand Up @@ -195,6 +196,7 @@ defmodule LivebookWeb.Hub.Edit.PersonalComponent do
hub={@hub}
secret_name={@secret_name}
secret_value={@secret_value}
disabled={false}
return_to={~p"/hub/#{@hub.id}"}
/>
</.modal>
Expand Down
24 changes: 22 additions & 2 deletions lib/livebook_web/live/hub/edit/team_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ defmodule LivebookWeb.Hub.Edit.TeamComponent do
{Provider.connection_status(@hub)}
</LayoutComponents.topbar>
<LayoutComponents.topbar :if={[email protected]} variant="warning">
<h2>
Workspace disabled: your organization doesn't have an active subscription. Please contact your <.link
href={org_url(@hub, "/users")}
class="underline"
>org's admin</.link>.
</h2>
</LayoutComponents.topbar>
<div class="p-4 md:px-12 md:py-7 max-w-screen-md mx-auto">
<div id={"#{@id}-component"}>
<div class="mb-8 flex flex-col space-y-2">
Expand Down Expand Up @@ -176,10 +185,15 @@ defmodule LivebookWeb.Hub.Edit.TeamComponent do
secrets={@secrets}
edit_path={"hub/#{@hub.id}/secrets/edit"}
return_to={~p"/hub/#{@hub.id}"}
disabled={not @hub.active}
/>
<div>
<.button patch={~p"/hub/#{@hub.id}/secrets/new"} id="add-secret">
<.button
patch={~p"/hub/#{@hub.id}/secrets/new"}
id="add-secret"
disabled={not @hub.active}
>
Add secret
</.button>
</div>
Expand All @@ -200,6 +214,7 @@ defmodule LivebookWeb.Hub.Edit.TeamComponent do
hub_id={@hub.id}
file_systems={@file_systems}
target={@myself}
disabled={not @hub.active}
/>
</div>
Expand Down Expand Up @@ -233,7 +248,11 @@ defmodule LivebookWeb.Hub.Edit.TeamComponent do
</div>
<div>
<.button patch={~p"/hub/#{@hub.id}/groups/new"} id="add-deployment-group">
<.button
patch={~p"/hub/#{@hub.id}/groups/new"}
id="add-deployment-group"
disabled={not @hub.active}
>
Add deployment group
</.button>
</div>
Expand Down Expand Up @@ -289,6 +308,7 @@ defmodule LivebookWeb.Hub.Edit.TeamComponent do
secret_name={@secret_name}
secret_value={@secret_value}
return_to={~p"/hub/#{@hub.id}"}
disabled={not @hub.active}
/>
</.modal>
Expand Down
5 changes: 5 additions & 0 deletions lib/livebook_web/live/hub/edit_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule LivebookWeb.Hub.EditLive do
def mount(_params, _session, socket) do
if connected?(socket) do
Hubs.Broadcasts.subscribe([:connection])

Livebook.Teams.Broadcasts.subscribe([:deployment_groups, :app_deployments, :agents])
end

Expand Down Expand Up @@ -111,6 +112,10 @@ defmodule LivebookWeb.Hub.EditLive do
{:noreply, load_hub(socket, id)}
end

def handle_info({:hub_changed, id}, %{assigns: %{hub: %{id: id}}} = socket) do
{:noreply, load_hub(socket, id)}
end

def handle_info(_message, socket) do
{:noreply, socket}
end
Expand Down
8 changes: 7 additions & 1 deletion lib/livebook_web/live/hub/file_system_form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ defmodule LivebookWeb.Hub.FileSystemFormComponent do
</p>
<% end %>
<div class="flex space-x-2">
<.button type="submit" disabled={not @changeset.valid?}>
<.button
type="submit"
disabled={
not @changeset.valid? or
match?(%Livebook.Hubs.Team{active: false}, @hub)
}
>
<.remix_icon icon={@button.icon} />
<span class="font-normal">{@button.label}</span>
</.button>
Expand Down
6 changes: 5 additions & 1 deletion lib/livebook_web/live/hub/file_system_list_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ defmodule LivebookWeb.Hub.FileSystemListComponent do
</div>
</div>
<div class="flex">
<.button patch={~p"/hub/#{@hub_id}/file-systems/new"} id="add-file-system">
<.button
patch={~p"/hub/#{@hub_id}/file-systems/new"}
id="add-file-system"
disabled={@disabled}
>
Add file storage
</.button>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/livebook_web/live/hub/secret_form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ defmodule LivebookWeb.Hub.SecretFormComponent do
<.hidden_field field={f[:hub_id]} value={@hub.id} />
<.hidden_field field={f[:deployment_group_id]} value={@deployment_group_id} />
<div class="flex space-x-2">
<.button type="submit" disabled={not @changeset.valid?}>
<.button type="submit" disabled={not @changeset.valid? or @disabled}>
<.remix_icon icon={@button.icon} />
<span class="font-normal">{@button.label}</span>
</.button>
Expand Down
1 change: 1 addition & 0 deletions proto/lib/livebook_proto/agent_connected.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ defmodule LivebookProto.AgentConnected do
json_name: "appDeployments"

field :agents, 9, repeated: true, type: LivebookProto.Agent
field :org_active, 10, type: :bool, json_name: "orgActive"
end
2 changes: 2 additions & 0 deletions proto/lib/livebook_proto/event.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ defmodule LivebookProto.Event do
type: LivebookProto.AppDeploymentStopped,
json_name: "appDeploymentStopped",
oneof: 0

field :org_updated, 17, type: LivebookProto.OrgUpdated, json_name: "orgUpdated", oneof: 0
end
6 changes: 6 additions & 0 deletions proto/lib/livebook_proto/org_updated.pb.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule LivebookProto.OrgUpdated do
use Protobuf, protoc_gen_elixir_version: "0.13.0", syntax: :proto3

field :id, 1, type: :string
field :active, 2, type: :bool
end
1 change: 1 addition & 0 deletions proto/lib/livebook_proto/user_connected.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ defmodule LivebookProto.UserConnected do
json_name: "appDeployments"

field :agents, 6, repeated: true, type: LivebookProto.Agent
field :org_active, 7, type: :bool, json_name: "orgActive"
end
8 changes: 8 additions & 0 deletions proto/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ message UserConnected {
repeated DeploymentGroup deployment_groups = 4;
repeated AppDeployment app_deployments = 5;
repeated Agent agents = 6;
bool org_active = 7;
}

message AgentConnected {
Expand All @@ -118,6 +119,7 @@ message AgentConnected {
repeated DeploymentGroup deployment_groups = 7;
repeated AppDeployment app_deployments = 8;
repeated Agent agents = 9;
bool org_active = 10;
}

message AppDeployment {
Expand Down Expand Up @@ -154,6 +156,11 @@ message AgentLeft {
string id = 1;
}

message OrgUpdated {
string id = 1;
bool active = 2;
}

message Agent {
string id = 1;
string name = 2;
Expand Down Expand Up @@ -207,5 +214,6 @@ message Event {
AgentJoined agent_joined = 14;
AgentLeft agent_left = 15;
AppDeploymentStopped app_deployment_stopped = 16;
OrgUpdated org_updated = 17;
}
}

0 comments on commit ac96e4b

Please sign in to comment.