From de0d6e92c2f588ea4e31206c7090d535a15769f7 Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Wed, 6 May 2020 00:33:01 -0700 Subject: [PATCH 1/8] Add device foreign key to firewall_rules --- .../20200228154815_create_firewall_rules.exs | 3 +++ config/config.exs | 2 +- config/dev.exs | 10 +++++----- mix.lock | 10 +++++----- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/apps/cf_http/priv/repo/migrations/20200228154815_create_firewall_rules.exs b/apps/cf_http/priv/repo/migrations/20200228154815_create_firewall_rules.exs index 336a27fdb..40a2456d2 100644 --- a/apps/cf_http/priv/repo/migrations/20200228154815_create_firewall_rules.exs +++ b/apps/cf_http/priv/repo/migrations/20200228154815_create_firewall_rules.exs @@ -7,8 +7,11 @@ defmodule CfHttp.Repo.Migrations.CreateFirewallRules do add :port, :string add :protocol, :string add :enabled, :boolean, default: false, null: false + add :device_id, references(:devices, on_delete: :delete_all) timestamps() end + + create index(:firewall_rules, [:device_id]) end end diff --git a/config/config.exs b/config/config.exs index 4b6613136..54bf9f45b 100644 --- a/config/config.exs +++ b/config/config.exs @@ -27,7 +27,7 @@ config :cf_http, CfHttpWeb.Endpoint, url: [host: "localhost"], secret_key_base: "5OVYJ83AcoQcPmdKNksuBhJFBhjHD1uUa9mDOHV/6EIdBQ6pXksIhkVeWIzFk5SD", render_errors: [view: CfHttpWeb.ErrorView, accepts: ~w(html json)], - pubsub_server: [name: CfHttp.PubSub] + pubsub_server: CfHttp.PubSub # Configures Elixir's Logger config :logger, :console, diff --git a/config/dev.exs b/config/dev.exs index 5a9a44fff..ef9d2c1f0 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -26,7 +26,7 @@ config :cf_http, CfHttpWeb.Endpoint, "--mode", "development", "--watch-stdin", - cd: Path.expand("../assets", __DIR__) + cd: Path.expand("../apps/cf_http/assets", __DIR__) ] ] @@ -58,10 +58,10 @@ config :cf_http, CfHttpWeb.Endpoint, config :cf_http, CfHttpWeb.Endpoint, live_reload: [ patterns: [ - ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$", - ~r"priv/gettext/.*(po)$", - ~r"lib/cf_http_web/(live|views)/.*(ex)$", - ~r"lib/cf_http_web/templates/.*(eex)$" + ~r"apps/cf_http/priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$", + ~r"apps/cf_http/priv/gettext/.*(po)$", + ~r"apps/cf_http/lib/cf_http_web/(live|views)/.*(ex)$", + ~r"apps/cf_http/lib/cf_http_web/templates/.*(eex)$" ] ] diff --git a/mix.lock b/mix.lock index 73733f638..78a04796e 100644 --- a/mix.lock +++ b/mix.lock @@ -4,18 +4,18 @@ "cowboy": {:hex, :cowboy, "2.7.0", "91ed100138a764355f43316b1d23d7ff6bdb0de4ea618cb5d8677c93a7a2f115", [:rebar3], [{:cowlib, "~> 2.8.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "04fd8c6a39edc6aaa9c26123009200fc61f92a3a94f3178c527b70b767c6e605"}, "cowlib": {:hex, :cowlib, "2.8.0", "fd0ff1787db84ac415b8211573e9a30a3ebe71b5cbff7f720089972b2319c8a4", [:rebar3], [], "hexpm", "79f954a7021b302186a950a32869dbc185523d99d3e44ce430cd1f3289f41ed4"}, "credo": {:hex, :credo, "1.4.0", "92339d4cbadd1e88b5ee43d427b639b68a11071b6f73854e33638e30a0ea11f5", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "1fd3b70dce216574ce3c18bdf510b57e7c4c85c2ec9cad4bff854abaf7e58658"}, - "db_connection": {:hex, :db_connection, "2.2.1", "caee17725495f5129cb7faebde001dc4406796f12a62b8949f4ac69315080566", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "2b02ece62d9f983fcd40954e443b7d9e6589664380e5546b2b9b523cd0fb59e1"}, + "db_connection": {:hex, :db_connection, "2.2.2", "3bbca41b199e1598245b716248964926303b5d4609ff065125ce98bcd368939e", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "642af240d8a8affb93b4ba5a6fcd2bbcbdc327e1a524b825d383711536f8070c"}, "decimal": {:hex, :decimal, "1.8.1", "a4ef3f5f3428bdbc0d35374029ffcf4ede8533536fa79896dd450168d9acdf3c", [:mix], [], "hexpm", "3cb154b00225ac687f6cbd4acc4b7960027c757a5152b369923ead9ddbca7aec"}, - "ecto": {:hex, :ecto, "3.4.1", "ca5b5f6314eebd7fa2e52c6d78abb1ef955005dd60cc7a047b963ee23ee14a6c", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "748a317a2eacac0b7b6540cb7d2198b79457ede9cec2b4d1582117f90ac309d5"}, + "ecto": {:hex, :ecto, "3.4.3", "3a14c2500c3964165245a4f24a463e080762f7ccd0c632c763ea589f75ca205f", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "9b6f18dea95f2004d0369f6a8346513ca3f706614f4ede219a5f3fe5db5dd962"}, "ecto_network": {:hex, :ecto_network, "1.3.0", "1e77fa37c20e0f6a426d3862732f3317b0fa4c18f123d325f81752a491d7304e", [:mix], [{:ecto_sql, ">= 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:phoenix_html, ">= 0.0.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.14.0", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm", "053a5e46ef2837e8ea5ea97c82fa0f5494699209eddd764e663c85f11b2865bd"}, - "ecto_sql": {:hex, :ecto_sql, "3.4.2", "3d842665a81ba2137b62aa70151afe81dae44824cd09b2076a255937ab4e2dc9", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0 or ~> 0.4.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.0", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f2b064102467e1525314a464b6fea0707ff28ee132a15006727ccf51b73492ff"}, + "ecto_sql": {:hex, :ecto_sql, "3.4.3", "c552aa8a7ccff2b64024f835503b3155d8e73452c180298527fbdbcd6e79710b", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0 or ~> 0.4.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.0", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ec9e59d6fa3f8cfda9963ada371e9e6659167c2338a997bd7ea23b10b245842b"}, "file_system": {:hex, :file_system, "0.2.8", "f632bd287927a1eed2b718f22af727c5aeaccc9a98d8c2bd7bff709e851dc986", [:mix], [], "hexpm", "97a3b6f8d63ef53bd0113070102db2ce05352ecf0d25390eb8d747c2bde98bca"}, "gettext": {:hex, :gettext, "0.17.4", "f13088e1ec10ce01665cf25f5ff779e7df3f2dc71b37084976cf89d1aa124d5c", [:mix], [], "hexpm", "3c75b5ea8288e2ee7ea503ff9e30dfe4d07ad3c054576a6e60040e79a801e14d"}, - "jason": {:hex, :jason, "1.2.0", "10043418c42d2493d0ee212d3fddd25d7ffe484380afad769a0a38795938e448", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "116747dbe057794c3a3e4e143b7c8390b29f634e16c78a7f59ba75bfa6852e7f"}, + "jason": {:hex, :jason, "1.2.1", "12b22825e22f468c02eb3e4b9985f3d0cb8dc40b9bd704730efa11abd2708c44", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "b659b8571deedf60f79c5a608e15414085fa141344e2716fbd6988a084b5f993"}, "mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm", "6cbe761d6a0ca5a31a0931bf4c63204bceb64538e664a8ecf784a9a6f3b875f1"}, "phoenix": {:hex, :phoenix, "1.5.1", "95156589879dc69201d5fc0ebdbfdfc7901a09a3616ea611ec297f81340275a2", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc272b38e79d2881790fccae6f67a9fbe9b790103d6878175ea03d23003152eb"}, "phoenix_ecto": {:hex, :phoenix_ecto, "4.1.0", "a044d0756d0464c5a541b4a0bf4bcaf89bffcaf92468862408290682c73ae50d", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "c5e666a341ff104d0399d8f0e4ff094559b2fde13a5985d4cb5023b2c2ac558b"}, - "phoenix_html": {:hex, :phoenix_html, "2.14.1", "7dabafadedb552db142aacbd1f11de1c0bbaa247f90c449ca549d5e30bbc66b4", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "536d5200ad37fecfe55b3241d90b7a8c3a2ca60cd012fc065f776324fa9ab0a9"}, + "phoenix_html": {:hex, :phoenix_html, "2.14.2", "b8a3899a72050f3f48a36430da507dd99caf0ac2d06c77529b1646964f3d563e", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "58061c8dfd25da5df1ea0ca47c972f161beb6c875cd293917045b92ffe1bf617"}, "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.2.1", "274a4b07c4adbdd7785d45a8b0bb57634d0b4f45b18d2c508b26c0344bd59b8f", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "41b4103a2fa282cfd747d377233baf213c648fdcc7928f432937676532490eee"}, "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.0.0", "a1ae76717bb168cdeb10ec9d92d1480fec99e3080f011402c0a2d68d47395ffb", [:mix], [], "hexpm", "c52d948c4f261577b9c6fa804be91884b381a7f8f18450c5045975435350f771"}, "plug": {:hex, :plug, "1.10.0", "6508295cbeb4c654860845fb95260737e4a8838d34d115ad76cd487584e2fc4d", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "422a9727e667be1bf5ab1de03be6fa0ad67b775b2d84ed908f3264415ef29d4a"}, From d8ae231da3aaab332aec6db4edd1c6553d966f9e Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Wed, 6 May 2020 00:34:05 -0700 Subject: [PATCH 2/8] Rename files firewall_rule -> rule --- apps/cf_http/lib/cf_http/{firewall_rules.ex => rules.ex} | 0 .../cf_http/{firewall_rules/firewall_rule.ex => rules/rule.ex} | 0 .../{firewall_rule_controller.ex => rule_controller.ex} | 0 .../cf_http_web/templates/{firewall_rule => rule}/edit.html.eex | 0 .../cf_http_web/templates/{firewall_rule => rule}/form.html.eex | 0 .../cf_http_web/templates/{firewall_rule => rule}/index.html.eex | 0 .../cf_http_web/templates/{firewall_rule => rule}/new.html.eex | 0 .../cf_http_web/templates/{firewall_rule => rule}/show.html.eex | 0 .../lib/cf_http_web/views/{firewall_rule_view.ex => rule_view.ex} | 0 ..._create_firewall_rules.exs => 20200228154815_create_rules.exs} | 0 .../test/cf_http/{firewall_rules_test.exs => rules_test.exs} | 0 ...firewall_rule_controller_test.exs => rule_controller_test.exs} | 0 12 files changed, 0 insertions(+), 0 deletions(-) rename apps/cf_http/lib/cf_http/{firewall_rules.ex => rules.ex} (100%) rename apps/cf_http/lib/cf_http/{firewall_rules/firewall_rule.ex => rules/rule.ex} (100%) rename apps/cf_http/lib/cf_http_web/controllers/{firewall_rule_controller.ex => rule_controller.ex} (100%) rename apps/cf_http/lib/cf_http_web/templates/{firewall_rule => rule}/edit.html.eex (100%) rename apps/cf_http/lib/cf_http_web/templates/{firewall_rule => rule}/form.html.eex (100%) rename apps/cf_http/lib/cf_http_web/templates/{firewall_rule => rule}/index.html.eex (100%) rename apps/cf_http/lib/cf_http_web/templates/{firewall_rule => rule}/new.html.eex (100%) rename apps/cf_http/lib/cf_http_web/templates/{firewall_rule => rule}/show.html.eex (100%) rename apps/cf_http/lib/cf_http_web/views/{firewall_rule_view.ex => rule_view.ex} (100%) rename apps/cf_http/priv/repo/migrations/{20200228154815_create_firewall_rules.exs => 20200228154815_create_rules.exs} (100%) rename apps/cf_http/test/cf_http/{firewall_rules_test.exs => rules_test.exs} (100%) rename apps/cf_http/test/cf_http_web/controllers/{firewall_rule_controller_test.exs => rule_controller_test.exs} (100%) diff --git a/apps/cf_http/lib/cf_http/firewall_rules.ex b/apps/cf_http/lib/cf_http/rules.ex similarity index 100% rename from apps/cf_http/lib/cf_http/firewall_rules.ex rename to apps/cf_http/lib/cf_http/rules.ex diff --git a/apps/cf_http/lib/cf_http/firewall_rules/firewall_rule.ex b/apps/cf_http/lib/cf_http/rules/rule.ex similarity index 100% rename from apps/cf_http/lib/cf_http/firewall_rules/firewall_rule.ex rename to apps/cf_http/lib/cf_http/rules/rule.ex diff --git a/apps/cf_http/lib/cf_http_web/controllers/firewall_rule_controller.ex b/apps/cf_http/lib/cf_http_web/controllers/rule_controller.ex similarity index 100% rename from apps/cf_http/lib/cf_http_web/controllers/firewall_rule_controller.ex rename to apps/cf_http/lib/cf_http_web/controllers/rule_controller.ex diff --git a/apps/cf_http/lib/cf_http_web/templates/firewall_rule/edit.html.eex b/apps/cf_http/lib/cf_http_web/templates/rule/edit.html.eex similarity index 100% rename from apps/cf_http/lib/cf_http_web/templates/firewall_rule/edit.html.eex rename to apps/cf_http/lib/cf_http_web/templates/rule/edit.html.eex diff --git a/apps/cf_http/lib/cf_http_web/templates/firewall_rule/form.html.eex b/apps/cf_http/lib/cf_http_web/templates/rule/form.html.eex similarity index 100% rename from apps/cf_http/lib/cf_http_web/templates/firewall_rule/form.html.eex rename to apps/cf_http/lib/cf_http_web/templates/rule/form.html.eex diff --git a/apps/cf_http/lib/cf_http_web/templates/firewall_rule/index.html.eex b/apps/cf_http/lib/cf_http_web/templates/rule/index.html.eex similarity index 100% rename from apps/cf_http/lib/cf_http_web/templates/firewall_rule/index.html.eex rename to apps/cf_http/lib/cf_http_web/templates/rule/index.html.eex diff --git a/apps/cf_http/lib/cf_http_web/templates/firewall_rule/new.html.eex b/apps/cf_http/lib/cf_http_web/templates/rule/new.html.eex similarity index 100% rename from apps/cf_http/lib/cf_http_web/templates/firewall_rule/new.html.eex rename to apps/cf_http/lib/cf_http_web/templates/rule/new.html.eex diff --git a/apps/cf_http/lib/cf_http_web/templates/firewall_rule/show.html.eex b/apps/cf_http/lib/cf_http_web/templates/rule/show.html.eex similarity index 100% rename from apps/cf_http/lib/cf_http_web/templates/firewall_rule/show.html.eex rename to apps/cf_http/lib/cf_http_web/templates/rule/show.html.eex diff --git a/apps/cf_http/lib/cf_http_web/views/firewall_rule_view.ex b/apps/cf_http/lib/cf_http_web/views/rule_view.ex similarity index 100% rename from apps/cf_http/lib/cf_http_web/views/firewall_rule_view.ex rename to apps/cf_http/lib/cf_http_web/views/rule_view.ex diff --git a/apps/cf_http/priv/repo/migrations/20200228154815_create_firewall_rules.exs b/apps/cf_http/priv/repo/migrations/20200228154815_create_rules.exs similarity index 100% rename from apps/cf_http/priv/repo/migrations/20200228154815_create_firewall_rules.exs rename to apps/cf_http/priv/repo/migrations/20200228154815_create_rules.exs diff --git a/apps/cf_http/test/cf_http/firewall_rules_test.exs b/apps/cf_http/test/cf_http/rules_test.exs similarity index 100% rename from apps/cf_http/test/cf_http/firewall_rules_test.exs rename to apps/cf_http/test/cf_http/rules_test.exs diff --git a/apps/cf_http/test/cf_http_web/controllers/firewall_rule_controller_test.exs b/apps/cf_http/test/cf_http_web/controllers/rule_controller_test.exs similarity index 100% rename from apps/cf_http/test/cf_http_web/controllers/firewall_rule_controller_test.exs rename to apps/cf_http/test/cf_http_web/controllers/rule_controller_test.exs From bf7ccbebd185714f1b87f9dfae597d201d47249e Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Wed, 6 May 2020 00:35:49 -0700 Subject: [PATCH 3/8] Rename firewall_rule -> rule --- apps/cf_http/lib/cf_http/devices/device.ex | 2 +- apps/cf_http/lib/cf_http/rules.ex | 78 +++++++++---------- apps/cf_http/lib/cf_http/rules/rule.ex | 8 +- .../controllers/rule_controller.ex | 54 ++++++------- apps/cf_http/lib/cf_http_web/router.ex | 4 +- .../cf_http_web/templates/rule/edit.html.eex | 6 +- .../cf_http_web/templates/rule/index.html.eex | 20 ++--- .../cf_http_web/templates/rule/new.html.eex | 6 +- .../cf_http_web/templates/rule/show.html.eex | 14 ++-- .../lib/cf_http_web/views/rule_view.ex | 2 +- .../20200228154815_create_rules.exs | 6 +- apps/cf_http/test/cf_http/rules_test.exs | 4 +- .../controllers/rule_controller_test.exs | 12 +-- 13 files changed, 108 insertions(+), 108 deletions(-) diff --git a/apps/cf_http/lib/cf_http/devices/device.ex b/apps/cf_http/lib/cf_http/devices/device.ex index 8e4d8e242..b83740905 100644 --- a/apps/cf_http/lib/cf_http/devices/device.ex +++ b/apps/cf_http/lib/cf_http/devices/device.ex @@ -12,7 +12,7 @@ defmodule CfHttp.Devices.Device do field :verified_at, :utc_datetime field :user_id, :id - has_many :firewall_rules, CfHttp.FirewallRules.FirewallRule + has_many :rules, CfHttp.Rules.Rule timestamps() end diff --git a/apps/cf_http/lib/cf_http/rules.ex b/apps/cf_http/lib/cf_http/rules.ex index 7be333dd1..6696f0776 100644 --- a/apps/cf_http/lib/cf_http/rules.ex +++ b/apps/cf_http/lib/cf_http/rules.ex @@ -1,104 +1,104 @@ -defmodule CfHttp.FirewallRules do +defmodule CfHttp.Rules do @moduledoc """ - The FirewallRules context. + The Rules context. """ import Ecto.Query, warn: false alias CfHttp.Repo - alias CfHttp.FirewallRules.FirewallRule + alias CfHttp.Rules.Rule @doc """ - Returns the list of firewall_rules. + Returns the list of rules. ## Examples - iex> list_firewall_rules() - [%FirewallRule{}, ...] + iex> list_rules() + [%Rule{}, ...] """ - def list_firewall_rules do - Repo.all(FirewallRule) + def list_rules do + Repo.all(Rule) end @doc """ - Gets a single firewall_rule. + Gets a single rule. - Raises `Ecto.NoResultsError` if the Firewall rule does not exist. + Raises `Ecto.NoResultsError` if the Rule does not exist. ## Examples - iex> get_firewall_rule!(123) - %FirewallRule{} + iex> get_rule!(123) + %Rule{} - iex> get_firewall_rule!(456) + iex> get_rule!(456) ** (Ecto.NoResultsError) """ - def get_firewall_rule!(id), do: Repo.get!(FirewallRule, id) + def get_rule!(id), do: Repo.get!(Rule, id) @doc """ - Creates a firewall_rule. + Creates a rule. ## Examples - iex> create_firewall_rule(%{field: value}) - {:ok, %FirewallRule{}} + iex> create_rule(%{field: value}) + {:ok, %Rule{}} - iex> create_firewall_rule(%{field: bad_value}) + iex> create_rule(%{field: bad_value}) {:error, %Ecto.Changeset{}} """ - def create_firewall_rule(attrs \\ %{}) do - %FirewallRule{} - |> FirewallRule.changeset(attrs) + def create_rule(attrs \\ %{}) do + %Rule{} + |> Rule.changeset(attrs) |> Repo.insert() end @doc """ - Updates a firewall_rule. + Updates a rule. ## Examples - iex> update_firewall_rule(firewall_rule, %{field: new_value}) - {:ok, %FirewallRule{}} + iex> update_rule(rule, %{field: new_value}) + {:ok, %Rule{}} - iex> update_firewall_rule(firewall_rule, %{field: bad_value}) + iex> update_rule(rule, %{field: bad_value}) {:error, %Ecto.Changeset{}} """ - def update_firewall_rule(%FirewallRule{} = firewall_rule, attrs) do - firewall_rule - |> FirewallRule.changeset(attrs) + def update_rule(%Rule{} = rule, attrs) do + rule + |> Rule.changeset(attrs) |> Repo.update() end @doc """ - Deletes a firewall_rule. + Deletes a rule. ## Examples - iex> delete_firewall_rule(firewall_rule) - {:ok, %FirewallRule{}} + iex> delete_rule(rule) + {:ok, %Rule{}} - iex> delete_firewall_rule(firewall_rule) + iex> delete_rule(rule) {:error, %Ecto.Changeset{}} """ - def delete_firewall_rule(%FirewallRule{} = firewall_rule) do - Repo.delete(firewall_rule) + def delete_rule(%Rule{} = rule) do + Repo.delete(rule) end @doc """ - Returns an `%Ecto.Changeset{}` for tracking firewall_rule changes. + Returns an `%Ecto.Changeset{}` for tracking rule changes. ## Examples - iex> change_firewall_rule(firewall_rule) - %Ecto.Changeset{source: %FirewallRule{}} + iex> change_rule(rule) + %Ecto.Changeset{source: %Rule{}} """ - def change_firewall_rule(%FirewallRule{} = firewall_rule) do - FirewallRule.changeset(firewall_rule, %{}) + def change_rule(%Rule{} = rule) do + Rule.changeset(rule, %{}) end end diff --git a/apps/cf_http/lib/cf_http/rules/rule.ex b/apps/cf_http/lib/cf_http/rules/rule.ex index b134879ba..254512913 100644 --- a/apps/cf_http/lib/cf_http/rules/rule.ex +++ b/apps/cf_http/lib/cf_http/rules/rule.ex @@ -1,4 +1,4 @@ -defmodule CfHttp.FirewallRules.FirewallRule do +defmodule CfHttp.Rules.Rule do @moduledoc """ Not really sure what to write here. I'll update this later. """ @@ -6,7 +6,7 @@ defmodule CfHttp.FirewallRules.FirewallRule do use Ecto.Schema import Ecto.Changeset - schema "firewall_rules" do + schema "rules" do field :destination, :string field :enabled, :boolean, default: false field :port, :string @@ -18,8 +18,8 @@ defmodule CfHttp.FirewallRules.FirewallRule do end @doc false - def changeset(firewall_rule, attrs) do - firewall_rule + def changeset(rule, attrs) do + rule |> cast(attrs, [:destination, :port, :protocol, :enabled]) |> validate_required([:destination, :port, :protocol, :enabled]) end diff --git a/apps/cf_http/lib/cf_http_web/controllers/rule_controller.ex b/apps/cf_http/lib/cf_http_web/controllers/rule_controller.ex index 17884adae..8838ebf0e 100644 --- a/apps/cf_http/lib/cf_http_web/controllers/rule_controller.ex +++ b/apps/cf_http/lib/cf_http_web/controllers/rule_controller.ex @@ -1,35 +1,35 @@ -defmodule CfHttpWeb.FirewallRuleController do +defmodule CfHttpWeb.RuleController do @moduledoc """ - Controller logic for FirewallRules + Controller logic for Rules """ use CfHttpWeb, :controller alias CfHttp.Devices - alias CfHttp.FirewallRules - alias CfHttp.FirewallRules.FirewallRule + alias CfHttp.Rules + alias CfHttp.Rules.Rule plug CfHttpWeb.Plugs.Authenticator def index(conn, %{"device_id" => device_id}) do device = Devices.get_device!(device_id) - render(conn, "index.html", device: device, firewall_rules: device.firewall_rules) + render(conn, "index.html", device: device, rules: device.rules) end def new(conn, %{"device_id" => device_id}) do device = Devices.get_device!(device_id) - changeset = FirewallRules.change_firewall_rule(%FirewallRule{device_id: device_id}) + changeset = Rules.change_rule(%Rule{device_id: device_id}) render(conn, "new.html", changeset: changeset, device: device) end - def create(conn, %{"firewall_rule" => firewall_rule_params}) do - case FirewallRules.create_firewall_rule(firewall_rule_params) do - {:ok, firewall_rule} -> + def create(conn, %{"rule" => rule_params}) do + case Rules.create_rule(rule_params) do + {:ok, rule} -> conn - |> put_flash(:info, "Firewall rule created successfully.") - |> redirect(to: Routes.firewall_rule_path(conn, :show, firewall_rule)) + |> put_flash(:info, "Rule created successfully.") + |> redirect(to: Routes.rule_path(conn, :show, rule)) {:error, %Ecto.Changeset{} = changeset} -> render(conn, "new.html", changeset: changeset) @@ -37,36 +37,36 @@ defmodule CfHttpWeb.FirewallRuleController do end def show(conn, %{"id" => id}) do - firewall_rule = FirewallRules.get_firewall_rule!(id) - render(conn, "show.html", firewall_rule: firewall_rule) + rule = Rules.get_rule!(id) + render(conn, "show.html", rule: rule) end def edit(conn, %{"id" => id}) do - firewall_rule = FirewallRules.get_firewall_rule!(id) - changeset = FirewallRules.change_firewall_rule(firewall_rule) - render(conn, "edit.html", firewall_rule: firewall_rule, changeset: changeset) + rule = Rules.get_rule!(id) + changeset = Rules.change_rule(rule) + render(conn, "edit.html", rule: rule, changeset: changeset) end - def update(conn, %{"id" => id, "firewall_rule" => firewall_rule_params}) do - firewall_rule = FirewallRules.get_firewall_rule!(id) + def update(conn, %{"id" => id, "rule" => rule_params}) do + rule = Rules.get_rule!(id) - case FirewallRules.update_firewall_rule(firewall_rule, firewall_rule_params) do - {:ok, firewall_rule} -> + case Rules.update_rule(rule, rule_params) do + {:ok, rule} -> conn - |> put_flash(:info, "Firewall rule updated successfully.") - |> redirect(to: Routes.firewall_rule_path(conn, :show, firewall_rule)) + |> put_flash(:info, "Rule updated successfully.") + |> redirect(to: Routes.rule_path(conn, :show, rule)) {:error, %Ecto.Changeset{} = changeset} -> - render(conn, "edit.html", firewall_rule: firewall_rule, changeset: changeset) + render(conn, "edit.html", rule: rule, changeset: changeset) end end def delete(conn, %{"id" => id}) do - firewall_rule = FirewallRules.get_firewall_rule!(id) - {:ok, _firewall_rule} = FirewallRules.delete_firewall_rule(firewall_rule) + rule = Rules.get_rule!(id) + {:ok, _rule} = Rules.delete_rule(rule) conn - |> put_flash(:info, "Firewall rule deleted successfully.") - |> redirect(to: Routes.firewall_rule_path(conn, :index, firewall_rule.device)) + |> put_flash(:info, "Rule deleted successfully.") + |> redirect(to: Routes.rule_path(conn, :index, rule.device)) end end diff --git a/apps/cf_http/lib/cf_http_web/router.ex b/apps/cf_http/lib/cf_http_web/router.ex index 84a8d6f22..9239afc28 100644 --- a/apps/cf_http/lib/cf_http_web/router.ex +++ b/apps/cf_http/lib/cf_http_web/router.ex @@ -22,10 +22,10 @@ defmodule CfHttpWeb.Router do resources "/users", UserController, only: [:new, :create] resources "/devices", DeviceController, except: [:create] do - resources "/firewall_rules", FirewallRuleController, only: [:new, :index, :create] + resources "/rules", RuleController, only: [:new, :index, :create] end - resources "/firewall_rules", FirewallRuleController, only: [:show, :update, :delete, :edit] + resources "/rules", RuleController, only: [:show, :update, :delete, :edit] end # Other scopes may use custom stacks. diff --git a/apps/cf_http/lib/cf_http_web/templates/rule/edit.html.eex b/apps/cf_http/lib/cf_http_web/templates/rule/edit.html.eex index b2b99434e..e49613818 100644 --- a/apps/cf_http/lib/cf_http_web/templates/rule/edit.html.eex +++ b/apps/cf_http/lib/cf_http_web/templates/rule/edit.html.eex @@ -1,5 +1,5 @@ -

