Module: Collavre::Creative::Permissible
- Extended by:
- ActiveSupport::Concern
- Included in:
- Collavre::Creative
- Defined in:
- app/models/collavre/creative/permissible.rb
Instance Method Summary collapse
- #all_shared_users(required_permission = :no_access) ⇒ Object
-
#children_with_permission(user = nil, min_permission = :read) ⇒ Object
Returns only children for which the user has at least the given permission.
- #find_ai_agent(required_permission = :write) ⇒ Object
- #has_permission?(user, required_permission = :read) ⇒ Boolean
Instance Method Details
#all_shared_users(required_permission = :no_access) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/collavre/creative/permissible.rb', line 64 def all_shared_users( = :no_access) base_creative = effective_origin(Set.new) ancestor_ids = [ base_creative.id ] + base_creative.ancestors.pluck(:id) = CreativeShare..fetch(.to_s) shares = CreativeShare.where(creative_id: ancestor_ids).includes(:user) shares_for_user_hash = shares.group_by(&:user_id) shares_for_user_hash.filter_map do |_user_id, user_shares| closest_share = CreativeShare.closest_parent_share(ancestor_ids, user_shares) next unless closest_share = CreativeShare..fetch(closest_share..to_s) next if < closest_share end end |
#children_with_permission(user = nil, min_permission = :read) ⇒ Object
Returns only children for which the user has at least the given permission
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/models/collavre/creative/permissible.rb', line 20 def (user = nil, = :read) user ||= Collavre.current_user children_scope = effective_origin(Set.new).children children_ids = children_scope.pluck(:id) return [] if children_ids.empty? min_rank = CreativeShare.[.to_s] accessible_ids = Set.new if user user_entries = CreativeSharesCache .where(creative_id: children_ids, user_id: user.id) .pluck(:creative_id, :permission) user_has_entry = Set.new user_entries.each do |cid, perm| user_has_entry << cid perm_rank = CreativeSharesCache.[perm] if perm_rank && perm_rank >= min_rank && perm_rank != CreativeSharesCache.[:no_access] accessible_ids << cid end end public_accessible = CreativeSharesCache .where(creative_id: children_ids, user_id: nil) .where("permission >= ?", min_rank) .where.not(permission: :no_access) .pluck(:creative_id) accessible_ids.merge(public_accessible - user_has_entry.to_a) owned_ids = children_scope.where(user_id: user.id).pluck(:id) accessible_ids.merge(owned_ids) else accessible_ids = CreativeSharesCache .where(creative_id: children_ids, user_id: nil) .where("permission >= ?", min_rank) .where.not(permission: :no_access) .pluck(:creative_id) .to_set end children_scope.where(id: accessible_ids.to_a).order(:sequence).to_a end |
#find_ai_agent(required_permission = :write) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/collavre/creative/permissible.rb', line 83 def find_ai_agent( = :write) base_creative = effective_origin(Set.new) ancestor_ids = [ base_creative.id ] + base_creative.ancestors.pluck(:id) = CreativeShare..fetch(.to_s) shares = CreativeShare.where(creative_id: ancestor_ids) .joins(:user).merge(User.ai_agents) .includes(:user) shares_for_user_hash = shares.group_by(&:user_id) shares_for_user_hash.each_value do |user_shares| closest_share = CreativeShare.closest_parent_share(ancestor_ids, user_shares) next unless closest_share = CreativeShare..fetch(closest_share..to_s) next if < return closest_share.user end nil end |
#has_permission?(user, required_permission = :read) ⇒ Boolean
15 16 17 |
# File 'app/models/collavre/creative/permissible.rb', line 15 def (user, = :read) Collavre::Creatives::PermissionChecker.new(self, user).allowed?() end |