Class: Parse::Constraint::WriteableByConstraint

Inherits:
Parse::Constraint 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 Attribute Summary

Attributes inherited from Parse::Constraint

#operand, #operation, #operator, #value

Instance Method Summary collapse

Methods inherited from Parse::Constraint

#as_json, constraint_keyword, create, formatted_value, #formatted_value, #initialize, #key, #precedence, register, #to_s

Constructor Details

This class inherits a constructor from Parse::Constraint

Instance Method Details

#buildHash

Returns the compiled constraint using aggregation pipeline.

Returns:

  • (Hash)

    the compiled constraint using aggregation pipeline.



3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
# File 'lib/parse/query/constraints.rb', line 3146

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:



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

register :writeable_by