Class: Parse::Constraint::WriteableByConstraint

Inherits:
Constraint
  • Object
show all
Includes:
AclConstraintHelpers
Defined in:
lib/parse/query/constraints.rb

Overview

Note:

This constraint uses aggregation pipeline because Parse Server restricts direct queries on the internal _wperm field.

ACL Write Permission Query Constraint Query objects based on write permissions using MongoDB's internal _wperm field. Parse Server restricts direct queries on _wperm, so this uses aggregation pipeline.

Examples:

Find objects with NO write permissions (master key only / read-only)

Song.query.where(:acl.writeable_by => [])

Find objects writable by a specific user ID

Song.query.where(:acl.writeable_by => "userId123")
Song.query.where(:acl.writeable_by => current_user)

Find objects writable by a role

Song.query.where(:acl.writeable_by => "role:Admin")

Direct Known Subclasses

WritableByConstraint

Instance Method Summary collapse

Instance Method Details

#buildHash

Returns the compiled constraint using aggregation pipeline.

Returns:

  • (Hash)

    the compiled constraint using aggregation pipeline.



3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
# File 'lib/parse/query/constraints.rb', line 3117

def build
  keys = normalize_acl_keys(@value)

  if keys.empty?
    # Empty array = no write permissions (master key only)
    pipeline = [
      {
        "$match" => {
          "$or" => [
            { "_wperm" => { "$exists" => true, "$eq" => [] } },
            { "_wperm" => { "$exists" => false } },
          ],
        },
      },
    ]
  else
    # Find objects writable by ANY of the specified keys
    pipeline = [
      {
        "$match" => {
          "_wperm" => { "$in" => keys },
        },
      },
    ]
  end

  { "__aggregation_pipeline" => pipeline }
end

#writeable_byWriteableByConstraint

A registered method on a symbol to create the constraint.

Examples:

q.where :acl.writeable_by => []
q.where :acl.writeable_by => "userId"

Returns:



3114
# File 'lib/parse/query/constraints.rb', line 3114

register :writeable_by