Module: Shared::Permissions
- Extended by:
- ActiveSupport::Concern
- Included in:
- News
- Defined in:
- app/models/concerns/shared/permissions.rb
Instance Method Summary collapse
-
#is_destroyable?(user) ⇒ Boolean
Returns whether it is permissible to try to destroy the record based on its relationships to projects the user is in.
- #is_editable?(user) ⇒ Boolean
- #is_in_users_projects?(user) ⇒ Boolean
Instance Method Details
#is_destroyable?(user) ⇒ Boolean
Returns whether it is permissible to try to destroy the record based on its relationships to projects the user is in. I.e. false if it is related to data in a project in which they user is not a member. !! Does not look at :dependendant assertions
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/concerns/shared/permissions.rb', line 15 def is_destroyable?(user) u = user u = User.find(user) if !user.kind_of?(User) return true if u.is_administrator? p = u.projects.pluck(:id) # TODO: !! replace with a simple wrapped transaction and roll it back self.class.reflect_on_all_associations(:has_many).each do |r| if r.klass.column_names.include?('project_id') # If this has any related data in another project, we can't destroy it # if !send(r.name).nil? return false if send(r.name).where.not(project_id: p).any? # count(:all) > 0 # end end end self.class.reflect_on_all_associations(:has_one).each do |r| if is_community? # *this* object is community, others we don't care about if o = send(r.name) return false if o.respond_to?(:project_id) && !p.include?(o.project_id) end end end true end |
#is_editable?(user) ⇒ Boolean
48 49 50 51 52 53 54 |
# File 'app/models/concerns/shared/permissions.rb', line 48 def is_editable?(user) u = user u = User.find(user) if !user.kind_of?(User) return true if u.is_administrator? || is_community? return false if !is_in_users_projects?(u) true end |
#is_in_users_projects?(user) ⇒ Boolean
44 45 46 |
# File 'app/models/concerns/shared/permissions.rb', line 44 def is_in_users_projects?(user) user.projects.pluck(:id).include?(project_id) end |