Skip to content

Commit

Permalink
Add Coherence
Browse files Browse the repository at this point in the history
  • Loading branch information
taneliang committed Aug 14, 2018
1 parent 397529e commit db2c033
Show file tree
Hide file tree
Showing 36 changed files with 1,129 additions and 2 deletions.
26 changes: 26 additions & 0 deletions api/sync/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,29 @@ 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: Sync.Coherence.User,
repo: Sync.Repo,
module: Sync,
web_module: SyncWeb,
router: SyncWeb.Router,
messages_backend: SyncWeb.Coherence.Messages,
logged_out_url: "/",
email_from_name: "Your Name",
email_from_email: "[email protected]",
opts: [
:authenticatable,
:recoverable,
:trackable,
:unlockable_with_token,
:confirmable,
:registerable
]

config :coherence, SyncWeb.Coherence.Mailer,
adapter: Swoosh.Adapters.Sendgrid,
api_key: "your api key here"

# %% End Coherence Configuration %%
138 changes: 138 additions & 0 deletions api/sync/lib/sync/coherence/schemas.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
defmodule Sync.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([], 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
31 changes: 31 additions & 0 deletions api/sync/lib/sync/coherence/user.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Sync.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
124 changes: 124 additions & 0 deletions api/sync/lib/sync_web/coherence_messages.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
defmodule SyncWeb.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 SyncWeb.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: Gettext.dgettext(@domain, "Account already confirmed.")
def account_is_not_locked, do: Gettext.dgettext(@domain, "Account is not locked.")
def account_updated_successfully, do: Gettext.dgettext(@domain, "Account updated successfully.")
def already_confirmed, do: Gettext.dgettext(@domain, "already confirmed")
def already_locked, do: Gettext.dgettext(@domain, "already locked")
def already_logged_in, do: Gettext.dgettext(@domain, "Already logged in.")
def cant_be_blank, do: Gettext.dgettext(@domain, "can't be blank")
def cant_find_that_token, do: Gettext.dgettext(@domain, "Can't find that token")
def confirmation_email_sent, do: Gettext.dgettext(@domain, "Confirmation email sent.")
def confirmation_token_expired, do: Gettext.dgettext(@domain, "Confirmation token expired.")

def could_not_find_that_email_address,
do: Gettext.dgettext(@domain, "Could not find that email address")

def forgot_your_password, do: Gettext.dgettext(@domain, "Forgot your password?")
def http_authentication_required, do: Gettext.dgettext(@domain, "HTTP Authentication Required")

def incorrect_login_or_password(opts),
do: Gettext.dgettext(@domain, "Incorrect %{login_field} or password.", opts)

def invalid_current_password, do: Gettext.dgettext(@domain, "invalid current password")

def invalid_invitation,
do: Gettext.dgettext(@domain, "Invalid Invitation. Please contact the site administrator.")

def invalid_request, do: Gettext.dgettext(@domain, "Invalid Request.")
def invalid_confirmation_token, do: Gettext.dgettext(@domain, "Invalid confirmation token.")
def invalid_email_or_password, do: Gettext.dgettext(@domain, "Invalid email or password.")
def invalid_invitation_token, do: Gettext.dgettext(@domain, "Invalid invitation token.")
def invalid_reset_token, do: Gettext.dgettext(@domain, "Invalid reset token.")
def invalid_unlock_token, do: Gettext.dgettext(@domain, "Invalid unlock token.")
def invitation_already_sent, do: Gettext.dgettext(@domain, "Invitation already sent.")
def invitation_sent, do: Gettext.dgettext(@domain, "Invitation sent.")
def invite_someone, do: Gettext.dgettext(@domain, "Invite Someone")

def maximum_login_attempts_exceeded,
do:
Gettext.dgettext(@domain, "Maximum Login attempts exceeded. Your account has been locked.")

def need_an_account, do: Gettext.dgettext(@domain, "Need An Account?")
def not_locked, do: Gettext.dgettext(@domain, "not locked")
def password_reset_token_expired, do: Gettext.dgettext(@domain, "Password reset token expired.")

def password_updated_successfully,
do: Gettext.dgettext(@domain, "Password updated successfully.")

def problem_confirming_user_account,
do:
Gettext.dgettext(
@domain,
"Problem confirming user account. Please contact the system administrator."
)

def registration_created_successfully,
do: Gettext.dgettext(@domain, "Registration created successfully.")

def required, do: Gettext.dgettext(@domain, "required")
def resend_confirmation_email, do: Gettext.dgettext(@domain, "Resend confirmation email")

def reset_email_sent,
do: Gettext.dgettext(@domain, "Reset email sent. Check your email for a reset link.")

def restricted_area, do: Gettext.dgettext(@domain, "Restricted Area")
def send_an_unlock_email, do: Gettext.dgettext(@domain, "Send an unlock email")
def sign_in, do: Gettext.dgettext(@domain, "Sign In")
def sign_out, do: Gettext.dgettext(@domain, "Sign Out")
def signed_in_successfully, do: Gettext.dgettext(@domain, "Signed in successfully.")

def too_many_failed_login_attempts,
do: Gettext.dgettext(@domain, "Too many failed login attempts. Account has been locked.")

def unauthorized_ip_address, do: Gettext.dgettext(@domain, "Unauthorized IP Address")
def unlock_instructions_sent, do: Gettext.dgettext(@domain, "Unlock Instructions sent.")

def user_account_confirmed_successfully,
do: Gettext.dgettext(@domain, "User account confirmed successfully.")

def user_already_has_an_account, do: Gettext.dgettext(@domain, "User already has an account!")

def you_must_confirm_your_account,
do: Gettext.dgettext(@domain, "You must confirm your account before you can login.")

def your_account_has_been_unlocked,
do: Gettext.dgettext(@domain, "Your account has been unlocked")

def your_account_is_not_locked, do: Gettext.dgettext(@domain, "Your account is not locked.")

def verify_user_token(opts),
do: Gettext.dgettext(@domain, "Invalid %{user_token} error: %{error}", opts)

def you_are_using_an_invalid_security_token,
do:
Gettext.dgettext(
@domain,
"You are using an invalid security token for this site! This security\n" <>
"violation has been logged.\n"
)

def mailer_required, do: Gettext.dgettext(@domain, "Mailer configuration required!")
def account_is_inactive(), do: Gettext.dgettext(@domain, "Account is inactive!")
end
47 changes: 47 additions & 0 deletions api/sync/lib/sync_web/coherence_web.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule SyncWeb.Coherence do
@moduledoc false

def view do
quote do
use Phoenix.View, root: "lib/sync_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 SyncWeb.Router.Helpers
import SyncWeb.ErrorHelpers
import SyncWeb.Gettext
import SyncWeb.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 SyncWeb.Router.Helpers
import SyncWeb.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 db2c033

Please sign in to comment.