Edit Firewall rule

+

Edit Rule

-<%= render "form.html", Map.put(assigns, :action, Routes.firewall_rule_path(@conn, :update, @firewall_rule)) %> +<%= render "form.html", Map.put(assigns, :action, Routes.rule_path(@conn, :update, @rule)) %> -<%= link "Back", to: Routes.firewall_rule_path(@conn, :index, @firewall_rule.device) %> +<%= link "Back", to: Routes.rule_path(@conn, :index, @rule.device) %> diff --git a/apps/cf_http/lib/cf_http_web/templates/rule/index.html.eex b/apps/cf_http/lib/cf_http_web/templates/rule/index.html.eex index b2b0a0849..0c49c8fa4 100644 --- a/apps/cf_http/lib/cf_http_web/templates/rule/index.html.eex +++ b/apps/cf_http/lib/cf_http_web/templates/rule/index.html.eex @@ -1,4 +1,4 @@ -

Listing Firewall rules

+

Listing Rules

@@ -12,21 +12,21 @@ -<%= for firewall_rule <- @firewall_rules do %> +<%= for rule <- @rules do %> - - - - + + + + <% end %>
<%= firewall_rule.destination %><%= firewall_rule.port %><%= firewall_rule.protocol %><%= firewall_rule.enabled %><%= rule.destination %><%= rule.port %><%= rule.protocol %><%= rule.enabled %> - <%= link "Show", to: Routes.firewall_rule_path(@conn, :show, firewall_rule) %> - <%= link "Edit", to: Routes.firewall_rule_path(@conn, :edit, firewall_rule) %> - <%= link "Delete", to: Routes.firewall_rule_path(@conn, :delete, firewall_rule), method: :delete, data: [confirm: "Are you sure?"] %> + <%= link "Show", to: Routes.rule_path(@conn, :show, rule) %> + <%= link "Edit", to: Routes.rule_path(@conn, :edit, rule) %> + <%= link "Delete", to: Routes.rule_path(@conn, :delete, rule), method: :delete, data: [confirm: "Are you sure?"] %>
-<%= link "New Firewall rule", to: Routes.firewall_rule_path(@conn, :new, @device) %> +<%= link "New Rule", to: Routes.rule_path(@conn, :new, @device) %> diff --git a/apps/cf_http/lib/cf_http_web/templates/rule/new.html.eex b/apps/cf_http/lib/cf_http_web/templates/rule/new.html.eex index ca0b2c400..5c88667cd 100644 --- a/apps/cf_http/lib/cf_http_web/templates/rule/new.html.eex +++ b/apps/cf_http/lib/cf_http_web/templates/rule/new.html.eex @@ -1,5 +1,5 @@ -

