diff --git a/config/dev.exs b/config/dev.exs --- a/config/dev.exs +++ b/config/dev.exs @@ -35,9 +35,9 @@ # Configure your database config :kolab_chat, KolabChat.Repo, - adapter: Ecto.Adapters.Postgres, - username: "postgres", - password: "postgres", + adapter: Ecto.Adapters.MySQL, + username: "root", + password: "12345", database: "kolab_chat_dev", hostname: "localhost", pool_size: 10 diff --git a/config/test.exs b/config/test.exs --- a/config/test.exs +++ b/config/test.exs @@ -11,9 +11,9 @@ # Configure your database config :kolab_chat, KolabChat.Repo, - adapter: Ecto.Adapters.Postgres, - username: "postgres", - password: "postgres", + adapter: Ecto.Adapters.MySQL, + username: "root", + password: "12345", database: "kolab_chat_test", hostname: "localhost", pool: Ecto.Adapters.SQL.Sandbox diff --git a/lib/kolab_chat.ex b/lib/kolab_chat.ex --- a/lib/kolab_chat.ex +++ b/lib/kolab_chat.ex @@ -9,7 +9,7 @@ # Define workers and child supervisors to be supervised children = [ # Start the Ecto repository - #supervisor(KolabChat.Repo, []), + supervisor(KolabChat.Repo, []), # Start the endpoint when the application starts supervisor(KolabChat.Endpoint, []), # Start your own worker by calling: KolabChat.Worker.start_link(arg1, arg2, arg3) diff --git a/mix.exs b/mix.exs --- a/mix.exs +++ b/mix.exs @@ -19,7 +19,7 @@ def application do [mod: {KolabChat, []}, applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext, - :phoenix_ecto, :postgrex]] + :phoenix_ecto, :mariaex]] end # Specifies which paths to compile per environment. @@ -33,7 +33,7 @@ [{:phoenix, "~> 1.2.1"}, {:phoenix_pubsub, "~> 1.0"}, {:phoenix_ecto, "~> 3.0"}, - {:postgrex, ">= 0.0.0"}, + {:mariaex, "~> 0.7.9", override: true}, {:phoenix_html, "~> 2.6"}, {:phoenix_live_reload, "~> 1.0", only: :dev}, {:gettext, "~> 0.11"}, diff --git a/mix.lock b/mix.lock --- a/mix.lock +++ b/mix.lock @@ -1,19 +1,19 @@ %{"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], []}, "cowboy": {:hex, :cowboy, "1.0.4", "a324a8df9f2316c833a470d918aaf73ae894278b8aa6226ce7a9bf699388f878", [:rebar, :make], [{:cowlib, "~> 1.0.0", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.0", [hex: :ranch, optional: false]}]}, "cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []}, - "db_connection": {:hex, :db_connection, "1.1.0", "b2b88db6d7d12f99997b584d09fad98e560b817a20dab6a526830e339f54cdb3", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]}, + "db_connection": {:hex, :db_connection, "1.0.0", "63c03e520d54886a66104d34e32397ba960db6e74b596ce221592c07d6a40d8d", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]}, "decimal": {:hex, :decimal, "1.3.1", "157b3cedb2bfcb5359372a7766dd7a41091ad34578296e951f58a946fcab49c6", [:mix], []}, - "ecto": {:hex, :ecto, "2.0.5", "7f4c79ac41ffba1a4c032b69d7045489f0069c256de606523c65d9f8188e502d", [:mix], [{:db_connection, "~> 1.0-rc.4", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.1.2 or ~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.7.7", [hex: :mariaex, optional: true]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.12.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}]}, + "ecto": {:hex, :ecto, "2.0.6", "9dcbf819c2a77f67a66b83739b7fcc00b71aaf6c100016db4f798930fa4cfd47", [:mix], [{:db_connection, "~> 1.0", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.1.2 or ~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.12.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}]}, "fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []}, - "gettext": {:hex, :gettext, "0.12.1", "c0624f52763469ef7a3674919ae28b8286d88195b90fa1516180f31bbbd26d14", [:mix], []}, + "gettext": {:hex, :gettext, "0.13.0", "daafbddc5cda12738bb93b01d84105fe75b916a302f1c50ab9fb066b95ec9db4", [:mix], []}, + "mariaex": {:hex, :mariaex, "0.7.9", "52f837cf1b0717f95a0e62624bb99707329cba599885cf3bd2fdecc0172a98ad", [:mix], [{:db_connection, "~> 1.0.0-rc", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}, "mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [:mix], []}, "phoenix": {:hex, :phoenix, "1.2.1", "6dc592249ab73c67575769765b66ad164ad25d83defa3492dc6ae269bd2a68ab", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.1", [hex: :plug, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]}, "phoenix_ecto": {:hex, :phoenix_ecto, "3.0.1", "42eb486ef732cf209d0a353e791806721f33ff40beab0a86f02070a5649ed00a", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, optional: false]}, {:phoenix_html, "~> 2.6", [hex: :phoenix_html, optional: true]}, {:plug, "~> 1.0", [hex: :plug, optional: false]}]}, "phoenix_html": {:hex, :phoenix_html, "2.8.0", "777598a4b6609ad6ab8b180f7b25c9af2904644e488922bb9b9b03ce988d20b1", [:mix], [{:plug, "~> 1.0", [hex: :plug, optional: false]}]}, - "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.5", "829218c4152ba1e9848e2bf8e161fcde6b4ec679a516259442561d21fde68d0b", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2-rc", [hex: :phoenix, optional: false]}]}, + "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.6", "4490d588c4f60248b1c5f1f0dc0a7271e1aed4bddbd8b1542630f7bf6bc7b012", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2-rc", [hex: :phoenix, optional: false]}]}, "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.1", "c10ddf6237007c804bf2b8f3c4d5b99009b42eca3a0dfac04ea2d8001186056a", [:mix], []}, - "plug": {:hex, :plug, "1.2.2", "cfbda521b54c92ab8ddffb173fbaabed8d8fc94bec07cd9bb58a84c1c501b0bd", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]}, + "plug": {:hex, :plug, "1.3.0", "6e2b01afc5db3fd011ca4a16efd9cb424528c157c30a44a0186bcc92c7b2e8f3", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]}, "poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []}, "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []}, - "postgrex": {:hex, :postgrex, "0.12.1", "2f8b46cb3a44dcd42f42938abedbfffe7e103ba4ce810ccbeee8dcf27ca0fb06", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.0-rc.4", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}, "ranch": {:hex, :ranch, "1.2.1", "a6fb992c10f2187b46ffd17ce398ddf8a54f691b81768f9ef5f461ea7e28c762", [:make], []}} diff --git a/priv/repo/migrations/20161213091438_add_users.exs b/priv/repo/migrations/20161213091438_add_users.exs new file mode 100644 --- /dev/null +++ b/priv/repo/migrations/20161213091438_add_users.exs @@ -0,0 +1,10 @@ +defmodule KolabChat.Repo.Migrations.AddUsers do + use Ecto.Migration + + def change do + create table(:users) do + add :username, :string + add :status, :string + end + end +end diff --git a/web/controllers/auth_controller.ex b/web/controllers/auth_controller.ex --- a/web/controllers/auth_controller.ex +++ b/web/controllers/auth_controller.ex @@ -1,6 +1,8 @@ defmodule KolabChat.AuthController do use KolabChat.Web, :controller + alias KolabChat.User + @doc """ Handler for the default logon form """ @@ -10,13 +12,34 @@ cond do is_nil(user) or user == "" -> conn - |> put_flash(:error, gettext("Invalid username")) + |> put_flash(:error, gettext("Invalid username!")) |> redirect(to: "/") true -> + changeset = User.changeset(%User{}, %{username: user}) + signin(conn, changeset) + end + end + + defp signin(conn, changeset) do + case insert_or_update_user(changeset) do + {:ok, user} -> conn - |> put_flash(:info, gettext("Successfully authenticated")) - |> put_session(:user, user) + |> put_flash(:info, gettext("Signed in!")) + |> put_session(:user_id, user.id) |> redirect(to: "/") + {:error, _reason} -> + conn + |> put_flash(:error, gettext("Error signing in")) + |> redirect(to: "/") + end + end + + defp insert_or_update_user(changeset) do + case Repo.get_by(User, username: changeset.changes.username) do + nil -> + Repo.insert(changeset) + user -> + {:ok, user} end end diff --git a/web/controllers/plugs/set_user.ex b/web/controllers/plugs/set_user.ex --- a/web/controllers/plugs/set_user.ex +++ b/web/controllers/plugs/set_user.ex @@ -1,11 +1,16 @@ defmodule KolabChat.Plugs.SetUser do import Plug.Conn + alias KolabChat.Repo + alias KolabChat.User + def init(params), do: params def call(conn, _params) do + user_id = get_session(conn, :user_id) + cond do - user = get_session(conn, :user) -> + user = user_id && Repo.get(User, user_id) -> assign(conn, :user, user) true -> assign(conn, :user, nil) diff --git a/web/models/user.ex b/web/models/user.ex new file mode 100644 --- /dev/null +++ b/web/models/user.ex @@ -0,0 +1,14 @@ +defmodule KolabChat.User do + use KolabChat.Web, :model + + schema "users" do + field :username, :string + field :status, :string + end + + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [:username]) + |> validate_required([:username]) + end +end