Fakultas Ilmu Komputer UI

Commit 21487729 authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

Merge branch 'deploy-runtime' into 'master'

Deploy runtime

See merge request !3
parents 17e4e476 720becab
...@@ -6,14 +6,15 @@ defmodule Diskuy.Account.User do ...@@ -6,14 +6,15 @@ defmodule Diskuy.Account.User do
field :username, :string field :username, :string
field :email, :string field :email, :string
field :picture, :string field :picture, :string
field :role, :string, default: "reguler"
timestamps() timestamps()
end end
@doc false @doc false
def changeset(user, attrs) do def changeset(user, attrs) do
user user
|> cast(attrs, [:username, :email, :picture]) |> cast(attrs, [:username, :email, :picture, :role])
|> validate_required([:username, :email]) |> validate_required([:username, :email, :role])
|> validate_format(:email, ~r/^[A-Za-z0-9._-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/) |> validate_format(:email, ~r/^[A-Za-z0-9._-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/)
|> unique_constraint(:username) |> unique_constraint(:username)
|> unique_constraint(:email) |> unique_constraint(:email)
......
...@@ -27,10 +27,14 @@ defmodule DiskuyWeb.Auth.GoogleAuth do ...@@ -27,10 +27,14 @@ defmodule DiskuyWeb.Auth.GoogleAuth do
case Account.get_by_username(username) do case Account.get_by_username(username) do
{:ok, _user} -> {:ok, _user} ->
new_username = create_username(username) new_username = create_username(username)
{:ok, user} = Account.create_user(%{username: new_username, email: email, picture: picture}) {:ok, user} = Account.create_user(%{username: new_username,
email: email,
picture: picture, role: "reguler"})
Guardian.create_token(user) Guardian.create_token(user)
{:error, :not_found} -> {:error, :not_found} ->
{:ok, user} = Account.create_user(%{username: username, email: email, picture: picture}) {:ok, user} = Account.create_user(%{username: username,
email: email,
picture: picture, role: "reguler"})
Guardian.create_token(user) Guardian.create_token(user)
end end
end end
......
...@@ -45,4 +45,13 @@ defmodule DiskuyWeb.Auth.Guardian do ...@@ -45,4 +45,13 @@ defmodule DiskuyWeb.Auth.Guardian do
end end
end end
def check_admin(user) do
case user.role == "admin" do
true ->
{:ok, :authorized}
false ->
{:error, :unauthorized}
end
end
end end
...@@ -32,8 +32,9 @@ defmodule DiskuyWeb.PostController do ...@@ -32,8 +32,9 @@ defmodule DiskuyWeb.PostController do
def update(conn, %{"id" => id, "post" => post_params}) do def update(conn, %{"id" => id, "post" => post_params}) do
current_user = Guardian.Plug.current_resource(conn) current_user = Guardian.Plug.current_resource(conn)
post = Forum.get_post!(id) post = Forum.get_post!(id)
new_post_params = post_params |> Map.drop(["id", "points", "user_id", "thread_id"])
with {:ok, :authorized} <- Guardian.check_authorized(current_user, post.user_id), with {:ok, :authorized} <- Guardian.check_authorized(current_user, post.user_id),
{:ok, %Post{} = post} <- Forum.update_post(post, post_params) do {:ok, %Post{} = post} <- Forum.update_post(post, new_post_params) do
render(conn, "show.json", post: post) render(conn, "show.json", post: post)
end end
end end
......
...@@ -32,8 +32,9 @@ defmodule DiskuyWeb.ThreadController do ...@@ -32,8 +32,9 @@ defmodule DiskuyWeb.ThreadController do
def update(conn, %{"id" => id, "thread" => thread_params}) do def update(conn, %{"id" => id, "thread" => thread_params}) do
current_user = Guardian.Plug.current_resource(conn) current_user = Guardian.Plug.current_resource(conn)
thread = Forum.get_thread!(id) thread = Forum.get_thread!(id)
new_thread_params = thread_params |> Map.drop(["id", "points", "user_id", "topic_id"])
with {:ok, :authorized} <- Guardian.check_authorized(current_user, thread.user_id), with {:ok, :authorized} <- Guardian.check_authorized(current_user, thread.user_id),
{:ok, %Thread{} = thread} <- Forum.update_thread(thread, thread_params) do {:ok, %Thread{} = thread} <- Forum.update_thread(thread, new_thread_params) do
render(conn, "show.json", thread: thread) render(conn, "show.json", thread: thread)
end end
end end
......
...@@ -4,6 +4,7 @@ defmodule DiskuyWeb.TopicController do ...@@ -4,6 +4,7 @@ defmodule DiskuyWeb.TopicController do
alias Diskuy.Forum alias Diskuy.Forum
alias Diskuy.Forum.Topic alias Diskuy.Forum.Topic
alias Diskuy.Utility.Utility alias Diskuy.Utility.Utility
alias DiskuyWeb.Auth.Guardian
action_fallback DiskuyWeb.FallbackController action_fallback DiskuyWeb.FallbackController
...@@ -13,7 +14,9 @@ defmodule DiskuyWeb.TopicController do ...@@ -13,7 +14,9 @@ defmodule DiskuyWeb.TopicController do
end end
def create(conn, %{"topic" => topic_params}) do def create(conn, %{"topic" => topic_params}) do
with {:ok, %Topic{} = topic} <- Forum.create_topic(topic_params) do current_user = Guardian.Plug.current_resource(conn)
with {:ok, :authorized} <- Guardian.check_admin(current_user),
{:ok, %Topic{} = topic} <- Forum.create_topic(topic_params) do
conn conn
|> put_status(:created) |> put_status(:created)
|> put_resp_header("location", Routes.topic_path(conn, :show, topic)) |> put_resp_header("location", Routes.topic_path(conn, :show, topic))
...@@ -22,24 +25,21 @@ defmodule DiskuyWeb.TopicController do ...@@ -22,24 +25,21 @@ defmodule DiskuyWeb.TopicController do
end end
def show(conn, %{"id" => id}) do def show(conn, %{"id" => id}) do
new_id = Utility.capitalize_string(id) topic = Forum.get_topic_by_name!(id)
topic = Forum.get_topic_by_name!(new_id)
render(conn, "show.json", topic: topic) render(conn, "show.json", topic: topic)
end end
def update(conn, %{"id" => id, "topic" => topic_params}) do def update(conn, %{"id" => id, "topic" => topic_params}) do
new_id = Utility.capitalize_string(id) topic = Forum.get_topic_by_name!(id)
topic = Forum.get_topic_by_name!(new_id) current_user = Guardian.Plug.current_resource(conn)
with {:ok, :authorized} <- Guardian.check_admin(current_user),
with {:ok, %Topic{} = topic} <- Forum.update_topic(topic, topic_params) do {:ok, %Topic{} = topic} <- Forum.update_topic(topic, topic_params) do
render(conn, "show.json", topic: topic) render(conn, "show.json", topic: topic)
end end
end end
def delete(conn, %{"id" => id}) do def delete(conn, %{"id" => id}) do
new_id = Utility.capitalize_string(id) topic = Forum.get_topic_by_name!(id)
topic = Forum.get_topic_by_name!(new_id)
with {:ok, %Topic{}} <- Forum.delete_topic(topic) do with {:ok, %Topic{}} <- Forum.delete_topic(topic) do
send_resp(conn, :no_content, "") send_resp(conn, :no_content, "")
......
...@@ -36,7 +36,7 @@ defmodule DiskuyWeb.UserController do ...@@ -36,7 +36,7 @@ defmodule DiskuyWeb.UserController do
def update(conn, %{"user" => user_params}) do def update(conn, %{"user" => user_params}) do
user = Guardian.Plug.current_resource(conn) user = Guardian.Plug.current_resource(conn)
new_user_params = user_params |> Map.drop(["email", "id"]) new_user_params = user_params |> Map.drop(["email", "id", "role"])
with {:ok, %User{} = user} <- Account.update_user(user, new_user_params) do with {:ok, %User{} = user} <- Account.update_user(user, new_user_params) do
render(conn, "show.json", user: user) render(conn, "show.json", user: user)
end end
......
...@@ -18,7 +18,8 @@ defmodule DiskuyWeb.UserView do ...@@ -18,7 +18,8 @@ defmodule DiskuyWeb.UserView do
%{id: user.id, %{id: user.id,
username: user.username, username: user.username,
name: user.email, name: user.email,
picture: user.picture picture: user.picture,
role: user.role
} }
end end
...@@ -28,6 +29,7 @@ defmodule DiskuyWeb.UserView do ...@@ -28,6 +29,7 @@ defmodule DiskuyWeb.UserView do
email: user.email, email: user.email,
username: user.username, username: user.username,
picture: user.picture, picture: user.picture,
role: user.role,
token: token token: token
} }
end end
......
...@@ -6,6 +6,7 @@ defmodule Diskuy.Repo.Migrations.CreateUsers do ...@@ -6,6 +6,7 @@ defmodule Diskuy.Repo.Migrations.CreateUsers do
add :username, :string add :username, :string
add :email, :string add :email, :string
add :picture, :string add :picture, :string
add :role, :string
timestamps() timestamps()
end end
create unique_index(:users, [:username]) create unique_index(:users, [:username])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment