diff --git a/config/routes.rb b/config/routes.rb index d53e0e6..56bedc6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,18 +1,14 @@ # frozen_string_literal: true require "hanami/middleware/body_parser" -require "adamantium/middleware/basic_auth" +# require_relative "../slices/admin/config/routes" +# require_relative "authenticated_admin_action" module Adamantium class Routes < Hanami::Routes use Hanami::Middleware::BodyParser, [:form, :json] # use Adamantium::Middleware::ProcessParams - if Hanami.app.settings.basic_auth_username && Hanami.app.settings.basic_auth_password - use Adamantium::Middleware::BasicAuth do |username, password| - username == Hanami.app.settings.basic_auth_username && - password == Hanami.app.settings.basic_auth_password - end - end + scope "micropub" do get "/", to: "site.config" @@ -66,45 +62,6 @@ module Adamantium redirect "deploying-a-hanami-app-to-fly-io", to: "/post/deploying-a-hanami-20-app-to-flyio" redirect "deploying-a-hanami-app-to-fly-io/", to: "/post/deploying-a-hanami-20-app-to-flyio" - slice :admin, at: "/admin" do - get "/", to: "index" - - get "/tags", to: "tags.index" - delete "/tags/:id", to: "tags.delete" - - get "/tags/auto_tagging", to: "auto_tagging.index" - get "/tags/auto_tagging/new", to: "auto_tagging.new" - post "/tags/auto_tagging", to: "auto_tagging.create" - delete "/tags/auto_taggings/:id", to: "auto_tagging.delete" - - get "/tags/merge", to: "merge_tags.index" - get "/tags/merge/:id", to: "merge_tags.new" - post "/tags/merge", to: "merge_tags.merge" - - get "/bookmarks", to: "bookmarks.index" - delete "/bookmarks/:id", to: "bookmarks.delete" - post "/bookmarks/clean", to: "bookmarks.clean" - post "/bookmarks/cache/:id", to: "bookmarks.cache" - post "/bookmarks/:id/archive", to: "bookmarks.archive" - post "/bookmarks/:id/publish", to: "bookmarks.publish" - - get "/posts", to: "posts.index" - delete "/posts/:id", to: "posts.delete" - post "/posts/:id/archive", to: "posts.archive" - post "/posts/:id/publish", to: "posts.publish" - get "/posts/:id", to: "posts.show" - post "/posts/:id/syndicate/:target", to: "posts.syndicate" - - get "/media", to: "photos.index" - delete "/media/public/media/:year/:path", to: "photos.delete" - - get "/trips", to: "trips.index" - get "/trips/:id", to: "trips.show" - post "/trips", to: "trips.create" - post "/trips/add_post", to: "trips.add_post" - post "/trips/remove_post", to: "trips.remove_post" - get "/trips/new", to: "trips.new" - post "/trips/:id", to: "trips.update" - end + slice :admin, at: "/admin" end end diff --git a/lib/adamantium/middleware/basic_auth.rb b/lib/adamantium/middleware/basic_auth.rb deleted file mode 100644 index 1338b21..0000000 --- a/lib/adamantium/middleware/basic_auth.rb +++ /dev/null @@ -1,16 +0,0 @@ -module Adamantium - module Middleware - class BasicAuth < Rack::Auth::Basic - def call(env) - request = Rack::Request.new(env) - if /^\/admin*/.match?(request.path) - # Execute basic authentication - super(env) - else - # Pass basic authentication - @app.call(env) - end - end - end - end -end diff --git a/slices/admin/config/authenticated_admin_action.rb b/slices/admin/config/authenticated_admin_action.rb new file mode 100644 index 0000000..514ff55 --- /dev/null +++ b/slices/admin/config/authenticated_admin_action.rb @@ -0,0 +1,17 @@ +module Adamantium + class AuthenticatedAdminAction + def self.call(action:) + + action_proc = ->(env) { Admin::Container["actions.#{action}"].(env) } + + if Hanami.app.settings.basic_auth_username && Hanami.app.settings.basic_auth_password + Rack::Auth::Basic.new(action_proc) do |username, password| + username == Hanami.app.settings.basic_auth_username && + password == Hanami.app.settings.basic_auth_password + end + else + Rack::Auth::Basic.new(action_proc) { |_username, _password| true } + end + end + end +end \ No newline at end of file diff --git a/slices/admin/config/routes.rb b/slices/admin/config/routes.rb new file mode 100644 index 0000000..fc9d52c --- /dev/null +++ b/slices/admin/config/routes.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require "hanami/middleware/body_parser" +require_relative "authenticated_admin_action" + +module Admin + class Routes < Hanami::Routes + use Hanami::Middleware::BodyParser, [:form, :json] + + Auth = Adamantium::AuthenticatedAdminAction + + get "/", to: Auth.(action: "index") + + get "/tags", to: Auth.(action: "tags.index") + delete "/tags/:id", to: Auth.(action: "tags.delete") + + get "/tags/auto_tagging", to: Auth.(action: "auto_tagging.index") + get "/tags/auto_tagging/new", to: Auth.(action: "auto_tagging.new") + post "/tags/auto_tagging", to: Auth.(action: "auto_tagging.create") + delete "/tags/auto_taggings/:id", to: Auth.(action: "auto_tagging.delete") + + get "/tags/merge", to: Auth.(action: "merge_tags.index") + get "/tags/merge/:id", to: Auth.(action: "merge_tags.new") + post "/tags/merge", to: Auth.(action: "merge_tags.merge") + + get "/bookmarks", to: Auth.(action: "bookmarks.index") + delete "/bookmarks/:id", to: Auth.(action: "bookmarks.delete") + post "/bookmarks/clean", to: Auth.(action: "bookmarks.clean") + post "/bookmarks/cache/:id", to: Auth.(action: "bookmarks.cache") + post "/bookmarks/:id/archive", to: Auth.(action: "bookmarks.archive") + post "/bookmarks/:id/publish", to: Auth.(action: "bookmarks.publish") + + get "/posts", to: Auth.(action: "posts.index") + delete "/posts/:id", to: Auth.(action: "posts.delete") + post "/posts/:id/archive", to: Auth.(action: "posts.archive") + post "/posts/:id/publish", to: Auth.(action: "posts.publish") + get "/posts/:id", to: Auth.(action: "posts.show") + post "/posts/:id/syndicate/:target", to: Auth.(action: "posts.syndicate") + + get "/media", to: Auth.(action: "photos.index") + delete "/media/public/media/:year/:path", to: Auth.(action: "photos.delete") + + get "/trips", to: Auth.(action: "trips.index") + get "/trips/:id", to: Auth.(action: "trips.show") + post "/trips", to: Auth.(action: "trips.create") + post "/trips/add_post", to: Auth.(action: "trips.add_post") + post "/trips/remove_post", to: Auth.(action: "trips.remove_post") + get "/trips/new", to: Auth.(action: "trips.new") + post "/trips/:id", to: Auth.(action: "trips.update") + end +end