New Firewall rule

+

New Rule

-<%= render "form.html", Map.put(assigns, :action, Routes.firewall_rule_path(@conn, :create, @device)) %> +<%= render "form.html", Map.put(assigns, :action, Routes.rule_path(@conn, :create, @device)) %> -<%= link "Back", to: Routes.firewall_rule_path(@conn, :index, @device) %> +<%= link "Back", to: Routes.rule_path(@conn, :index, @device) %> diff --git a/apps/cf_http/lib/cf_http_web/templates/rule/show.html.eex b/apps/cf_http/lib/cf_http_web/templates/rule/show.html.eex index d32be268d..6c785c6bb 100644 --- a/apps/cf_http/lib/cf_http_web/templates/rule/show.html.eex +++ b/apps/cf_http/lib/cf_http_web/templates/rule/show.html.eex @@ -1,28 +1,28 @@ -

Show Firewall rule

+

Show Rule

  • Destination: - <%= @firewall_rule.destination %> + <%= @rule.destination %>
  • Port: - <%= @firewall_rule.port %> + <%= @rule.port %>
  • Protocol: - <%= @firewall_rule.protocol %> + <%= @rule.protocol %>
  • Enabled: - <%= @firewall_rule.enabled %> + <%= @rule.enabled %>
-<%= link "Edit", to: Routes.firewall_rule_path(@conn, :edit, @firewall_rule) %> -<%= link "Back", to: Routes.firewall_rule_path(@conn, :index, @firewall_rule.device) %> +<%= link "Edit", to: Routes.rule_path(@conn, :edit, @rule) %> +<%= link "Back", to: Routes.rule_path(@conn, :index, @rule.device) %> diff --git a/apps/cf_http/lib/cf_http_web/views/rule_view.ex b/apps/cf_http/lib/cf_http_web/views/rule_view.ex index e873be9b3..d354a584f 100644 --- a/apps/cf_http/lib/cf_http_web/views/rule_view.ex +++ b/apps/cf_http/lib/cf_http_web/views/rule_view.ex @@ -1,3 +1,3 @@ -defmodule CfHttpWeb.FirewallRuleView do +defmodule CfHttpWeb.RuleView do use CfHttpWeb, :view end diff --git a/apps/cf_http/priv/repo/migrations/20200228154815_create_rules.exs b/apps/cf_http/priv/repo/migrations/20200228154815_create_rules.exs index 40a2456d2..a39328eb2 100644 --- a/apps/cf_http/priv/repo/migrations/20200228154815_create_rules.exs +++ b/apps/cf_http/priv/repo/migrations/20200228154815_create_rules.exs @@ -1,8 +1,8 @@ -defmodule CfHttp.Repo.Migrations.CreateFirewallRules do +defmodule CfHttp.Repo.Migrations.CreateRules do use Ecto.Migration def change do - create table(:firewall_rules) do + create table(:rules) do add :destination, :inet add :port, :string add :protocol, :string @@ -12,6 +12,6 @@ defmodule CfHttp.Repo.Migrations.CreateFirewallRules do timestamps() end - create index(:firewall_rules, [:device_id]) + create index(:rules, [:device_id]) end end diff --git a/apps/cf_http/test/cf_http/rules_test.exs b/apps/cf_http/test/cf_http/rules_test.exs index 0101d51c6..7222a43cc 100644 --- a/apps/cf_http/test/cf_http/rules_test.exs +++ b/apps/cf_http/test/cf_http/rules_test.exs @@ -1,6 +1,6 @@ -defmodule CfHttp.FirewallRulesTest do +defmodule CfHttp.RulesTest do use CfHttp.DataCase - describe "firewall_rules" do + describe "rules" do end end diff --git a/apps/cf_http/test/cf_http_web/controllers/rule_controller_test.exs b/apps/cf_http/test/cf_http_web/controllers/rule_controller_test.exs index fc836c691..5c36eb39a 100644 --- a/apps/cf_http/test/cf_http_web/controllers/rule_controller_test.exs +++ b/apps/cf_http/test/cf_http_web/controllers/rule_controller_test.exs @@ -1,21 +1,21 @@ -defmodule CfHttpWeb.FirewallRuleControllerTest do +defmodule CfHttpWeb.RuleControllerTest do use CfHttpWeb.ConnCase describe "index" do end - describe "new firewall_rule" do + describe "new rule" do end - describe "create firewall_rule" do + describe "create rule" do end - describe "edit firewall_rule" do + describe "edit rule" do end - describe "update firewall_rule" do + describe "update rule" do end - describe "delete firewall_rule" do + describe "delete rule" do end end From 8c67fb4a4676f121af9024ca51128a07b2dc9342 Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Thu, 7 May 2020 09:09:15 -0700 Subject: [PATCH 4/8] add tachyons.io --- apps/cf_http/assets/css/app.css | 2 +- apps/cf_http/assets/css/main.css | 0 apps/cf_http/assets/css/phoenix.css | 135 ------------------ apps/cf_http/assets/static/images/logo.svg | 7 - .../templates/device/form.html.eex | 2 +- .../cf_http_web/templates/layout/app.html.eex | 13 +- .../cf_http_web/templates/rule/form.html.eex | 2 +- mix.exs | 4 +- 8 files changed, 11 insertions(+), 154 deletions(-) create mode 100644 apps/cf_http/assets/css/main.css delete mode 100644 apps/cf_http/assets/css/phoenix.css delete mode 100644 apps/cf_http/assets/static/images/logo.svg diff --git a/apps/cf_http/assets/css/app.css b/apps/cf_http/assets/css/app.css index fec0b3fc8..9313f4354 100644 --- a/apps/cf_http/assets/css/app.css +++ b/apps/cf_http/assets/css/app.css @@ -1,3 +1,3 @@ /* This file is for your main application css. */ -@import "./phoenix.css"; +@import "./main.css"; diff --git a/apps/cf_http/assets/css/main.css b/apps/cf_http/assets/css/main.css new file mode 100644 index 000000000..e69de29bb diff --git a/apps/cf_http/assets/css/phoenix.css b/apps/cf_http/assets/css/phoenix.css deleted file mode 100644 index 8d1d79c27..000000000 --- a/apps/cf_http/assets/css/phoenix.css +++ /dev/null @@ -1,135 +0,0 @@ -/* Includes some default style for the starter application. - * This can be safely deleted to start fresh. - */ - -/* Milligram v1.3.0 https://milligram.github.io - * Copyright (c) 2017 CJ Patoilo Licensed under the MIT license - */ - -*,*:after,*:before{box-sizing:inherit}html{box-sizing:border-box;font-size:62.5%}body{color:#000000;font-family:'Helvetica', 'Arial', sans-serif;font-size:1.6em;font-weight:300;line-height:1.6}blockquote{border-left:0.3rem solid #d1d1d1;margin-left:0;margin-right:0;padding:1rem 1.5rem}blockquote *:last-child{margin-bottom:0}.button,button,input[type='button'],input[type='reset'],input[type='submit']{background-color:#0069d9;border:0.1rem solid #0069d9;border-radius:.4rem;color:#fff;cursor:pointer;display:inline-block;font-size:1.1rem;font-weight:700;height:3.8rem;letter-spacing:.1rem;line-height:3.8rem;padding:0 3.0rem;text-align:center;text-decoration:none;text-transform:uppercase;white-space:nowrap}.button:focus,.button:hover,button:focus,button:hover,input[type='button']:focus,input[type='button']:hover,input[type='reset']:focus,input[type='reset']:hover,input[type='submit']:focus,input[type='submit']:hover{background-color:#606c76;border-color:#606c76;color:#fff;outline:0}.button[disabled],button[disabled],input[type='button'][disabled],input[type='reset'][disabled],input[type='submit'][disabled]{cursor:default;opacity:.5}.button[disabled]:focus,.button[disabled]:hover,button[disabled]:focus,button[disabled]:hover,input[type='button'][disabled]:focus,input[type='button'][disabled]:hover,input[type='reset'][disabled]:focus,input[type='reset'][disabled]:hover,input[type='submit'][disabled]:focus,input[type='submit'][disabled]:hover{background-color:#0069d9;border-color:#0069d9}.button.button-outline,button.button-outline,input[type='button'].button-outline,input[type='reset'].button-outline,input[type='submit'].button-outline{background-color:transparent;color:#0069d9}.button.button-outline:focus,.button.button-outline:hover,button.button-outline:focus,button.button-outline:hover,input[type='button'].button-outline:focus,input[type='button'].button-outline:hover,input[type='reset'].button-outline:focus,input[type='reset'].button-outline:hover,input[type='submit'].button-outline:focus,input[type='submit'].button-outline:hover{background-color:transparent;border-color:#606c76;color:#606c76}.button.button-outline[disabled]:focus,.button.button-outline[disabled]:hover,button.button-outline[disabled]:focus,button.button-outline[disabled]:hover,input[type='button'].button-outline[disabled]:focus,input[type='button'].button-outline[disabled]:hover,input[type='reset'].button-outline[disabled]:focus,input[type='reset'].button-outline[disabled]:hover,input[type='submit'].button-outline[disabled]:focus,input[type='submit'].button-outline[disabled]:hover{border-color:inherit;color:#0069d9}.button.button-clear,button.button-clear,input[type='button'].button-clear,input[type='reset'].button-clear,input[type='submit'].button-clear{background-color:transparent;border-color:transparent;color:#0069d9}.button.button-clear:focus,.button.button-clear:hover,button.button-clear:focus,button.button-clear:hover,input[type='button'].button-clear:focus,input[type='button'].button-clear:hover,input[type='reset'].button-clear:focus,input[type='reset'].button-clear:hover,input[type='submit'].button-clear:focus,input[type='submit'].button-clear:hover{background-color:transparent;border-color:transparent;color:#606c76}.button.button-clear[disabled]:focus,.button.button-clear[disabled]:hover,button.button-clear[disabled]:focus,button.button-clear[disabled]:hover,input[type='button'].button-clear[disabled]:focus,input[type='button'].button-clear[disabled]:hover,input[type='reset'].button-clear[disabled]:focus,input[type='reset'].button-clear[disabled]:hover,input[type='submit'].button-clear[disabled]:focus,input[type='submit'].button-clear[disabled]:hover{color:#0069d9}code{background:#f4f5f6;border-radius:.4rem;font-size:86%;margin:0 .2rem;padding:.2rem .5rem;white-space:nowrap}pre{background:#f4f5f6;border-left:0.3rem solid #0069d9;overflow-y:hidden}pre>code{border-radius:0;display:block;padding:1rem 1.5rem;white-space:pre}hr{border:0;border-top:0.1rem solid #f4f5f6;margin:3.0rem 0}input[type='email'],input[type='number'],input[type='password'],input[type='search'],input[type='tel'],input[type='text'],input[type='url'],textarea,select{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:transparent;border:0.1rem solid #d1d1d1;border-radius:.4rem;box-shadow:none;box-sizing:inherit;height:3.8rem;padding:.6rem 1.0rem;width:100%}input[type='email']:focus,input[type='number']:focus,input[type='password']:focus,input[type='search']:focus,input[type='tel']:focus,input[type='text']:focus,input[type='url']:focus,textarea:focus,select:focus{border-color:#0069d9;outline:0}select{background:url('data:image/svg+xml;utf8,') center right no-repeat;padding-right:3.0rem}select:focus{background-image:url('data:image/svg+xml;utf8,')}textarea{min-height:6.5rem}label,legend{display:block;font-size:1.6rem;font-weight:700;margin-bottom:.5rem}fieldset{border-width:0;padding:0}input[type='checkbox'],input[type='radio']{display:inline}.label-inline{display:inline-block;font-weight:normal;margin-left:.5rem}.row{display:flex;flex-direction:column;padding:0;width:100%}.row.row-no-padding{padding:0}.row.row-no-padding>.column{padding:0}.row.row-wrap{flex-wrap:wrap}.row.row-top{align-items:flex-start}.row.row-bottom{align-items:flex-end}.row.row-center{align-items:center}.row.row-stretch{align-items:stretch}.row.row-baseline{align-items:baseline}.row .column{display:block;flex:1 1 auto;margin-left:0;max-width:100%;width:100%}.row .column.column-offset-10{margin-left:10%}.row .column.column-offset-20{margin-left:20%}.row .column.column-offset-25{margin-left:25%}.row .column.column-offset-33,.row .column.column-offset-34{margin-left:33.3333%}.row .column.column-offset-50{margin-left:50%}.row .column.column-offset-66,.row .column.column-offset-67{margin-left:66.6666%}.row .column.column-offset-75{margin-left:75%}.row .column.column-offset-80{margin-left:80%}.row .column.column-offset-90{margin-left:90%}.row .column.column-10{flex:0 0 10%;max-width:10%}.row .column.column-20{flex:0 0 20%;max-width:20%}.row .column.column-25{flex:0 0 25%;max-width:25%}.row .column.column-33,.row .column.column-34{flex:0 0 33.3333%;max-width:33.3333%}.row .column.column-40{flex:0 0 40%;max-width:40%}.row .column.column-50{flex:0 0 50%;max-width:50%}.row .column.column-60{flex:0 0 60%;max-width:60%}.row .column.column-66,.row .column.column-67{flex:0 0 66.6666%;max-width:66.6666%}.row .column.column-75{flex:0 0 75%;max-width:75%}.row .column.column-80{flex:0 0 80%;max-width:80%}.row .column.column-90{flex:0 0 90%;max-width:90%}.row .column .column-top{align-self:flex-start}.row .column .column-bottom{align-self:flex-end}.row .column .column-center{-ms-grid-row-align:center;align-self:center}@media (min-width: 40rem){.row{flex-direction:row;margin-left:-1.0rem;width:calc(100% + 2.0rem)}.row .column{margin-bottom:inherit;padding:0 1.0rem}}a{color:#0069d9;text-decoration:none}a:focus,a:hover{color:#606c76}dl,ol,ul{list-style:none;margin-top:0;padding-left:0}dl dl,dl ol,dl ul,ol dl,ol ol,ol ul,ul dl,ul ol,ul ul{font-size:90%;margin:1.5rem 0 1.5rem 3.0rem}ol{list-style:decimal inside}ul{list-style:circle inside}.button,button,dd,dt,li{margin-bottom:1.0rem}fieldset,input,select,textarea{margin-bottom:1.5rem}blockquote,dl,figure,form,ol,p,pre,table,ul{margin-bottom:2.5rem}table{border-spacing:0;width:100%}td,th{border-bottom:0.1rem solid #e1e1e1;padding:1.2rem 1.5rem;text-align:left}td:first-child,th:first-child{padding-left:0}td:last-child,th:last-child{padding-right:0}b,strong{font-weight:bold}p{margin-top:0}h1,h2,h3,h4,h5,h6{font-weight:300;letter-spacing:-.1rem;margin-bottom:2.0rem;margin-top:0}h1{font-size:4.6rem;line-height:1.2}h2{font-size:3.6rem;line-height:1.25}h3{font-size:2.8rem;line-height:1.3}h4{font-size:2.2rem;letter-spacing:-.08rem;line-height:1.35}h5{font-size:1.8rem;letter-spacing:-.05rem;line-height:1.5}h6{font-size:1.6rem;letter-spacing:0;line-height:1.4}img{max-width:100%}.clearfix:after{clear:both;content:' ';display:table}.float-left{float:left}.float-right{float:right} - -/* General style */ -h1{font-size: 3.6rem; line-height: 1.25} -h2{font-size: 2.8rem; line-height: 1.3} -h3{font-size: 2.2rem; letter-spacing: -.08rem; line-height: 1.35} -h4{font-size: 1.8rem; letter-spacing: -.05rem; line-height: 1.5} -h5{font-size: 1.6rem; letter-spacing: 0; line-height: 1.4} -h6{font-size: 1.4rem; letter-spacing: 0; line-height: 1.2} - -.container{ - margin: 0 auto; - max-width: 80.0rem; - padding: 0 2.0rem; - position: relative; - width: 100% -} -select { - width: auto; -} - -/* Alerts and form errors */ -.alert { - padding: 15px; - margin-bottom: 20px; - border: 1px solid transparent; - border-radius: 4px; -} -.alert-info { - color: #31708f; - background-color: #d9edf7; - border-color: #bce8f1; -} -.alert-warning { - color: #8a6d3b; - background-color: #fcf8e3; - border-color: #faebcc; -} -.alert-danger { - color: #a94442; - background-color: #f2dede; - border-color: #ebccd1; -} -.alert p { - margin-bottom: 0; -} -.alert:empty { - display: none; -} -.help-block { - color: #a94442; - display: block; - margin: -1rem 0 2rem; -} - -/* FireGuard promo and logo */ -.cf-hero { - text-align: center; - border-bottom: 1px solid #e3e3e3; - background: #eee; - border-radius: 6px; - padding: 3em; - margin-bottom: 3rem; - font-weight: 200; - font-size: 120%; -} -.cf-hero p { - margin: 0; -} -.cf-logo { - min-width: 300px; - margin: 1rem; - display: block; -} - -.cf-logo img { - width: auto; - display: block; -} - -/* Headers */ -header { - width: 100%; - background: #fdfdfd; - border-bottom: 1px solid #eaeaea; - margin-bottom: 2rem; -} -header section { - align-items: center; - display: flex; - flex-direction: column; - justify-content: space-between; -} -header section :first-child { - order: 2; -} -header section :last-child { - order: 1; -} -header nav ul, -header nav li { - margin: 0; - padding: 0; - display: block; - text-align: right; - white-space: nowrap; -} -header nav ul { - margin: 1rem; - margin-top: 0; -} -header nav a { - display: block; -} - -@media (min-width: 40.0rem) { /* Small devices (landscape phones, 576px and up) */ - header section { - flex-direction: row; - } - header nav ul { - margin: 1rem; - } - .cf-logo { - flex-basis: 527px; - margin: 0 1rem; - } -} diff --git a/apps/cf_http/assets/static/images/logo.svg b/apps/cf_http/assets/static/images/logo.svg deleted file mode 100644 index 73ba08fad..000000000 --- a/apps/cf_http/assets/static/images/logo.svg +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/apps/cf_http/lib/cf_http_web/templates/device/form.html.eex b/apps/cf_http/lib/cf_http_web/templates/device/form.html.eex index e43e401a1..2cd1a0abe 100644 --- a/apps/cf_http/lib/cf_http_web/templates/device/form.html.eex +++ b/apps/cf_http/lib/cf_http_web/templates/device/form.html.eex @@ -1,6 +1,6 @@ <%= form_for @changeset, @action, fn f -> %> <%= if @changeset.action do %> -
+

Oops, something went wrong! Please check the errors below.

<% end %> diff --git a/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex b/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex index 8a78b406d..fa586e55a 100644 --- a/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex +++ b/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex @@ -6,24 +6,25 @@ FireGuard "/> + <%= csrf_meta_tag() %>
-
+
- + FireGuard
-
- - +
+

<%= get_flash(@conn, :info) %>

+

<%= get_flash(@conn, :error) %>

<%= @inner_content %>
diff --git a/apps/cf_http/lib/cf_http_web/templates/rule/form.html.eex b/apps/cf_http/lib/cf_http_web/templates/rule/form.html.eex index 23e5eb5df..8a4461fbf 100644 --- a/apps/cf_http/lib/cf_http_web/templates/rule/form.html.eex +++ b/apps/cf_http/lib/cf_http_web/templates/rule/form.html.eex @@ -1,6 +1,6 @@ <%= form_for @changeset, @action, fn f -> %> <%= if @changeset.action do %> -
+

Oops, something went wrong! Please check the errors below.

<% end %> diff --git a/mix.exs b/mix.exs index 98ed83f66..93934f430 100644 --- a/mix.exs +++ b/mix.exs @@ -32,8 +32,6 @@ defmodule FireguardUmbrella.MixProject do # # Run "mix help deps" for examples and options. defp deps do - [ - {:credo, "~> 1.4", only: [:dev, :test], runtime: false} - ] + [] end end From 8b66cd2f6b98adc42539316ea10d7f6a35216904 Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Sun, 10 May 2020 11:17:37 -0500 Subject: [PATCH 5/8] tachyons first steps --- .../cf_http_web/templates/layout/app.html.eex | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex b/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex index fa586e55a..5a5c91f47 100644 --- a/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex +++ b/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex @@ -10,23 +10,15 @@ <%= csrf_meta_tag() %> -
-
- - - FireGuard - -
+
+ +
-
-

<%= get_flash(@conn, :info) %>

-

<%= get_flash(@conn, :error) %>

- <%= @inner_content %> -
+
From cc287a76818460f6953f7a46e2d42967c4b854ea Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Sun, 10 May 2020 11:46:13 -0500 Subject: [PATCH 6/8] Add sessions --- apps/cf_http/lib/cf_http/sessions.ex | 105 +++++++++++++++++ apps/cf_http/lib/cf_http/sessions/session.ex | 21 ++++ apps/cf_http/lib/cf_http/users.ex | 106 ++++++++++++++++++ apps/cf_http/lib/cf_http/{ => users}/user.ex | 2 +- .../templates/session/new.html.eex | 23 ++++ .../20200510162435_create_sessions.exs | 13 +++ 6 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 apps/cf_http/lib/cf_http/sessions.ex create mode 100644 apps/cf_http/lib/cf_http/sessions/session.ex create mode 100644 apps/cf_http/lib/cf_http/users.ex rename apps/cf_http/lib/cf_http/{ => users}/user.ex (94%) create mode 100644 apps/cf_http/lib/cf_http_web/templates/session/new.html.eex create mode 100644 apps/cf_http/priv/repo/migrations/20200510162435_create_sessions.exs diff --git a/apps/cf_http/lib/cf_http/sessions.ex b/apps/cf_http/lib/cf_http/sessions.ex new file mode 100644 index 000000000..ae2e9ce8e --- /dev/null +++ b/apps/cf_http/lib/cf_http/sessions.ex @@ -0,0 +1,105 @@ +defmodule CfHttp.Sessions do + @moduledoc """ + The Sessions context. + """ + + import Ecto.Query, warn: false + alias CfHttp.Repo + + alias CfHttp.Sessions.Session + + @doc """ + Returns the list of sessions. + + ## Examples + + iex> list_sessions() + [%Session{}, ...] + + """ + def list_sessions do + Repo.all(Session) + end + + @doc """ + Gets a single session. + + Raises `Ecto.NoResultsError` if the Session does not exist. + + ## Examples + + iex> get_session!(123) + %Session{} + + iex> get_session!(456) + ** (Ecto.NoResultsError) + + """ + def get_session!(id), do: Repo.get!(Session, id) + + @doc """ + Creates a session. + + ## Examples + + iex> create_session(%{field: value}) + {:ok, %Session{}} + + iex> create_session(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_session(attrs \\ %{}) do + %Session{} + |> Session.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a session. + + ## Examples + + iex> update_session(session, %{field: new_value}) + {:ok, %Session{}} + + iex> update_session(session, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_session(%Session{} = session, attrs) do + session + |> Session.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a session. + + ## Examples + + iex> delete_session(session) + {:ok, %Session{}} + + iex> delete_session(session) + {:error, %Ecto.Changeset{}} + + """ + def delete_session(%Session{} = session) do + Repo.delete(session) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking session changes. + + ## Examples + + iex> change_session(session) + %Ecto.Changeset{source: %Session{}} + + """ + def change_session(%Session{} = session) do + Session.changeset(session, %{}) + end +end + diff --git a/apps/cf_http/lib/cf_http/sessions/session.ex b/apps/cf_http/lib/cf_http/sessions/session.ex new file mode 100644 index 000000000..30b1a7842 --- /dev/null +++ b/apps/cf_http/lib/cf_http/sessions/session.ex @@ -0,0 +1,21 @@ +defmodule CfHttp.Sessions.Session do + @moduledoc """ + Represents a Session + """ + + use Ecto.Schema + import Ecto.Changeset + + schema "sessions" do + field :user_id, :id + + timestamps() + end + + @doc false + def changeset(session, attrs \\ %{}) do + session + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/apps/cf_http/lib/cf_http/users.ex b/apps/cf_http/lib/cf_http/users.ex new file mode 100644 index 000000000..102f806dc --- /dev/null +++ b/apps/cf_http/lib/cf_http/users.ex @@ -0,0 +1,106 @@ +defmodule CfHttp.Users do + @moduledoc """ + The Users context. + """ + + import Ecto.Query, warn: false + alias CfHttp.Repo + + alias CfHttp.Users.User + + @doc """ + Returns the list of users. + + ## Examples + + iex> list_users() + [%User{}, ...] + + """ + def list_users do + Repo.all(User) + end + + @doc """ + Gets a single user. + + Raises `Ecto.NoResultsError` if the User does not exist. + + ## Examples + + iex> get_user!(123) + %User{} + + iex> get_user!(456) + ** (Ecto.NoResultsError) + + """ + def get_user!(id), do: Repo.get!(User, id) + + @doc """ + Creates a user. + + ## Examples + + iex> create_user(%{field: value}) + {:ok, %User{}} + + iex> create_user(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_user(attrs \\ %{}) do + %User{} + |> User.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a user. + + ## Examples + + iex> update_user(user, %{field: new_value}) + {:ok, %User{}} + + iex> update_user(user, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_user(%User{} = user, attrs) do + user + |> User.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a user. + + ## Examples + + iex> delete_user(user) + {:ok, %User{}} + + iex> delete_user(user) + {:error, %Ecto.Changeset{}} + + """ + def delete_user(%User{} = user) do + Repo.delete(user) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking user changes. + + ## Examples + + iex> change_user(user) + %Ecto.Changeset{source: %User{}} + + """ + def change_user(%User{} = user) do + User.changeset(user, %{}) + end +end + + diff --git a/apps/cf_http/lib/cf_http/user.ex b/apps/cf_http/lib/cf_http/users/user.ex similarity index 94% rename from apps/cf_http/lib/cf_http/user.ex rename to apps/cf_http/lib/cf_http/users/user.ex index 16fdb6011..5b34bc16d 100644 --- a/apps/cf_http/lib/cf_http/user.ex +++ b/apps/cf_http/lib/cf_http/users/user.ex @@ -1,4 +1,4 @@ -defmodule CfHttp.User do +defmodule CfHttp.Users.User do @moduledoc """ Represents a User I guess """ diff --git a/apps/cf_http/lib/cf_http_web/templates/session/new.html.eex b/apps/cf_http/lib/cf_http_web/templates/session/new.html.eex new file mode 100644 index 000000000..5428d0e86 --- /dev/null +++ b/apps/cf_http/lib/cf_http_web/templates/session/new.html.eex @@ -0,0 +1,23 @@ +
+
+
+ Sign In +
+ + +
+
+ + +
+ +
+
+ +
+ +
+
diff --git a/apps/cf_http/priv/repo/migrations/20200510162435_create_sessions.exs b/apps/cf_http/priv/repo/migrations/20200510162435_create_sessions.exs new file mode 100644 index 000000000..60cca853b --- /dev/null +++ b/apps/cf_http/priv/repo/migrations/20200510162435_create_sessions.exs @@ -0,0 +1,13 @@ +defmodule CfHttp.Repo.Migrations.CreateSessions do + use Ecto.Migration + + def change do + create table(:sessions) do + add :user_id, references(:users, on_delete: :delete_all) + + timestamps() + end + + create index(:sessions, [:user_id]) + end +end From cb4c4ad8f52f5490f38c1c759c7bebfd71e18e1e Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Sun, 10 May 2020 11:55:01 -0500 Subject: [PATCH 7/8] Reorganizing page --- .../lib/cf_http_web/controllers/page_controller.ex | 7 ------- .../lib/cf_http_web/controllers/user_controller.ex | 2 +- apps/cf_http/lib/cf_http_web/plugs/authenticator.ex | 2 +- apps/cf_http/lib/cf_http_web/router.ex | 2 -- .../cf_http/lib/cf_http_web/templates/page/index.html.eex | 1 - apps/cf_http/lib/cf_http_web/views/page_view.ex | 3 --- .../test/cf_http_web/controllers/page_controller_test.exs | 8 -------- apps/cf_http/test/cf_http_web/views/page_view_test.exs | 3 --- 8 files changed, 2 insertions(+), 26 deletions(-) delete mode 100644 apps/cf_http/lib/cf_http_web/controllers/page_controller.ex delete mode 100644 apps/cf_http/lib/cf_http_web/templates/page/index.html.eex delete mode 100644 apps/cf_http/lib/cf_http_web/views/page_view.ex delete mode 100644 apps/cf_http/test/cf_http_web/controllers/page_controller_test.exs delete mode 100644 apps/cf_http/test/cf_http_web/views/page_view_test.exs diff --git a/apps/cf_http/lib/cf_http_web/controllers/page_controller.ex b/apps/cf_http/lib/cf_http_web/controllers/page_controller.ex deleted file mode 100644 index e122f634b..000000000 --- a/apps/cf_http/lib/cf_http_web/controllers/page_controller.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule CfHttpWeb.PageController do - use CfHttpWeb, :controller - - def index(conn, _params) do - render(conn, "index.html") - end -end diff --git a/apps/cf_http/lib/cf_http_web/controllers/user_controller.ex b/apps/cf_http/lib/cf_http_web/controllers/user_controller.ex index 2ee715eb5..aea355e96 100644 --- a/apps/cf_http/lib/cf_http_web/controllers/user_controller.ex +++ b/apps/cf_http/lib/cf_http_web/controllers/user_controller.ex @@ -4,7 +4,7 @@ defmodule CfHttpWeb.UserController do """ use CfHttpWeb, :controller - alias CfHttp.{Repo, User} + alias CfHttp.{Repo, Users.User} plug CfHttpWeb.Plugs.Authenticator when action in [:show, :edit, :update, :delete] diff --git a/apps/cf_http/lib/cf_http_web/plugs/authenticator.ex b/apps/cf_http/lib/cf_http_web/plugs/authenticator.ex index 19c661615..194cfa50b 100644 --- a/apps/cf_http/lib/cf_http_web/plugs/authenticator.ex +++ b/apps/cf_http/lib/cf_http_web/plugs/authenticator.ex @@ -4,7 +4,7 @@ defmodule CfHttpWeb.Plugs.Authenticator do """ import Plug.Conn - alias CfHttp.User + alias CfHttp.Users.User def init(default), do: default diff --git a/apps/cf_http/lib/cf_http_web/router.ex b/apps/cf_http/lib/cf_http_web/router.ex index 9239afc28..0cb9747fd 100644 --- a/apps/cf_http/lib/cf_http_web/router.ex +++ b/apps/cf_http/lib/cf_http_web/router.ex @@ -16,8 +16,6 @@ defmodule CfHttpWeb.Router do scope "/", CfHttpWeb do pipe_through :browser - get "/", PageController, :index - resources "/user", UserController, singleton: true, only: [:show, :edit, :update, :delete] resources "/users", UserController, only: [:new, :create] diff --git a/apps/cf_http/lib/cf_http_web/templates/page/index.html.eex b/apps/cf_http/lib/cf_http_web/templates/page/index.html.eex deleted file mode 100644 index d8e9d74ba..000000000 --- a/apps/cf_http/lib/cf_http_web/templates/page/index.html.eex +++ /dev/null @@ -1 +0,0 @@ -Welcome! diff --git a/apps/cf_http/lib/cf_http_web/views/page_view.ex b/apps/cf_http/lib/cf_http_web/views/page_view.ex deleted file mode 100644 index 6e3a53c9d..000000000 --- a/apps/cf_http/lib/cf_http_web/views/page_view.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule CfHttpWeb.PageView do - use CfHttpWeb, :view -end diff --git a/apps/cf_http/test/cf_http_web/controllers/page_controller_test.exs b/apps/cf_http/test/cf_http_web/controllers/page_controller_test.exs deleted file mode 100644 index a42397504..000000000 --- a/apps/cf_http/test/cf_http_web/controllers/page_controller_test.exs +++ /dev/null @@ -1,8 +0,0 @@ -defmodule CfHttpWeb.PageControllerTest do - use CfHttpWeb.ConnCase - - test "GET /", %{conn: conn} do - conn = get(conn, "/") - assert html_response(conn, 200) =~ "Welcome!" - end -end diff --git a/apps/cf_http/test/cf_http_web/views/page_view_test.exs b/apps/cf_http/test/cf_http_web/views/page_view_test.exs deleted file mode 100644 index d3f100c9a..000000000 --- a/apps/cf_http/test/cf_http_web/views/page_view_test.exs +++ /dev/null @@ -1,3 +0,0 @@ -defmodule CfHttpWeb.PageViewTest do - use CfHttpWeb.ConnCase, async: true -end From 149fabb22b580161f0652abd58515fb6b0010080 Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Mon, 11 May 2020 16:02:34 -0500 Subject: [PATCH 8/8] More housekeeping --- .../controllers/device_controller.ex | 8 ++- .../controllers/rule_controller.ex | 6 +- .../controllers/session_controller.ex | 59 +++++++++++++++++++ .../controllers/user_controller.ex | 26 ++++---- apps/cf_http/lib/cf_http_web/router.ex | 8 +++ .../cf_http_web/templates/layout/app.html.eex | 4 ++ 6 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 apps/cf_http/lib/cf_http_web/controllers/session_controller.ex diff --git a/apps/cf_http/lib/cf_http_web/controllers/device_controller.ex b/apps/cf_http/lib/cf_http_web/controllers/device_controller.ex index 69089af50..28ed56f17 100644 --- a/apps/cf_http/lib/cf_http_web/controllers/device_controller.ex +++ b/apps/cf_http/lib/cf_http_web/controllers/device_controller.ex @@ -1,8 +1,10 @@ defmodule CfHttpWeb.DeviceController do - use CfHttpWeb, :controller + @moduledoc """ + Implements the CRUD for a Device + """ - alias CfHttp.Devices - alias CfHttp.Devices.Device + use CfHttpWeb, :controller + alias CfHttp.{Devices, Devices.Device} plug CfHttpWeb.Plugs.Authenticator diff --git a/apps/cf_http/lib/cf_http_web/controllers/rule_controller.ex b/apps/cf_http/lib/cf_http_web/controllers/rule_controller.ex index 8838ebf0e..0cdd740a7 100644 --- a/apps/cf_http/lib/cf_http_web/controllers/rule_controller.ex +++ b/apps/cf_http/lib/cf_http_web/controllers/rule_controller.ex @@ -4,10 +4,7 @@ defmodule CfHttpWeb.RuleController do """ use CfHttpWeb, :controller - - alias CfHttp.Devices - alias CfHttp.Rules - alias CfHttp.Rules.Rule + alias CfHttp.{Devices, Rules, Rules.Rule} plug CfHttpWeb.Plugs.Authenticator @@ -44,6 +41,7 @@ defmodule CfHttpWeb.RuleController do def edit(conn, %{"id" => id}) do rule = Rules.get_rule!(id) changeset = Rules.change_rule(rule) + render(conn, "edit.html", rule: rule, changeset: changeset) end diff --git a/apps/cf_http/lib/cf_http_web/controllers/session_controller.ex b/apps/cf_http/lib/cf_http_web/controllers/session_controller.ex new file mode 100644 index 000000000..594b3ca26 --- /dev/null +++ b/apps/cf_http/lib/cf_http_web/controllers/session_controller.ex @@ -0,0 +1,59 @@ +defmodule CfHttpWeb.SessionController do + @moduledoc """ + Implements the CRUD for a Session + """ + + use CfHttpWeb, :controller + alias CfHttp.{Repo, Users.User, Sessions, Sessions.Session} + + plug :redirect_authenticated when action in [:new] + plug CfHttpWeb.Plugs.Authenticator when action in [:delete] + + # GET /sessions/new + def new(conn, _params) do + changeset = Session.changeset(%Session{}) + + render(conn, "new.html", changeset: changeset) + end + + # Sign In + # POST /sessions + def create(conn, params) do + changeset = Session.changeset(%Session{}, params) + + case Repo.insert(changeset) do + {:ok, session} -> + conn + |> assign(:current_session, session) + |> put_flash(:info, "Session created successfully") + |> redirect(to: Routes.device_path(conn, :index)) + + {:error, changeset} -> + conn + |> put_flash(:error, "Error creating session.") + |> render("new.html", changeset: changeset) + end + end + + # Sign Out + # DELETE /session + def delete(conn, _params) do + case Repo.delete(conn.current_session) do + {:ok, _session} -> + conn + |> assign(:current_session, nil) + |> put_flash(:info, "Session deleted successfully.") + |> redirect(to: "/") + end + end + + defp redirect_authenticated(conn, _) do + user = %User{id: 1, email: "dev_user@fireguard.network"} + session = %Session{user_id: user.id} + + conn + |> assign(:current_session, session) + |> redirect(to: "/") + |> halt() + end +end diff --git a/apps/cf_http/lib/cf_http_web/controllers/user_controller.ex b/apps/cf_http/lib/cf_http_web/controllers/user_controller.ex index aea355e96..0baf01dc3 100644 --- a/apps/cf_http/lib/cf_http_web/controllers/user_controller.ex +++ b/apps/cf_http/lib/cf_http_web/controllers/user_controller.ex @@ -4,30 +4,26 @@ defmodule CfHttpWeb.UserController do """ use CfHttpWeb, :controller - alias CfHttp.{Repo, Users.User} + alias CfHttp.{Repo, Users, Users.User} plug CfHttpWeb.Plugs.Authenticator when action in [:show, :edit, :update, :delete] # GET /users/new def new(conn, _params) do - changeset = User.changeset(%User{}) - - conn - |> render("new.html", changeset: changeset) + changeset = Users.change_user(%User{}) + render(conn, "new.html", changeset: changeset) end # POST /users - def create(conn, params) do - changeset = User.changeset(%User{}, params) - - case Repo.insert(changeset) do + def create(conn, %{"user" => user_params}) do + case Users.create_user(conn, user_params) do {:ok, user} -> conn |> assign(:current_user, user) - |> put_flash(:info, "User created successfully") + |> put_flash(:info, "User created successfully.") |> redirect(to: Routes.device_path(conn, :index)) - {:error, changeset} -> + {:error, %Ecto.Changeset{} = changeset} -> conn |> put_flash(:error, "Error creating user.") |> render("new.html", changeset: changeset) @@ -36,10 +32,10 @@ defmodule CfHttpWeb.UserController do # GET /user/edit def edit(conn, _params) do - changeset = User.changeset(conn.current_user) + user = conn.current_user + changeset = Users.change_user(user) - conn - |> render("edit.html", changeset: changeset) + render(conn, "edit.html", changeset: changeset) end # GET /user @@ -73,7 +69,7 @@ defmodule CfHttpWeb.UserController do conn |> assign(:current_user, nil) |> put_flash(:info, "User deleted successfully.") - |> redirect(to: Routes.page_path(conn, :index)) + |> redirect(to: "/") {:error, _changeset} -> conn diff --git a/apps/cf_http/lib/cf_http_web/router.ex b/apps/cf_http/lib/cf_http_web/router.ex index 0cb9747fd..5e6cea2e9 100644 --- a/apps/cf_http/lib/cf_http_web/router.ex +++ b/apps/cf_http/lib/cf_http_web/router.ex @@ -1,4 +1,8 @@ defmodule CfHttpWeb.Router do + @moduledoc """ + Main Application Router + """ + use CfHttpWeb, :router pipeline :browser do @@ -24,6 +28,10 @@ defmodule CfHttpWeb.Router do end resources "/rules", RuleController, only: [:show, :update, :delete, :edit] + + resources "/sessions", SessionController, only: [:new, :create, :delete] + + get "/", DeviceController, :index end # Other scopes may use custom stacks. diff --git a/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex b/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex index 5a5c91f47..79a1244b8 100644 --- a/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex +++ b/apps/cf_http/lib/cf_http_web/templates/layout/app.html.eex @@ -19,6 +19,10 @@

+
+ <%= @inner_content %> +
+