Videos are a conceptual subset of Articles (see [VideosController#create][1] for supporting "evidence"). As such, we want to ensure they conform to the expectations of the ArticlePolicy (as described in forem/forem#16483). This feature enhancement does a few things: 1. Renames swaps the `VideoPolicy#new?` and `VideoPolicy#create?` declarations (reversing the alias direction) 2. Moves methods out of the concrete ArticlesPolicy into the ApplicationPolicy. 3. Exposes these user oriented methods for external consumers (e.g. making future refactoring work easier). The happy benefit is that this already removes a bit of duplicated logic. 4. Highlights that while yes the Policy is correct, we also may be flirting with the concept of some sort of User "properties as it relates to authorization" logic (good standing users, established users, etc.) 5. Reworked the timing of a guard clause and setting of instance variables. [1]:33e9bac0f7/app/controllers/videos_controller.rb (L18-L22)) Relates to #16483 Closes forem/forem#16728 To test this, I'm relying on the existing test suite. I envision that shifting from returning false for suspended users to raising an exception might cause some false positive errors. Note, in the [AppliciationController][2] we handle the responses. Paired with [config/appliciation.rb][3], we handle the policy exceptions. [2]:33e9bac0f7/app/controllers/application_controller.rb (L32)[3]:33e9bac0f7/config/application.rb (L71-L81)
23 lines
1 KiB
Ruby
23 lines
1 KiB
Ruby
class VideoPolicy < ApplicationPolicy
|
|
# @return [Boolean] if the user can :create a video.
|
|
# @raise [ApplicationPolicy::UserSuspendedError] if the user's suspended.
|
|
# @raise [ApplicationPolicy::UserRequiredError] if the caller did not provide a user.
|
|
#
|
|
# @todo Consider extracting a user_is_established_in_community as this `user.created_at.before?`
|
|
# question rattles around in the code-base. But I think we'd want guidance on configuring
|
|
# what that means. [@jeremyf] envisions that we could use an admin setting to allow the
|
|
# administrators to set the number of days to consider a "new user".
|
|
def create?
|
|
return false unless Settings::General.enable_video_upload
|
|
|
|
require_user_in_good_standing!
|
|
return false unless user.created_at
|
|
# Newly granted admins get to "short-circuit" the business logic of "were you created too
|
|
# recently?"
|
|
return user_any_admin? if ArticlePolicy.limit_post_creation_to_admins?
|
|
|
|
user.created_at.before?(2.weeks.ago)
|
|
end
|
|
|
|
alias new? create?
|
|
end
|