Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit photo album visibility #286

Draft
wants to merge 1 commit into
base: staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/models/photo_album.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class PhotoAlbum < ApplicationRecord
validates :publicly_visible, inclusion: [true, false]

scope :publicly_visible, (-> { where(publicly_visible: true) })
scope :posted_between_or_publicly_visible, (lambda { |start_date, end_date|
where(publicly_visible: true)
.or(where.not(date: nil).where(date: start_date..end_date))
.or(where(date: nil).where(created_at: start_date..end_date))
})

def owners
if group.present?
Expand Down
10 changes: 8 additions & 2 deletions app/policies/photo_album_policy.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
class PhotoAlbumPolicy < ApplicationPolicy
class Scope < ApplicationPolicy::Scope
def resolve
def resolve # rubocop:disable Metrics/AbcSize
if user_can_read?
scope
membership = user.memberships.joins(:group).where(groups: { name: 'Leden' }).first
return scope.publicly_visible if membership.nil?

scope.posted_between_or_publicly_visible(
membership.start_date&.advance(months: -18),
membership.end_date&.advance(months: 6)
)
else
scope.publicly_visible
end
Expand Down