Fix admin api.

This commit is contained in:
lain 2018-12-01 09:03:16 +01:00
parent d0ec2812bd
commit 1c67277c80
5 changed files with 50 additions and 29 deletions

View File

@ -6,7 +6,7 @@ def init(options) do
options
end
def call(%{assigns: %{user: %User{info: %{"is_admin" => true}}}} = conn, _) do
def call(%{assigns: %{user: %User{info: %{is_admin: true}}}} = conn, _) do
conn
end

View File

@ -16,6 +16,7 @@ defmodule Pleroma.User.Info do
field(:no_rich_text, :boolean, default: false)
field(:ap_enabled, :boolean, default: false)
field(:is_moderator, :boolean, default: false)
field(:is_admin, :boolean, default: false)
field(:keys, :string, default: nil)
field(:settings, :map, default: nil)
field(:magic_key, :string, default: nil)
@ -134,4 +135,12 @@ def set_source_data(info, source_data) do
|> cast(params, [:source_data])
|> validate_required([:source_data])
end
def admin_api_update(info, params) do
info
|> cast(params, [
:is_moderator,
:is_admin
])
end
end

View File

@ -45,21 +45,29 @@ def right_add(conn, %{"permission_group" => permission_group, "nickname" => nick
user = User.get_by_nickname(nickname)
info =
user.info
%{}
|> Map.put("is_" <> permission_group, true)
cng = User.info_changeset(user, %{info: info})
info_cng = User.Info.admin_api_update(user.info, info)
cng =
Ecto.Changeset.change(user)
|> Ecto.Changeset.put_embed(:info, info_cng)
{:ok, user} = User.update_and_set_cache(cng)
conn
|> json(user.info)
|> json(info)
end
def right_get(conn, %{"nickname" => nickname}) do
user = User.get_by_nickname(nickname)
conn
|> json(user.info)
|> json(%{
is_moderator: user.info.is_moderator,
is_admin: user.info.is_admin
})
end
def right_add(conn, _) do
@ -84,14 +92,19 @@ def right_delete(
user = User.get_by_nickname(nickname)
info =
user.info
%{}
|> Map.put("is_" <> permission_group, false)
cng = User.info_changeset(user, %{info: info})
info_cng = User.Info.admin_api_update(user.info, info)
cng =
Ecto.Changeset.change(user)
|> Ecto.Changeset.put_embed(:info, info_cng)
{:ok, user} = User.update_and_set_cache(cng)
conn
|> json(user.info)
|> json(info)
end
end

View File

@ -5,7 +5,7 @@ defmodule Pleroma.Plugs.UserIsAdminPlugTest do
import Pleroma.Factory
test "accepts a user that is admin", %{conn: conn} do
user = insert(:user, info: %{"is_admin" => true})
user = insert(:user, info: %{is_admin: true})
conn =
build_conn()

View File

@ -8,7 +8,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
describe "/api/pleroma/admin/user" do
test "Delete" do
admin = insert(:user, info: %{"is_admin" => true})
admin = insert(:user, info: %{is_admin: true})
user = insert(:user)
conn =
@ -21,7 +21,7 @@ test "Delete" do
end
test "Create" do
admin = insert(:user, info: %{"is_admin" => true})
admin = insert(:user, info: %{is_admin: true})
conn =
build_conn()
@ -39,7 +39,7 @@ test "Create" do
describe "/api/pleroma/admin/permission_group" do
test "GET is giving user_info" do
admin = insert(:user, info: %{"is_admin" => true})
admin = insert(:user, info: %{is_admin: true})
conn =
build_conn()
@ -47,33 +47,30 @@ test "GET is giving user_info" do
|> put_req_header("accept", "application/json")
|> get("/api/pleroma/admin/permission_group/#{admin.nickname}")
assert json_response(conn, 200) == admin.info
assert json_response(conn, 200) == %{
"is_admin" => true,
"is_moderator" => false
}
end
test "/:right POST, can add to a permission group" do
admin = insert(:user, info: %{"is_admin" => true})
admin = insert(:user, info: %{is_admin: true})
user = insert(:user)
user_info =
user.info
|> Map.put("is_admin", true)
conn =
build_conn()
|> assign(:user, admin)
|> put_req_header("accept", "application/json")
|> post("/api/pleroma/admin/permission_group/#{user.nickname}/admin")
assert json_response(conn, 200) == user_info
assert json_response(conn, 200) == %{
"is_admin" => true
}
end
test "/:right DELETE, can remove from a permission group" do
admin = insert(:user, info: %{"is_admin" => true})
user = insert(:user, info: %{"is_admin" => true})
user_info =
user.info
|> Map.put("is_admin", false)
admin = insert(:user, info: %{is_admin: true})
user = insert(:user, info: %{is_admin: true})
conn =
build_conn()
@ -81,12 +78,14 @@ test "/:right DELETE, can remove from a permission group" do
|> put_req_header("accept", "application/json")
|> delete("/api/pleroma/admin/permission_group/#{user.nickname}/admin")
assert json_response(conn, 200) == user_info
assert json_response(conn, 200) == %{
"is_admin" => false
}
end
end
test "/api/pleroma/admin/invite_token" do
admin = insert(:user, info: %{"is_admin" => true})
admin = insert(:user, info: %{is_admin: true})
conn =
build_conn()
@ -98,8 +97,8 @@ test "/api/pleroma/admin/invite_token" do
end
test "/api/pleroma/admin/password_reset" do
admin = insert(:user, info: %{"is_admin" => true})
user = insert(:user, info: %{"is_admin" => true})
admin = insert(:user, info: %{is_admin: true})
user = insert(:user)
conn =
build_conn()