diff --git a/config/dev.exs b/config/dev.exs index 31f732d..88d7498 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -1,43 +1,43 @@ use Mix.Config # For development, we disable any cache and enable # debugging and code reloading. # # The watchers configuration can be used to run external # watchers to your application. For example, we use it # with brunch.io to recompile .js and .css sources. config :kolab_chat, KolabChat.Endpoint, http: [port: 4000], debug_errors: true, code_reloader: true, check_origin: false, watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin", cd: Path.expand("../", __DIR__)]] # Watch static and templates for browser reloading. config :kolab_chat, KolabChat.Endpoint, live_reload: [ patterns: [ ~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$}, ~r{priv/gettext/.*(po)$}, ~r{web/views/.*(ex)$}, ~r{web/templates/.*(eex)$} ] ] # Do not include metadata nor timestamps in development logs config :logger, :console, format: "[$level] $message\n" # Set a higher stacktrace during development. Avoid configuring such # in production as building large stacktraces may be expensive. config :phoenix, :stacktrace_depth, 20 # 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 index 981a8fa..1b65ffa 100644 --- a/config/test.exs +++ b/config/test.exs @@ -1,19 +1,19 @@ use Mix.Config # We don't run a server during test. If one is required, # you can enable the server option below. config :kolab_chat, KolabChat.Endpoint, http: [port: 4001], server: false # Print only warnings and errors during test config :logger, level: :warn # 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 index 542e0c3..41af2d8 100644 --- a/lib/kolab_chat.ex +++ b/lib/kolab_chat.ex @@ -1,31 +1,31 @@ defmodule KolabChat do use Application # See http://elixir-lang.org/docs/stable/elixir/Application.html # for more information on OTP Applications def start(_type, _args) do import Supervisor.Spec # 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) # worker(KolabChat.Worker, [arg1, arg2, arg3]), ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: KolabChat.Supervisor] Supervisor.start_link(children, opts) end # Tell Phoenix to update the endpoint configuration # whenever the application is updated. def config_change(changed, _new, removed) do KolabChat.Endpoint.config_change(changed, removed) :ok end end diff --git a/mix.exs b/mix.exs index 4033e82..c06a57a 100644 --- a/mix.exs +++ b/mix.exs @@ -1,54 +1,54 @@ defmodule KolabChat.Mixfile do use Mix.Project def project do [app: :kolab_chat, version: "0.0.1", elixir: "~> 1.2", elixirc_paths: elixirc_paths(Mix.env), compilers: [:phoenix, :gettext] ++ Mix.compilers, build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, aliases: aliases(), deps: deps()] end # Configuration for the OTP application. # # Type `mix help compile.app` for more information. 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. defp elixirc_paths(:test), do: ["lib", "web", "test/support"] defp elixirc_paths(_), do: ["lib", "web"] # Specifies your project dependencies. # # Type `mix help deps` for examples and options. defp deps do [{: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"}, {:cowboy, "~> 1.0"}] end # Aliases are shortcuts or tasks specific to the current project. # For example, to create, migrate and run the seeds file at once: # # $ mix ecto.setup # # See the documentation for `Mix` for more info on aliases. defp aliases do ["ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], "ecto.reset": ["ecto.drop", "ecto.setup"], "test": ["ecto.create --quiet", "ecto.migrate", "test"]] end end diff --git a/mix.lock b/mix.lock index 5d0acc8..f01dbbd 100644 --- 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 index 0000000..451c432 --- /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 index 611e1c3..40afc53 100644 --- a/web/controllers/auth_controller.ex +++ b/web/controllers/auth_controller.ex @@ -1,31 +1,54 @@ defmodule KolabChat.AuthController do use KolabChat.Web, :controller + alias KolabChat.User + @doc """ Handler for the default logon form """ def default_callback(conn, params) do %{"logon" => %{"password" => _pass, "username" => user}} = params 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 @doc """ Handler for logout action """ def logout(conn, _params) do conn |> configure_session(drop: true) |> redirect(to: "/") end end diff --git a/web/controllers/plugs/set_user.ex b/web/controllers/plugs/set_user.ex index 7b1adf8..803cf94 100644 --- a/web/controllers/plugs/set_user.ex +++ b/web/controllers/plugs/set_user.ex @@ -1,14 +1,19 @@ 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) end end end diff --git a/web/models/user.ex b/web/models/user.ex new file mode 100644 index 0000000..125bc5c --- /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