Skip to content

Commit

Permalink
User connection endpoint (#687)
Browse files Browse the repository at this point in the history
* adds two endpoints for listing and deleting connections by username
  • Loading branch information
kickster97 authored Jun 4, 2024
1 parent 39040bd commit 949b803
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
32 changes: 30 additions & 2 deletions spec/api/connections_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe LavinMQ::HTTP::ConnectionsController do
end
end

it "should only show all connections for monitoring" do
it "should show all connections for monitoring" do
Server.users.create("arnold", "pw", [LavinMQ::Tag::Monitoring])
hdrs = HTTP::Headers{"Authorization" => "Basic YXJub2xkOnB3"}
with_channel do
Expand All @@ -35,7 +35,7 @@ describe LavinMQ::HTTP::ConnectionsController do
end

describe "GET /api/vhosts/vhost/connections" do
it "should return network connections" do
it "should return network connections for vhost" do
with_channel do
response = get("/api/vhosts/%2f/connections")
response.status_code.should eq 200
Expand Down Expand Up @@ -98,4 +98,32 @@ describe LavinMQ::HTTP::ConnectionsController do
end
end
end

describe "GET /api/connections/username/:username" do
it "returns connections for a specific user" do
Server.users.create("arnold", "pw", [LavinMQ::Tag::Administrator])
hdrs = HTTP::Headers{"Authorization" => "Basic YXJub2xkOnB3"}
with_channel do
response = get("/api/connections/username/arnold", headers: hdrs)
body = JSON.parse(response.body)
body.as_a.empty?.should be_true
end
end
end

describe "DELETE /api/connections/username/:username" do
it "deletes connections for a specific user" do
Server.users.create("arnold", "pw", [LavinMQ::Tag::Administrator])
Server.users.add_permission("arnold", "/", /.*/, /.*/, /.*/)
hdrs = HTTP::Headers{"Authorization" => "Basic YXJub2xkOnB3"}
with_channel(user: "arnold", password: "pw") do
response = delete("/api/connections/username/arnold", headers: hdrs)
response.status_code.should eq 204
sleep 0.1
response = get("/api/connections/username/arnold", headers: hdrs)
body = JSON.parse(response.body)
body.as_a.empty?.should be_true
end
end
end
end
21 changes: 21 additions & 0 deletions src/lavinmq/http/controller/connections.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ module LavinMQ
page(context, connection.channels.each_value)
end
end

get "/api/connections/username/:username" do |context, params|
connections = get_connections_by_username(context, params["username"])
page(context, connections.each)
end

delete "/api/connections/username/:username" do |context, params|
connections = get_connections_by_username(context, params["username"])
reason = context.request.headers["X-Reason"]? || "Closed via management plugin"
connections.each do |c|
c.close(reason)
end
context.response.status_code = 204
context
end
end

private def get_connections_by_username(context, username)
username = URI.decode_www_form(username)
user = user(context)
connections(user).select { |c| c.user.name == username }
end

private def with_connection(context, params, &)
Expand Down

0 comments on commit 949b803

Please sign in to comment.