diff --git a/app/models/api_key.rb b/app/models/api_key.rb index 6150e4467..514844d46 100644 --- a/app/models/api_key.rb +++ b/app/models/api_key.rb @@ -42,6 +42,11 @@ def active? !disabled && !expired? end + sig { returns(T::Boolean) } + def permanent? + expires_at.nil? + end + sig { returns(T::Boolean) } def expired? e = expires_at diff --git a/app/models/user.rb b/app/models/user.rb index 4b1510902..12a72335b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,6 +29,13 @@ class User < ApplicationRecord has_many :reports, dependent: :nullify has_many :contact_messages, dependent: :nullify + # Returns true if the user has at least one key that is permanent + # and active + sig { returns(T::Boolean) } + def active_permanent_api_key? + api_keys.any? { |key| key.active? && key.permanent? } + end + # rubocop:disable Style/ArgumentsForwarding # TODO: Arguments forwarding doesn't seem to be supported by sorbet right now? sig { params(notification: T.untyped, args: T.untyped).void } diff --git a/app/views/api_keys/index.html.erb b/app/views/api_keys/index.html.erb index 59fff75fb..36cb3b82d 100644 --- a/app/views/api_keys/index.html.erb +++ b/app/views/api_keys/index.html.erb @@ -24,7 +24,7 @@ <% end %> <%# Don't show the ongoing access box and trial key box if they have an active license that doesn't expire %> -<% unless @api_keys.find(&:active?) && @api_keys.find(&:active?).expires_at.nil? %> +<% unless current_user.active_permanent_api_key? %> <%= render "ongoing_access_box" %> <%= render "create_key_box", disabled: !@api_keys.empty? %> <% end %>