154 lines
3.7 KiB
Ruby
154 lines
3.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Volumen
|
|
# Server-rendered admin routes, registered into Volumen::Server.
|
|
module AdminRoutes
|
|
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
def self.registered(app)
|
|
app.get "/admin" do
|
|
redirect to("/admin/")
|
|
end
|
|
|
|
app.get "/admin/" do
|
|
require_login!
|
|
@posts = sorted_posts
|
|
@crumbs = [["Posts", nil]]
|
|
erb :list
|
|
end
|
|
|
|
app.get "/admin/login" do
|
|
redirect to("/admin/") if authenticated?
|
|
|
|
erb :login
|
|
end
|
|
|
|
app.post "/admin/login" do
|
|
check_csrf!
|
|
check_login_rate_limit!
|
|
user = users.authenticate(params["username"], params["password"])
|
|
if user
|
|
session[:user] = user.username
|
|
redirect to("/admin/")
|
|
else
|
|
@error = "Invalid username or password."
|
|
erb :login
|
|
end
|
|
end
|
|
|
|
app.post "/admin/logout" do
|
|
check_csrf!
|
|
session.clear
|
|
redirect to("/admin/login")
|
|
end
|
|
|
|
app.get "/admin/posts/new" do
|
|
require_login!
|
|
@mode = :new
|
|
@post = Post.new(metadata: { "lang" => config.language })
|
|
@crumbs = [["Posts", to("/admin/")], ["New post", nil]]
|
|
erb :form
|
|
end
|
|
|
|
app.post "/admin/posts" do
|
|
require_login!
|
|
check_csrf!
|
|
create_post
|
|
end
|
|
|
|
app.get "/admin/posts/:slug/edit" do
|
|
require_login!
|
|
@mode = :edit
|
|
@post = store.find(params["slug"])
|
|
halt(404, "Not found") if @post.nil?
|
|
title = @post.title.to_s.empty? ? params["slug"] : @post.title
|
|
@crumbs = [["Posts", to("/admin/")], [title, nil]]
|
|
|
|
erb :form
|
|
end
|
|
|
|
app.post "/admin/posts/:slug" do
|
|
require_login!
|
|
check_csrf!
|
|
update_post
|
|
end
|
|
|
|
app.post "/admin/posts/:slug/delete" do
|
|
require_login!
|
|
check_csrf!
|
|
store.delete(params["slug"])
|
|
clear_posts_cache!
|
|
redirect to("/admin/")
|
|
end
|
|
|
|
app.post "/admin/preview" do
|
|
require_login!
|
|
check_csrf!
|
|
content_type :html
|
|
Markdown.render(params["body"].to_s)
|
|
end
|
|
|
|
app.post "/admin/uploads" do
|
|
require_login!
|
|
check_csrf!
|
|
content_type :json
|
|
upload = params["file"]
|
|
halt(400, json("error" => "no_file")) unless upload.is_a?(Hash) && upload[:tempfile]
|
|
|
|
validate_upload!(upload)
|
|
json("url" => store.store_upload(upload[:filename], upload[:tempfile]))
|
|
end
|
|
|
|
app.get "/media/*" do
|
|
path = store.media_file(params["splat"].first)
|
|
halt(404) if path.nil?
|
|
|
|
send_file(path)
|
|
end
|
|
|
|
app.get "/admin/logo.svg" do
|
|
send_file(File.expand_path("web/public/volumen-logo.svg", __dir__))
|
|
end
|
|
|
|
app.get "/admin/settings" do
|
|
require_login!
|
|
@crumbs = [["Settings", nil]]
|
|
render_settings
|
|
end
|
|
|
|
app.post "/admin/settings/password" do
|
|
require_login!
|
|
check_csrf!
|
|
change_password
|
|
end
|
|
|
|
app.post "/admin/settings/username" do
|
|
require_login!
|
|
check_csrf!
|
|
change_username
|
|
end
|
|
|
|
app.post "/admin/settings/users" do
|
|
require_login!
|
|
require_admin!
|
|
check_csrf!
|
|
create_user
|
|
end
|
|
|
|
app.post "/admin/settings/users/:username/role" do
|
|
require_login!
|
|
require_admin!
|
|
check_csrf!
|
|
change_role(params["username"])
|
|
end
|
|
|
|
app.post "/admin/settings/users/:username/delete" do
|
|
require_login!
|
|
require_admin!
|
|
check_csrf!
|
|
remove_user(params["username"])
|
|
end
|
|
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
end
|
|
end
|
|
end
|