|
1 | | -defmodule Triplex.Plug do |
2 | | - @moduledoc """ |
3 | | - This module have some basic functions for our triplex plugs. |
| 1 | +if Code.ensure_loaded?(Plug) do |
| 2 | + defmodule Triplex.Plug do |
| 3 | + @moduledoc """ |
| 4 | + This module have some basic functions for our triplex plugs. |
4 | 5 |
|
5 | | - The plugs we have for now are: |
| 6 | + The plugs we have for now are: |
6 | 7 |
|
7 | | - - `Triplex.ParamPlug` - loads the tenant from a body or query param |
8 | | - - `Triplex.SessionPlug` - loads the tenant from a session param |
9 | | - - `Triplex.SubdomainPlug` - loads the tenant from the url subdomain |
10 | | - - `Triplex.EnsurePlug` - ensures the current tenant is loaded and halts if not |
11 | | - """ |
| 8 | + - `Triplex.ParamPlug` - loads the tenant from a body or query param |
| 9 | + - `Triplex.SessionPlug` - loads the tenant from a session param |
| 10 | + - `Triplex.SubdomainPlug` - loads the tenant from the url subdomain |
| 11 | + - `Triplex.EnsurePlug` - ensures the current tenant is loaded and halts if not |
| 12 | + """ |
12 | 13 |
|
13 | | - import Plug.Conn |
| 14 | + import Plug.Conn |
14 | 15 |
|
15 | | - @raw_tenant_assign :raw_current_tenant |
| 16 | + @raw_tenant_assign :raw_current_tenant |
16 | 17 |
|
17 | | - @doc """ |
18 | | - Puts the given `tenant` as an assign on the given `conn`, but only if the |
19 | | - tenant is not reserved. |
| 18 | + @doc """ |
| 19 | + Puts the given `tenant` as an assign on the given `conn`, but only if the |
| 20 | + tenant is not reserved. |
20 | 21 |
|
21 | | - The `config` map/struct must have: |
| 22 | + The `config` map/struct must have: |
22 | 23 |
|
23 | | - - `tenant_handler`: function to handle the tenant param. Its return will |
24 | | - be used as the tenant. |
25 | | - - `assign`: the name of the assign where we must save the tenant. |
26 | | - """ |
27 | | - def put_tenant(conn, tenant, config) do |
28 | | - if conn.assigns[config.assign] do |
29 | | - conn |
30 | | - else |
31 | | - conn = assign(conn, @raw_tenant_assign, tenant) |
32 | | - tenant = tenant_handler(tenant, config.tenant_handler) |
33 | | - if Triplex.reserved_tenant?(tenant) do |
| 24 | + - `tenant_handler`: function to handle the tenant param. Its return will |
| 25 | + be used as the tenant. |
| 26 | + - `assign`: the name of the assign where we must save the tenant. |
| 27 | + """ |
| 28 | + def put_tenant(conn, tenant, config) do |
| 29 | + if conn.assigns[config.assign] do |
34 | 30 | conn |
35 | 31 | else |
36 | | - assign(conn, config.assign, tenant) |
| 32 | + conn = assign(conn, @raw_tenant_assign, tenant) |
| 33 | + tenant = tenant_handler(tenant, config.tenant_handler) |
| 34 | + if Triplex.reserved_tenant?(tenant) do |
| 35 | + conn |
| 36 | + else |
| 37 | + assign(conn, config.assign, tenant) |
| 38 | + end |
37 | 39 | end |
38 | 40 | end |
39 | | - end |
40 | 41 |
|
41 | | - @doc """ |
42 | | - Ensure the tenant is loaded, and if not, halts the `conn`. |
| 42 | + @doc """ |
| 43 | + Ensure the tenant is loaded, and if not, halts the `conn`. |
43 | 44 |
|
44 | | - The `config` map/struct must have: |
| 45 | + The `config` map/struct must have: |
45 | 46 |
|
46 | | - - `assign`: the name of the assign where we must save the tenant. |
47 | | - """ |
48 | | - def ensure_tenant(conn, config) do |
49 | | - if loaded_tenant = conn.assigns[config.assign] do |
50 | | - callback(conn, loaded_tenant, config.callback) |
51 | | - else |
52 | | - conn |
53 | | - |> callback(conn.assigns[@raw_tenant_assign], config.failure_callback) |
54 | | - |> halt() |
| 47 | + - `assign`: the name of the assign where we must save the tenant. |
| 48 | + """ |
| 49 | + def ensure_tenant(conn, config) do |
| 50 | + if loaded_tenant = conn.assigns[config.assign] do |
| 51 | + callback(conn, loaded_tenant, config.callback) |
| 52 | + else |
| 53 | + conn |
| 54 | + |> callback(conn.assigns[@raw_tenant_assign], config.failure_callback) |
| 55 | + |> halt() |
| 56 | + end |
55 | 57 | end |
56 | | - end |
57 | 58 |
|
58 | | - defp tenant_handler(tenant, nil), |
59 | | - do: tenant |
60 | | - defp tenant_handler(tenant, handler) when is_function(handler), |
61 | | - do: handler.(tenant) |
| 59 | + defp tenant_handler(tenant, nil), |
| 60 | + do: tenant |
| 61 | + defp tenant_handler(tenant, handler) when is_function(handler), |
| 62 | + do: handler.(tenant) |
62 | 63 |
|
63 | | - defp callback(conn, _, nil), |
64 | | - do: conn |
65 | | - defp callback(conn, tenant, callback) when is_function(callback), |
66 | | - do: callback.(conn, tenant) |
| 64 | + defp callback(conn, _, nil), |
| 65 | + do: conn |
| 66 | + defp callback(conn, tenant, callback) when is_function(callback), |
| 67 | + do: callback.(conn, tenant) |
| 68 | + end |
67 | 69 | end |
68 | | - |
|
0 commit comments