Skip to content

Commit

Permalink
mix coh.install --authenticatable --registerable --recoverable --reme…
Browse files Browse the repository at this point in the history
…mberable --trackable [#70]
  • Loading branch information
marnen committed Feb 27, 2018
1 parent b91edd6 commit d81d571
Show file tree
Hide file tree
Showing 30 changed files with 997 additions and 0 deletions.
18 changes: 18 additions & 0 deletions phoenix/contraq/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ config :logger, :console,
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"

# %% Coherence Configuration %% Don't remove this line
config :coherence,
user_schema: Contraq.Coherence.User,
repo: Contraq.Repo,
module: Contraq,
web_module: ContraqWeb,
router: ContraqWeb.Router,
messages_backend: ContraqWeb.Coherence.Messages,
logged_out_url: "/",
email_from_name: "Your Name",
email_from_email: "[email protected]",
opts: [:trackable, :rememberable, :recoverable, :registerable, :authenticatable]

config :coherence, ContraqWeb.Coherence.Mailer,
adapter: Swoosh.Adapters.Sendgrid,
api_key: "your api key here"
# %% End Coherence Configuration %%
44 changes: 44 additions & 0 deletions phoenix/contraq/lib/contraq/coherence/rememberable.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule Contraq.Coherence.Rememberable do
@moduledoc false
use Ecto.Schema

import Ecto.Changeset
import Ecto.Query

alias Coherence.Config



schema "rememberables" do
field :series_hash, :string
field :token_hash, :string
field :token_created_at, Timex.Ecto.DateTime
belongs_to :user, Module.concat(Config.module, Config.user_schema)

timestamps()
end

use Coherence.Rememberable

@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
@spec changeset(Ecto.Schema.t, Map.t) :: Ecto.Changeset.t
def changeset(model, params \\ %{}) do
model
|> cast(params, ~w(series_hash token_hash token_created_at user_id))
|> validate_required(~w(series_hash token_hash token_created_at user_id)a)
end

@doc """
Creates a changeset for a new schema
"""
@spec new_changeset(Map.t) :: Ecto.Changeset.t
def new_changeset(params \\ %{}) do
changeset %Rememberable{}, params
end

end
141 changes: 141 additions & 0 deletions phoenix/contraq/lib/contraq/coherence/schemas.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
defmodule Contraq.Coherence.Schemas do

use Coherence.Config

import Ecto.Query

@user_schema Config.user_schema
@repo Config.repo

def list_user do
@repo.all @user_schema
end

def get_by_user(opts) do
@repo.get_by @user_schema, opts
end

def get_user(id) do
@repo.get @user_schema, id
end

def get_user!(id) do
@repo.get! @user_schema, id
end

def get_user_by_email(email) do
@repo.get_by @user_schema, email: email
end

def change_user(struct, params) do
@user_schema.changeset struct, params
end

def change_user(params) do
@user_schema.changeset @user_schema.__struct__, params
end

def change_user do
@user_schema.changeset @user_schema.__struct__, %{}
end

def update_user(user, params) do
@repo.update change_user(user, params)
end

def create_user(params) do
@repo.insert change_user(params)
end

Enum.each [Contraq.Coherence.Rememberable], fn module ->

name =
module
|> Module.split
|> List.last
|> String.downcase

def unquote(String.to_atom("list_#{name}"))() do
@repo.all unquote(module)
end

def unquote(String.to_atom("list_#{name}"))(%Ecto.Query{} = query) do
@repo.all query
end

def unquote(String.to_atom("get_#{name}"))(id) do
@repo.get unquote(module), id
end

def unquote(String.to_atom("get_#{name}!"))(id) do
@repo.get! unquote(module), id
end

def unquote(String.to_atom("get_by_#{name}"))(opts) do
@repo.get_by unquote(module), opts
end

def unquote(String.to_atom("change_#{name}"))(struct, params) do
unquote(module).changeset(struct, params)
end

def unquote(String.to_atom("change_#{name}"))(params) do
unquote(module).new_changeset(params)
end

def unquote(String.to_atom("change_#{name}"))() do
unquote(module).new_changeset(%{})
end

def unquote(String.to_atom("create_#{name}"))(params) do
@repo.insert unquote(module).new_changeset(params)
end

def unquote(String.to_atom("update_#{name}"))(struct, params) do
@repo.update unquote(module).changeset(struct, params)
end

def unquote(String.to_atom("delete_#{name}"))(struct) do
@repo.delete struct
end
end

def query_by(schema, opts) do
Enum.reduce opts, schema, fn {k, v}, query ->
where(query, [b], field(b, ^k) == ^v)
end
end

def delete_all(%Ecto.Query{} = query) do
@repo.delete_all query
end

def delete_all(module) when is_atom(module) do
@repo.delete_all module
end

def create(%Ecto.Changeset{} = changeset) do
@repo.insert changeset
end

def create!(%Ecto.Changeset{} = changeset) do
@repo.insert! changeset
end

def update(%Ecto.Changeset{} = changeset) do
@repo.update changeset
end

def update!(%Ecto.Changeset{} = changeset) do
@repo.update! changeset
end

def delete(schema) do
@repo.delete schema
end

def delete!(schema) do
@repo.delete! schema
end

end
30 changes: 30 additions & 0 deletions phoenix/contraq/lib/contraq/coherence/user.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Contraq.Coherence.User do
@moduledoc false
use Ecto.Schema
use Coherence.Schema



schema "users" do
field :name, :string
field :email, :string
coherence_schema()

timestamps()
end

def changeset(model, params \\ %{}) do
model
|> cast(params, [:name, :email] ++ coherence_fields())
|> validate_required([:name, :email])
|> validate_format(:email, ~r/@/)
|> unique_constraint(:email)
|> validate_coherence(params)
end

def changeset(model, params, :password) do
model
|> cast(params, ~w(password password_confirmation reset_password_token reset_password_sent_at))
|> validate_coherence_password_reset(params)
end
end
79 changes: 79 additions & 0 deletions phoenix/contraq/lib/contraq_web/coherence_messages.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
defmodule ContraqWeb.Coherence.Messages do
@moduledoc """
Application facing messages generated by the Coherence application.
This module was created by the coh.install mix task. It contains all the
messages used in the coherence application except those in other generated
files like the view and templates.
To assist in upgrading Coherence, the `Coherence.Messages behaviour will
alway contain every message for the current version. This will help in upgrades
to ensure the user had added new the new messages from the current version.
"""
@behaviour Coherence.Messages

import ContraqWeb.Gettext

# Change this to override the "coherence" gettext domain. If you would like
# the coherence message to be part of your projects domain change it to "default"
@domain "coherence"

##################
# Messages

def account_already_confirmed, do: dgettext(@domain, "Account already confirmed.")
def account_is_not_locked, do: dgettext(@domain, "Account is not locked.")
def account_updated_successfully, do: dgettext(@domain, "Account updated successfully.")
def already_confirmed, do: dgettext(@domain, "already confirmed")
def already_locked, do: dgettext(@domain, "already locked")
def already_logged_in, do: dgettext(@domain, "Already logged in.")
def cant_be_blank, do: dgettext(@domain, "can't be blank")
def cant_find_that_token, do: dgettext(@domain, "Can't find that token")
def confirmation_email_sent, do: dgettext(@domain, "Confirmation email sent.")
def confirmation_token_expired, do: dgettext(@domain, "Confirmation token expired.")
def could_not_find_that_email_address, do: dgettext(@domain, "Could not find that email address")
def forgot_your_password, do: dgettext(@domain, "Forgot your password?")
def http_authentication_required, do: dgettext(@domain, "HTTP Authentication Required")
def incorrect_login_or_password(opts), do: dgettext(@domain, "Incorrect %{login_field} or password.", opts)
def invalid_current_password, do: dgettext(@domain, "invalid current password")
def invalid_invitation, do: dgettext(@domain, "Invalid Invitation. Please contact the site administrator.")
def invalid_request, do: dgettext(@domain, "Invalid Request.")
def invalid_confirmation_token, do: dgettext(@domain, "Invalid confirmation token.")
def invalid_email_or_password, do: dgettext(@domain, "Invalid email or password.")
def invalid_invitation_token, do: dgettext(@domain, "Invalid invitation token.")
def invalid_reset_token, do: dgettext(@domain, "Invalid reset token.")
def invalid_unlock_token, do: dgettext(@domain, "Invalid unlock token.")
def invitation_already_sent, do: dgettext(@domain, "Invitation already sent.")
def invitation_sent, do: dgettext(@domain, "Invitation sent.")
def invite_someone, do: dgettext(@domain, "Invite Someone")
def maximum_login_attempts_exceeded, do: dgettext(@domain, "Maximum Login attempts exceeded. Your account has been locked.")
def need_an_account, do: dgettext(@domain, "Need An Account?")
def not_locked, do: dgettext(@domain, "not locked")
def password_reset_token_expired, do: dgettext(@domain, "Password reset token expired.")
def password_updated_successfully, do: dgettext(@domain, "Password updated successfully.")
def problem_confirming_user_account, do: dgettext(@domain, "Problem confirming user account. Please contact the system administrator.")
def registration_created_successfully, do: dgettext(@domain, "Registration created successfully.")
def required, do: dgettext(@domain, "required")
def resend_confirmation_email, do: dgettext(@domain, "Resend confirmation email")
def reset_email_sent, do: dgettext(@domain, "Reset email sent. Check your email for a reset link.")
def restricted_area, do: dgettext(@domain, "Restricted Area")
def send_an_unlock_email, do: dgettext(@domain, "Send an unlock email")
def sign_in, do: dgettext(@domain, "Sign In")
def sign_out, do: dgettext(@domain, "Sign Out")
def signed_in_successfully, do: dgettext(@domain, "Signed in successfully.")
def too_many_failed_login_attempts, do: dgettext(@domain, "Too many failed login attempts. Account has been locked.")
def unauthorized_ip_address, do: dgettext(@domain, "Unauthorized IP Address")
def unlock_instructions_sent, do: dgettext(@domain, "Unlock Instructions sent.")
def user_account_confirmed_successfully, do: dgettext(@domain, "User account confirmed successfully.")
def user_already_has_an_account, do: dgettext(@domain, "User already has an account!")
def you_must_confirm_your_account, do: dgettext(@domain, "You must confirm your account before you can login.")
def your_account_has_been_unlocked, do: dgettext(@domain, "Your account has been unlocked")
def your_account_is_not_locked, do: dgettext(@domain, "Your account is not locked.")
def verify_user_token(opts),
do: dgettext(@domain, "Invalid %{user_token} error: %{error}", opts)
def you_are_using_an_invalid_security_token,
do: dgettext(@domain, "You are using an invalid security token for this site! This security\n" <>
"violation has been logged.\n")
def mailer_required, do: dgettext(@domain, "Mailer configuration required!")
def account_is_inactive(), do: dgettext(@domain, "Account is inactive!")
end
47 changes: 47 additions & 0 deletions phoenix/contraq/lib/contraq_web/coherence_web.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule ContraqWeb.Coherence do
@moduledoc false

def view do
quote do
use Phoenix.View, root: "lib/contraq_web/templates"
# Import convenience functions from controllers

import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]

# Use all HTML functionality (forms, tags, etc)
use Phoenix.HTML

import ContraqWeb.Router.Helpers
import ContraqWeb.ErrorHelpers
import ContraqWeb.Gettext
import ContraqWeb.Coherence.ViewHelpers
end
end

def controller do
quote do
use Phoenix.Controller, except: [layout_view: 2]
use Coherence.Config
use Timex

import Ecto
import Ecto.Query
import Plug.Conn
import ContraqWeb.Router.Helpers
import ContraqWeb.Gettext
import Coherence.ControllerHelpers

alias Coherence.Config
alias Coherence.ControllerHelpers, as: Helpers

require Redirects
end
end

@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
end
Loading

0 comments on commit d81d571

Please sign in to comment.