Skip to content

Commit

Permalink
chg: [auth] wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gallypette committed May 11, 2023
1 parent 65c9ab0 commit 340e6ab
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 20 deletions.
8 changes: 6 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ config :phoenix, :json_library, Jason
# Fun With Flags configuration
config :fun_with_flags, :cache,
enabled: true,
ttl: 900 # in seconds
# in seconds
ttl: 900

config :fun_with_flags, :persistence,
[adapter: FunWithFlags.Store.Persistent.Ecto, repo: Cocktailparty.Repo]
adapter: FunWithFlags.Store.Persistent.Ecto,
repo: Cocktailparty.Repo

config :fun_with_flags, :cache_bust_notifications,
enabled: true,
adapter: FunWithFlags.Notifications.PhoenixPubSub,
Expand Down
4 changes: 4 additions & 0 deletions lib/cocktailparty/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,8 @@ defmodule Cocktailparty.Accounts do
false -> false
end
end

def get_users_by_role(role) do
Repo.all(from u in User, where: u.role == ^role)
end
end
17 changes: 17 additions & 0 deletions lib/cocktailparty/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ defmodule Cocktailparty.Accounts.User do

import Ecto.Changeset

@uuser "uuser"
@user_role "user"
@poweruser_role "poweruser"

schema "users" do
field :email, :string
field :password, :string, virtual: true, redact: true
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime
field :is_admin, :boolean, default: false
field :role, :string, default: "user"

many_to_many :sources, Cocktailparty.Catalog.Source,
join_through: "sources_subscriptions",
Expand Down Expand Up @@ -160,6 +165,18 @@ defmodule Cocktailparty.Accounts.User do
end
end

@doc """
A user changeset for promoting the user to a new role
"""
def promote_changeset(user, attrs, _opts \\ []) do
user
|> cast(attrs, [:role])
|> validate_inclusion(:role, roles())
end

def roles, do: [@uuser, @user_role, @poweruser_role]

defimpl FunWithFlags.Actor, for: Cocktailparty.Accounts.User do
def id(%Cocktailparty.Accounts.User{id: id}), do: "user:#{id}"
end
Expand Down
1 change: 0 additions & 1 deletion lib/cocktailparty/catalog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ defmodule Cocktailparty.Catalog do
# We ask the broker to delete the source with the old channel
GenServer.cast(Cocktailparty.Broker, {:delete_source, source})


# We update the source
{:ok, source} = Repo.update(changeset)

Expand Down
19 changes: 13 additions & 6 deletions lib/cocktailparty_web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,31 @@
<%= if @current_user do %>
<li>
<.link
:if={@is_admin}
href={~p"/admin/sources"}
class="text-[0.8125rem] leading-6 text-zinc-900 font-semibold hover:text-zinc-700"
:if={@is_admin}
>

>
Admin Sources
</.link>
</li>
<li>
<.link
:if={@is_admin}
href={~p"/admin/sources"}
class="text-[0.8125rem] leading-6 text-zinc-900 font-semibold hover:text-zinc-700"
:if={@is_admin}
>

>
Admin Users
</.link>
</li>
<li>
<.link
:if={@is_admin}
href={~p"/feature-flags"}
class="text-[0.8125rem] leading-6 text-zinc-900 font-semibold hover:text-zinc-700"
>
FeatureFlags
</.link>
</li>
<li>
<.link
href={~p"/sources"}
Expand Down
16 changes: 10 additions & 6 deletions lib/cocktailparty_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ defmodule CocktailpartyWeb.Router do
plug :default_admin_rights
end

pipeline :mounted_apps do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_secure_browser_headers
plug :fetch_current_user
plug :default_admin_rights
end

pipeline :api do
plug :accepts, ["json"]
end
Expand All @@ -29,11 +38,6 @@ defmodule CocktailpartyWeb.Router do
plug :require_admin_user
end

pipeline :mounted_apps do
plug :accepts, ["html"]
plug :put_secure_browser_headers
end

scope "/", CocktailpartyWeb do
pipe_through [:browser, :auth]
get "/", PageController, :home
Expand All @@ -50,7 +54,7 @@ defmodule CocktailpartyWeb.Router do
end

scope path: "/feature-flags" do
pipe_through :mounted_apps
pipe_through [:mounted_apps, :auth, :require_admin]
forward "/", FunWithFlags.UI.Router, namespace: "feature-flags"
end

Expand Down
10 changes: 5 additions & 5 deletions priv/repo/migrations/20230511100454_add_fun_with_flags.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ defmodule Cocktailparty.Repo.Migrations.AddFunWithFlags do
end

create index(
:fun_with_flags_toggles,
[:flag_name, :gate_type, :target],
[unique: true, name: "fwf_flag_name_gate_target_idx"]
)
:fun_with_flags_toggles,
[:flag_name, :gate_type, :target],
unique: true,
name: "fwf_flag_name_gate_target_idx"
)
end

def down do
drop table(:fun_with_flags_toggles)
end

end
9 changes: 9 additions & 0 deletions priv/repo/migrations/20230511135234_add_role_to_users.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Cocktailparty.Repo.Migrations.AddRoleToUsers do
use Ecto.Migration

def change do
alter table(:users) do
add :role, :string, default: "user"
end
end
end

0 comments on commit 340e6ab

Please sign in to comment.