Skip to content

Commit

Permalink
feat: view system (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
pichoemr authored Nov 8, 2022
1 parent 720a21a commit 5a6612f
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .vscode/thunder-tests/thunderEnvironment.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"default": true,
"sortNum": 10000,
"created": "2022-02-07T13:10:08.908Z",
"modified": "2022-09-22T08:33:12.317Z",
"modified": "2022-11-08T09:29:59.165Z",
"data": [
{
"name": "endpoint",
Expand Down Expand Up @@ -49,7 +49,7 @@
},
{
"name": "access_token",
"value": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJsZW5yYSIsImV4cCI6MTY2NjI1NDc3OSwiaWF0IjoxNjYzODM1NTc5LCJpc3MiOiJsZW5yYSIsImp0aSI6IjBhNTk4MTM3LTA1ZTgtNDU0Ny04Mzg5LTk3NzUwZDE5ZTJmOCIsIm5iZiI6MTY2MzgzNTU3OCwic3ViIjoiMSIsInR5cCI6ImFjY2VzcyJ9.ovt3JTCBvrcreBjw4hjiiOlCKkA88gJ_6ARcMUBF-q-f374ruwmIHlwOwep1l1nhaeNaCvEdphADHEG6u41J3w"
"value": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJsZW5yYSIsImV4cCI6MTY3MDMxODk5OSwiaWF0IjoxNjY3ODk5Nzk5LCJpc3MiOiJsZW5yYSIsImp0aSI6IjI4NzM3YmNjLTVhZjYtNDUwNi05ZmFjLWZiNDdmNzBhZjU4MCIsIm5iZiI6MTY2Nzg5OTc5OCwic3ViIjoiMSIsInR5cCI6ImFjY2VzcyJ9.QG3U3HW1m52jo15jb7GOYmBp9RKwo2YellgYC7T01WAoj4JJ8TK3TEU3uObH4DrB-oKN7VzNmV2QqbZxMpexow"
},
{
"name": "app_name",
Expand Down
8 changes: 4 additions & 4 deletions .vscode/thunder-tests/thunderclient.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"method": "POST",
"sortNum": 10000,
"created": "2022-02-07T13:14:55.835Z",
"modified": "2022-02-08T10:38:10.992Z",
"modified": "2022-11-08T10:07:54.823Z",
"headers": [],
"params": [],
"body": {
Expand All @@ -19,7 +19,7 @@
"tests": [
{
"type": "set-env-var",
"custom": "json.data.access_token",
"custom": "header.access_token",
"action": "setto",
"value": "{{access_token}}"
}
Expand All @@ -34,7 +34,7 @@
"method": "POST",
"sortNum": 20000,
"created": "2022-02-07T13:20:09.115Z",
"modified": "2022-02-08T10:38:17.865Z",
"modified": "2022-11-08T09:29:59.166Z",
"headers": [],
"params": [],
"body": {
Expand All @@ -45,7 +45,7 @@
"tests": [
{
"type": "set-env-var",
"custom": "json.data.access_token",
"custom": "header.access_token",
"action": "setto",
"value": "{{access_token}}"
}
Expand Down
3 changes: 2 additions & 1 deletion apps/lenra/lib/lenra/errors/business_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Lenra.Errors.BusinessError do
{:not_latest_cgu, "Not latest CGU."},
{:did_not_accept_cgu, "You must accept the CGU to use Lenra"},
{:no_app_found, "No application found for the current link"},
{:invitation_wrong_email, "Cannot accept the invitation with this email."}
{:invitation_wrong_email, "Cannot accept the invitation with this email."},
{:application_not_built, "Your application has not been built yet."}
]
end
4 changes: 2 additions & 2 deletions apps/lenra/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ defmodule Lenra.MixProject do
{:libcluster, "~> 3.3"},
{:application_runner,
git: "https://github.com/lenra-io/application-runner.git",
tag: "v1.0.0-beta.70",
tag: "v1.0.0-beta.83",
submodules: true},
{:lenra_common, git: "https://github.com/lenra-io/lenra-common.git", tag: "v2.3.1"}
{:lenra_common, git: "https://github.com/lenra-io/lenra-common.git", tag: "v2.4.0"}

]
end
Expand Down
37 changes: 32 additions & 5 deletions apps/lenra_web/lib/lenra_web/app_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,34 @@ defmodule LenraWeb.AppAdapter do
alias Lenra.Accounts.User
alias Lenra.{Apps, Repo}
alias Lenra.Apps.{App, Environment, MainEnv}
alias Lenra.Errors.BusinessError

@impl ApplicationRunner.Adapter
def allow(user_id, app_name) do
with %App{} = application <- Repo.get_by(App, service_name: app_name),
with %App{} = app <- get_app(app_name),
%App{} = application <- Repo.preload(app, main_env: [:environment]),
%User{} = user <- Accounts.get_user(user_id) do
Bouncer.allow(LenraWeb.AppAdapter.Policy, :join_app, user, application)
else
_err ->
false
BusinessError.forbidden_tuple()
end
end

@impl ApplicationRunner.Adapter
def get_function_name(app_name) do
lenra_env = Application.fetch_env!(:lenra, :lenra_env)

with %App{} = app <- Repo.get_by(App, service_name: app_name),
with %App{} = app <- get_app(app_name),
%App{} = application <-
Repo.preload(app, main_env: [environment: [:deployed_build]]) do
build_number = application.main_env.environment.deployed_build.build_number
String.downcase("#{lenra_env}-#{app_name}-#{build_number}")
build = application.main_env.environment.deployed_build

if build do
String.downcase("#{lenra_env}-#{app_name}-#{build.build_number}")
else
BusinessError.application_not_built_tuple()
end
end
end

Expand All @@ -42,6 +49,26 @@ defmodule LenraWeb.AppAdapter do
List.first(application.environments).id
end

@impl ApplicationRunner.Adapter
def resource_from_params(params) do
case LenraWeb.Guardian.resource_from_token(params["token"]) do
{:ok, user, _claims} ->
{:ok, user.id}

_error ->
BusinessError.forbidden_tuple()
end
end

defp get_app(app_name) do
App
|> Repo.get_by(service_name: app_name)
|> case do
nil -> BusinessError.no_app_found_tuple()
%App{} = app -> app
end
end

defmodule Policy do
@moduledoc """
This policy defines the rules to join an application.
Expand Down
6 changes: 0 additions & 6 deletions apps/lenra_web/lib/lenra_web/channels/app_channel.ex

This file was deleted.

5 changes: 5 additions & 0 deletions apps/lenra_web/lib/lenra_web/channels/app_socket.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule LenraWeb.AppSocket do
use ApplicationRunner.AppSocket,
adapter: LenraWeb.AppAdapter,
route_channel: LenraWeb.RouteChannel
end
6 changes: 6 additions & 0 deletions apps/lenra_web/lib/lenra_web/channels/route_channel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule LenraWeb.RouteChannel do
@moduledoc """
LenraWeb.RouteChannel use ApplicationRunner.RouteChannel
"""
use ApplicationRunner.RouteChannel
end
13 changes: 0 additions & 13 deletions apps/lenra_web/lib/lenra_web/channels/user_socket.ex

This file was deleted.

2 changes: 1 addition & 1 deletion apps/lenra_web/lib/lenra_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule LenraWeb.Endpoint do
signing_salt: "MFyEizGS"
]

socket "/socket", LenraWeb.UserSocket,
socket "/socket", LenraWeb.AppSocket,
websocket: true,
longpoll: false

Expand Down
3 changes: 1 addition & 2 deletions apps/lenra_web/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ defmodule LenraWeb.MixProject do
{:lenra, in_umbrella: true},
{:cors_plug, "~> 3.0", only: :dev, runtime: false},
{:bouncer, git: "https://github.com/lenra-io/bouncer.git", tag: "v1.0.0"},
{:lenra_common, git: "https://github.com/lenra-io/lenra-common.git", tag: "v2.3.1"}

{:lenra_common, git: "https://github.com/lenra-io/lenra-common.git", tag: "v2.4.0"}
]
end
end
20 changes: 10 additions & 10 deletions apps/lenra_web/test/channels/app_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ defmodule LenraWeb.AppChannelTest do
# "patch" => [%{"op" => "replace", "path" => "/root/children/0/value", "value" => "Hello Bob"}]
# }

setup do
{:ok, %{inserted_user: user}} = register_john_doe()
socket = socket(UserSocket, "socket_id", %{user: user})
# setup do
# {:ok, %{inserted_user: user}} = register_john_doe()
# socket = socket(UserSocket, "socket_id", %{user: user})

%{socket: socket, user: user}
end
# %{socket: socket, user: user}
# end

test "No app called, should return an error", %{socket: socket} do
res = my_subscribe_and_join(socket)
# test "No app called, should return an error", %{socket: socket} do
# res = my_subscribe_and_join(socket)

assert {:error, %{"message" => "No application found for the current link", "reason" => :no_app_found}} = res
# assert {:error, %{"message" => "No application found for the current link", "reason" => :no_app_found}} = res

refute_push("ui", _)
end
# refute_push("ui", _)
# end

# test "Base use case with simple app", %{socket: socket, user: user} do
# # owstub
Expand Down
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ config :application_runner,
faas_auth: System.get_env("FAAS_AUTH", "Basic YWRtaW46Z0Q4VjNHR1YxeUpS"),
faas_registry: System.get_env("FAAS_REGISTRY", "registry.gitlab.com/lenra/platform/lenra-ci"),
env: Mix.env() |> Atom.to_string(),
mongo_url: System.get_env("MONGO_URL", "mongodb://localhost:27017")
mongo_url: System.get_env("MONGO_URL", "mongodb://localhost:27017"),
listeners_timeout: 1 * 60 * 60 * 1000

# additional_session_modules: {LenraWeb.ApplicationRunnerAdapter, :additional_session_modules},
# additional_env_modules: {LenraWeb.ApplicationRunnerAdapter, :additional_env_modules}
Expand Down
7 changes: 4 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%{
"application_runner": {:git, "https://github.com/lenra-io/application-runner.git", "30bbe57869a88e858d0609118c5df0e38f42211e", [tag: "v1.0.0-beta.70", submodules: true]},
"application_runner": {:git, "https://github.com/lenra-io/application-runner.git", "31d8276f9ac82db85fa061fbeb63bef17217c11e", [tag: "v1.0.0-beta.83", submodules: true]},
"argon2_elixir": {:hex, :argon2_elixir, "2.4.1", "edb27bdd326bc738f3e4614eddc2f73507be6fedc9533c6bcc6f15bbac9c85cc", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "0e21f52a373739d00bdfd5fe6da2f04eea623cb4f66899f7526dd9db03903d9f"},
"bamboo": {:hex, :bamboo, "2.1.0", "3c58f862efd74fa8c8d48a410ac592b41f7d24785e828566f7a0af549269ddc3", [:mix], [{:hackney, ">= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.4", [hex: :mime, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "f0ad2623b9a1d2dc06dcf289b59df9ebc522f49f3a21971ec87a8fce04e6d33e"},
"bamboo_smtp": {:hex, :bamboo_smtp, "4.0.1", "7e48188663f6164a81183688bb263be4c3952648fcd3ce52164f44d68777f9cd", [:mix], [{:bamboo, "~> 2.1.0", [hex: :bamboo, repo: "hexpm", optional: false]}, {:gen_smtp, "~> 1.1.1", [hex: :gen_smtp, repo: "hexpm", optional: false]}], "hexpm", "7ff1d62ae39bfb1c14f6d3cddba0fa1482a45c2a2b497a2da601eff7099605c8"},
Expand All @@ -16,6 +16,7 @@
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.3.1", "ebd1a1d7aff97f27c66654e78ece187abdc646992714164380d8a041eda16754", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3a6efd3366130eab84ca372cbd4a7d3c3a97bdfcfb4911233b035d117063f0af"},
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},
"credo": {:hex, :credo, "1.6.4", "ddd474afb6e8c240313f3a7b0d025cc3213f0d171879429bf8535d7021d9ad78", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "c28f910b61e1ff829bffa056ef7293a8db50e87f2c57a9b5c3f57eee124536b7"},
"crontab": {:hex, :crontab, "1.1.11", "4028ced51b813a5061f85b689d4391ef0c27550c8ab09aaf139e4295c3d93ea4", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "ecb045f9ac14a3e2990e54368f70cdb6e2f2abafc5bc329d6c31f0c74b653787"},
"db_connection": {:hex, :db_connection, "2.4.2", "f92e79aff2375299a16bcb069a14ee8615c3414863a6fef93156aee8e86c2ff3", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4fe53ca91b99f55ea249693a0229356a08f4d1a7931d8ffa79289b145fe83668"},
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
Expand Down Expand Up @@ -45,7 +46,7 @@
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"jose": {:hex, :jose, "1.11.2", "f4c018ccf4fdce22c71e44d471f15f723cb3efab5d909ab2ba202b5bf35557b3", [:mix, :rebar3], [], "hexpm", "98143fbc48d55f3a18daba82d34fe48959d44538e9697c08f34200fa5f0947d2"},
"json_diff": {:hex, :json_diff, "0.1.3", "c80d5ca5416e785867e765e906e9a91b7efc35bfd505af276654d108f4995736", [:mix], [], "hexpm", "a5332e8293e7e9f384d34ea44645d7961334db73739165178fd4a7728d06f7d1"},
"lenra_common": {:git, "https://github.com/lenra-io/lenra-common.git", "463d125bcdc3ee1177c5a237fb8b458a059c59a0", [tag: "v2.3.1"]},
"lenra_common": {:git, "https://github.com/lenra-io/lenra-common.git", "73e710692b5d52816827e7156af39ab7e0caf0f0", [tag: "v2.4.0"]},
"libcluster": {:hex, :libcluster, "3.3.1", "e7a4875cd1290cee7a693d6bd46076863e9e433708b01339783de6eff5b7f0aa", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "b575ca63c1cd84e01f3fa0fc45e6eb945c1ee7ae8d441d33def999075e9e5398"},
"libring": {:hex, :libring, "1.6.0", "d5dca4bcb1765f862ab59f175b403e356dec493f565670e0bacc4b35e109ce0d", [:mix], [], "hexpm", "5e91ece396af4bce99953d49ee0b02f698cd38326d93cd068361038167484319"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
Expand Down Expand Up @@ -73,7 +74,7 @@
"plug_crypto": {:hex, :plug_crypto, "1.2.2", "05654514ac717ff3a1843204b424477d9e60c143406aa94daf2274fdd280794d", [:mix], [], "hexpm", "87631c7ad914a5a445f0a3809f99b079113ae4ed4b867348dd9eec288cecb6db"},
"poison": {:hex, :poison, "5.0.0", "d2b54589ab4157bbb82ec2050757779bfed724463a544b6e20d79855a9e43b24", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "11dc6117c501b80c62a7594f941d043982a1bd05a1184280c0d9166eb4d8d3fc"},
"postgrex": {:hex, :postgrex, "0.15.13", "7794e697481799aee8982688c261901de493eb64451feee6ea58207d7266d54a", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "3ffb76e1a97cfefe5c6a95632a27ffb67f28871c9741fb585f9d1c3cd2af70f1"},
"query_parser": {:git, "https://github.com/lenra-io/query-parser.git", "d7a9b6d07345b7e9430d88ebcc931e1ec12ad325", [tag: "v1.0.0-beta.13"]},
"query_parser": {:git, "https://github.com/lenra-io/query-parser.git", "c4d37a7da86292d9e2729d91084574c7fbc7ad32", [tag: "v1.0.0-beta.15"]},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"sentry": {:hex, :sentry, "8.0.6", "c8de1bf0523bc120ec37d596c55260901029ecb0994e7075b0973328779ceef7", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, "~> 2.3", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm", "051a2d0472162f3137787c7c9d6e6e4ef239de9329c8c45b1f1bf1e9379e1883"},
"simplehttp": {:hex, :simplehttp, "0.5.1", "103d027c50398b1e2cf26329cd78d8cf55211c17d19e0bb258a7987fe8df3584", [:mix], [], "hexpm", "32a945235f59cdd6615478f143807f79416555559bf0a701971570628a6884f1"},
Expand Down

0 comments on commit 5a6612f

Please sign in to comment.