policy simplification

This commit is contained in:
Dylan Knutson
2025-03-03 04:57:48 +00:00
parent 24e6d0cf66
commit 111a22ff8a
9 changed files with 69 additions and 46 deletions

View File

@@ -10,6 +10,12 @@ class ApplicationPolicy
sig { returns(T.untyped) }
attr_reader :record
sig { params(user: T.nilable(User), record: T.untyped).void }
def initialize(user, record)
@user = user
@record = record
end
sig { returns(String) }
def cache_key
# find all the policy methods that the subclass defines up to ApplicationPolicy
@@ -27,10 +33,19 @@ class ApplicationPolicy
"#{self.class.name}::#{Digest::SHA256.hexdigest(method_values_string)[0..16]}"
end
sig { params(user: T.nilable(User), record: T.untyped).void }
def initialize(user, record)
@user = user
@record = record
sig(:final) { returns(T::Boolean) }
def is_role_admin?
user&.admin? || false
end
sig(:final) { returns(T::Boolean) }
def is_role_moderator?
user&.moderator? || false
end
sig(:final) { returns(T::Boolean) }
def is_role_user?
user&.user? || false
end
sig { returns(T::Boolean) }
@@ -68,6 +83,11 @@ class ApplicationPolicy
false
end
sig { returns(T::Boolean) }
def view_ip_address?
is_role_admin?
end
class Scope
extend T::Sig

View File

@@ -5,11 +5,11 @@ class Domain::Fa::PostPolicy < ApplicationPolicy
end
def view_file?
user&.admin?
is_role_admin?
end
def view_scraper_metadata?
user&.admin?
is_role_admin?
end
class Scope < ApplicationPolicy::Scope

View File

@@ -10,15 +10,15 @@ class Domain::Fa::UserPolicy < ApplicationPolicy
# Only admins and moderators can access these actions
def scan_user?
user&.admin? || user&.moderator?
is_role_admin? || is_role_moderator?
end
def enqueue_objects?
user&.admin? || user&.moderator?
is_role_admin? || is_role_moderator?
end
def view_scraped_at_timestamps?
user&.admin?
is_role_admin?
end
class Scope < ApplicationPolicy::Scope

View File

@@ -5,11 +5,11 @@ class Domain::Inkbunny::PostPolicy < ApplicationPolicy
end
def view_file?
user&.admin?
is_role_admin?
end
def view_scraper_metadata?
user&.admin?
is_role_admin?
end
class Scope < ApplicationPolicy::Scope

View File

@@ -14,22 +14,22 @@ class Domain::PostPolicy < ApplicationPolicy
sig { returns(T::Boolean) }
def view_file?
user&.admin? || user&.moderator? || false
is_role_admin? || is_role_moderator?
end
sig { returns(T::Boolean) }
def view_scan_metadata?
user&.admin? || user&.moderator? || false
is_role_admin? || is_role_moderator?
end
sig { returns(T::Boolean) }
def users_faving_post?
user&.admin? || user&.moderator? || false
is_role_admin? || is_role_moderator?
end
sig { returns(T::Boolean) }
def view_faved_by?
user&.admin? || user&.moderator? || false
is_role_admin? || is_role_moderator?
end
sig { returns(T::Boolean) }

View File

@@ -29,21 +29,21 @@ class Domain::UserPolicy < ApplicationPolicy
sig { returns(T::Boolean) }
def view_page_scanned_at_timestamps?
user&.admin? || user&.moderator? || false
is_role_admin? || is_role_moderator?
end
sig { returns(T::Boolean) }
def view_log_entries?
user&.admin? || false
is_role_admin?
end
sig { returns(T::Boolean) }
def followed_by?
user&.admin? || user&.moderator? || false
is_role_admin? || is_role_moderator?
end
sig { returns(T::Boolean) }
def following?
user&.admin? || user&.moderator? || false
is_role_admin? || is_role_moderator?
end
end

View File

@@ -4,57 +4,57 @@ class GlobalStatePolicy < ApplicationPolicy
sig { returns(T::Boolean) }
def index?
user&.admin? || false
is_role_admin?
end
sig { returns(T::Boolean) }
def show?
user&.admin? || false
is_role_admin?
end
sig { returns(T::Boolean) }
def create?
user&.admin? || false
is_role_admin?
end
sig { returns(T::Boolean) }
def update?
user&.admin? || false
is_role_admin?
end
sig { returns(T::Boolean) }
def destroy?
user&.admin? || false
is_role_admin?
end
sig { returns(T::Boolean) }
def fa_cookies?
user&.admin? || false
is_role_admin?
end
sig { returns(T::Boolean) }
def edit_fa_cookies?
user&.admin? || false
is_role_admin?
end
sig { returns(T::Boolean) }
def update_fa_cookies?
user&.admin? || false
is_role_admin?
end
sig { returns(T::Boolean) }
def ib_cookies?
user&.admin? || false
is_role_admin?
end
sig { returns(T::Boolean) }
def edit_ib_cookies?
user&.admin? || false
is_role_admin?
end
sig { returns(T::Boolean) }
def update_ib_cookies?
user&.admin? || false
is_role_admin?
end
class Scope < ApplicationPolicy::Scope

View File

@@ -4,12 +4,12 @@ class State::IpAddressRolePolicy < ApplicationPolicy
sig { returns(T::Boolean) }
def index?
user_is_admin? || ip_is_admin?
is_role_admin? || ip_is_admin?
end
sig { returns(T::Boolean) }
def create?
user_is_admin? || ip_is_admin?
is_role_admin? || ip_is_admin?
end
sig { returns(T::Boolean) }
@@ -19,7 +19,7 @@ class State::IpAddressRolePolicy < ApplicationPolicy
sig { returns(T::Boolean) }
def update?
user_is_admin? || ip_is_admin?
is_role_admin? || ip_is_admin?
end
sig { returns(T::Boolean) }
@@ -29,16 +29,11 @@ class State::IpAddressRolePolicy < ApplicationPolicy
sig { returns(T::Boolean) }
def destroy?
user_is_admin? || ip_is_admin?
is_role_admin?
end
private
sig { returns(T::Boolean) }
def user_is_admin?
T.cast(user, T.nilable(User))&.admin? || false
end
sig { returns(T::Boolean) }
def ip_is_admin?
ip_role = @controller.current_ip_address_role

View File

@@ -23,14 +23,22 @@
<body class="mx-0 flex flex-col h-full">
<header class="bg-slate-100 border-slate-200 border-b-2">
<div class="mx-auto max-w-5xl py-6 px-6 sm:px-8 flex items-baseline">
<h1 class="text-4xl sm:text-5xl font-bold text-slate-900">
<%= link_to root_path, class: "flex items-center" do %>
<%= image_tag asset_path("refurrer-logo-md.png"), class: "w-12 h-12 mr-2" %>
<span class="text-4xl sm:text-5xl font-bold text-slate-900">
ReFurrer
<span class="flex flex-col gap-2">
<h1 class="text-4xl sm:text-5xl font-bold text-slate-900">
<%= link_to root_path, class: "flex items-center" do %>
<%= image_tag asset_path("refurrer-logo-md.png"), class: "w-12 h-12 mr-2" %>
<span class="text-4xl sm:text-5xl font-bold text-slate-900">
ReFurrer
</span>
<% end %>
</h1>
<span class="font-mono text-slate-500">
<span>IP address</span>
<span class="font-mono text-slate-700">
<%= request.remote_ip %>
</span>
<% end %>
</h1>
</span>
</span>
<div class="flex-grow"></div>
<nav class="flex items-center space-x-4">
<% if user_signed_in? %>