access.rb revision d756a4e0f573b9895f5f99e210f049b9f4306b2d
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Boschmodule Repository::Access
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch extend ActiveSupport::Concern
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch OPTIONS = %w[private public_r public_rw]
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch included do
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch scope :pub, where("access != 'private'")
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch scope :accessible_by, ->(user) do
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch if user
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch where("access != 'private'
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch OR id IN (SELECT item_id FROM permissions WHERE item_type = 'Repository' AND subject_type = 'User' AND subject_id = ?)
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch OR id IN (SELECT item_id FROM permissions INNER JOIN team_users ON team_users.team_id = permissions.subject_id AND team_users.user_id = ?
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch WHERE item_type = 'Repository' AND subject_type = 'Team')", user, user)
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch else
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch pub
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch end
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch end
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch validates :access,
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch presence: true,
f1edf7f20661ef9627acbf4054acddcba4d2eb3fStephan Bosch inclusion: { in: Repository::Access::OPTIONS }
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch end
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch def is_private
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch access == 'private'
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch end
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch def public_rw?
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen access == 'public_rw'
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch end
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch private
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch def clear_readers
f1edf7f20661ef9627acbf4054acddcba4d2eb3fStephan Bosch if access_changed? and access_was == 'private'
f1edf7f20661ef9627acbf4054acddcba4d2eb3fStephan Bosch permissions.where(role: 'reader').each { |p| p.destroy }
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch end
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch end
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Boschend
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch