sudoers!
This commit is contained in:
parent
1e60a11bbe
commit
626a0cb572
|
@ -0,0 +1,10 @@
|
|||
defmodule Auth do
|
||||
use Tesla
|
||||
|
||||
plug Tesla.Middleware.BaseUrl, "https://auth.tilde.team/"
|
||||
plug Tesla.Middleware.JSON
|
||||
|
||||
def authenticate(user, pw) do
|
||||
get("?json&user=" <> user <> "&pw=" <> pw)
|
||||
end
|
||||
end
|
|
@ -6,6 +6,7 @@ defmodule Forum.User do
|
|||
schema "users" do
|
||||
field(:bio, :string)
|
||||
field(:name, :string)
|
||||
field(:sudoer, :boolean)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
@ -13,7 +14,7 @@ defmodule Forum.User do
|
|||
@doc false
|
||||
def changeset(%User{} = user, attrs) do
|
||||
user
|
||||
|> cast(attrs, [:name, :bio])
|
||||
|> cast(attrs, [:name, :bio, :sudoer])
|
||||
|> validate_required([:name, :bio])
|
||||
|> unique_constraint(:name)
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
defmodule ForumWeb.LoginController do
|
||||
use ForumWeb, :controller
|
||||
import Ecto.Query
|
||||
# import Ecto.Query
|
||||
import Plug.Conn
|
||||
|
||||
alias Forum.Repo
|
||||
|
@ -11,21 +11,23 @@ defmodule ForumWeb.LoginController do
|
|||
end
|
||||
|
||||
def authenticate(conn, %{"username" => user, "password" => pw}) do
|
||||
resp = Tesla.get("https://auth.tilde.team/?user=#{user}&pw=#{pw}")
|
||||
# resp = Tesla.get("https://auth.tilde.team/?user=#{user}&pw=#{pw}")
|
||||
respjson = Auth.authenticate(user, pw).body |> Poison.decode!
|
||||
|
||||
case resp.body do
|
||||
"0" ->
|
||||
case respjson["authenticated"] do
|
||||
false ->
|
||||
conn
|
||||
|> put_session(:loggedin, false)
|
||||
|> put_flash(:error, "log in failed")
|
||||
|> redirect(to: login_path(conn, :index))
|
||||
|
||||
"1" ->
|
||||
case Repo.one(User |> where(name: ^user)) do
|
||||
true ->
|
||||
case Repo.get_by(User, name: user) do
|
||||
nil ->
|
||||
case Forum.create_user(%{
|
||||
name: user,
|
||||
bio: "[tildepage](https://tilde.team/~#{user}/)\r\n\r\nchange meeeee"
|
||||
bio: "sample bio - change me",
|
||||
sudoer: respjson["sudoer"]
|
||||
}) do
|
||||
{:ok, user} ->
|
||||
IO.inspect(user)
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<div class="panel-footer">
|
||||
<div class="pull-right">
|
||||
<a href="#<%= @i %>">permalink</a>
|
||||
<%= if @post.user == Util.get_session_user(@conn) do %>
|
||||
<%= if @post.user == Util.get_session_user(@conn) or Util.get_session_user(@conn).sudoer do %>
|
||||
<%= link "edit post", to: post_path(@conn, :edit, @post), class: "btn btn-info btn-xs" %>
|
||||
<%= link "delet this", to: post_path(@conn, :delete, @post), method: :delete, data: [confirm: "are you sure?"], class: "btn btn-danger btn-xs" %>
|
||||
<% end %>
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<h4><a href="<%= thread_path(@conn, :show, thread) %>"><%= thread.name %></a></h4>
|
||||
</div>
|
||||
<div class="panel-footer">
|
||||
<%= if thread.user == Util.get_session_user(@conn) do %>
|
||||
<%= if thread.user == Util.get_session_user(@conn) or Util.get_session_user(@conn).sudoer do %>
|
||||
<div class="pull-right">
|
||||
<%= link "edit", to: thread_path(@conn, :edit, thread), class: "btn btn-info btn-xs" %>
|
||||
<%= link "delete", to: thread_path(@conn, :delete, thread), method: :delete, data: [confirm: "are you sure?"], class: "btn btn-danger btn-xs" %>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<%= if @thread.user == Util.get_session_user(@conn) do %>
|
||||
<%= if @thread.user == Util.get_session_user(@conn) or Util.get_session_user(@conn).sudoer do %>
|
||||
<div class="pull-right">
|
||||
<span><%= link "edit thread title", to: thread_path(@conn, :edit, @thread), class: "btn btn-info btn-xs" %></span>
|
||||
</div>
|
||||
|
|
1
mix.exs
1
mix.exs
|
@ -43,6 +43,7 @@ defmodule Forum.Mixfile do
|
|||
{:phoenix_html, "~> 2.10"},
|
||||
{:phoenix_live_reload, "~> 1.0", only: :dev},
|
||||
{:phoenix_pubsub, "~> 1.0"},
|
||||
{:poison, "~> 3.1"},
|
||||
{:tesla, ">= 0.10.0"},
|
||||
{:timex, "~> 3.0"}
|
||||
]
|
||||
|
|
|
@ -5,6 +5,7 @@ defmodule Forum.Repo.Migrations.CreateUsers do
|
|||
create table(:users) do
|
||||
add :name, :string
|
||||
add :bio, :text
|
||||
add :sudoer, :boolean, default: false
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
|
Reference in New Issue