asxpio/lib/admin_acl.rb
Sergei Poljanski 8471570187
admin: per-area permissions on every route
Give each user independent read, write and delete flags per area
(invoices, bingo, users), so someone can e.g. review and remove bingo
incidents without being able to add any. The request method and path
decide the flag needed: GET is read except the new/duplicate forms, POST
to .../delete is delete, any other POST is write. Unmapped admin paths
are superuser-only, and a test enumerates every registered admin route
so a new one cannot ship without a rule.

Pages follow the same flags: nav and dashboard tiles show readable areas
only, write forms and delete buttons are hidden, and the datacenter form
is shown read-only.

Assisted-by: Claude:opus-5
2026-10-10 08:27:37 +04:00

33 lines
1.1 KiB
Ruby

# Which permission an /admin request needs. Every admin path must map to
# an area (or be public/open); unmapped paths are superuser-only, and a
# test fails if any registered route is unmapped.
#
# GET/HEAD → read (except …/new and …/duplicate: write)
# POST …/delete, …/disable → delete
# any other non-GET → write
module AdminACL
RULES = [
[%r{\A/admin/(invoices|gel-rate|crypto-rate)(/|\z)}, 'invoices'],
[%r{\A/admin/bingo(/|\z)}, 'bingo'],
[%r{\A/admin/(users|audit)(/|\z)}, 'users'],
].freeze
module_function
def area_for(path)
RULES.find { |re, _| path.match?(re) }&.last
end
def perm_for(method, path)
get = %w[GET HEAD].include?(method)
return 'delete' if !get && path.match?(%r{/(delete|disable)\z})
return 'write' if !get || path.match?(%r{/(new|duplicate)\z})
'read'
end
def allowed?(user, method, path)
return true if user.superuser || path.match?(AdminAuth::OPEN)
area = area_for(path) or return false
user.can?(area, perm_for(method, path))
end
end